PROGRAMS – MODULE 3
ONE DIMENSIONAL ARRAYS
1. 1D ARRAY CREATION AND DISPLAY THE ELEMENTS
/*Program to create a 1D array having n elements*/
#include<stdio.h>
void main()
{
int A[20],i,n;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter elements into the array\n");
for(i=0;i<n;i++)
{
scanf("%d", &A[i]);
}
printf("The array is\n");
for(i=0;i<n;i++)
{
printf("%d\t", A[i]);
}
}
Output
Enter the size of the array
5
Enter elements into the array
1 2 3 4 5
The array is
1 2 3 4 5
-----------------------------------------------------------------------------------------------------
2. SUM AND AVERAGE OF ELEMENTS IN AN ARRAY
/*Sum and Avg of n elements in an araay*/
#include<stdio.h>
void main()
{
int A[20],i,n,sum=0;
float avg;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter elements into the array\n");
for(i=0;i<n;i++)
{
scanf("%d", &A[i]);
sum=sum+A[i];
}
avg=(float)sum/n;
printf("Sum of elements in the array=%d\n",sum);
printf("Average of elements in the array=%f\n",avg);
}
Output
Enter the size of the array
5
Enter elements into the array
1 2 3 4 5
Sum of elements in the array=15
Average of elements in the array=3.000000
-----------------------------------------------------------------------------------------------------
3. LARGEST ELEMNT IN AN ARRAY
/*Largest elements in an araay*/
#include<stdio.h>
void main()
{
int A[10],n,i;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter elements into the array\n");
for(i=0;i<n;i++)
{
scanf("%d", &A[i]);
}
for(i=1;i<n;i++)
{
if(A[i]> A[0])
{
A[0]=A[i];
}
}
printf("Largest element in the array is:%d",A[0]);
}
Output
Enter the size of the array
5
Enter elements into the array
3 5 1 8 4
Largest element in the array is : 8
-----------------------------------------------------------------------------------------------------
4. SMALLEST ELEMNT IN AN ARRAY
/*Smallest elements in an araay*/
#include<stdio.h>
void main()
{
int A[20],i,n;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter elements into the array\n");
for(i=0;i<n;i++)
{
scanf("%d", &A[i]);
}
for(i=1;i<n;i++)
{
if(A[i]< A[0])
{
A[0]=A[i];
}
}
printf("Smallest element in the array is:%d",A[0]);
}
Output
Enter the size of the array
5
Enter elements into the array
8 2 0 6 4
Smallest element in the array is : 0
-----------------------------------------------------------------------------------------------------
5. LINEAR SEARCH
/*Linear Search*/
#include<stdio.h>
void main()
{
int A[20],i,n,key,flag=0;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter elements into the array\n");
for(i=0;i<n;i++)
{
scanf("%d", &A[i]);
}
printf("Enter the elements to be searched\n");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(A[i]==key)
{
flag=1;
break;
}
}
if(flag==1)
printf("Search element found at position %d\n",i+1);
else
printf("Search element not found");
}
Output 1
Enter the size of the array
5
Enter elements into the array
1 2 3 4 5
Enter the elements to be searched
4
Search element found at position 4
Output 2
Enter the size of the array
5
Enter elements into the array
1 2 3 4 5
Enter the elements to be searched
4
Search element not found
-----------------------------------------------------------------------------------------------------
6. BUBBLE SORT
/*Bubble sort*/
#include<stdio.h>
void main()
{
int A[20],i,j,n,temp;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter elements into the array\n");
for(i=0;i<n;i++)
{
scanf("%d", &A[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(A[j]> A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
}
printf("The sorted array is\n");
for(i=0;i<n;i++)
{
printf("%d\t",A[i]);
}
}
Output
Enter the size of the array
5
Enter elements into the array
44 11 88 66 22
The sorted array is
11 22 44 66 88
-----------------------------------------------------------------------------------------------------
7. SELECTION SORT
/*Selection sort*/
#include<stdio.h>
void main()
{
int A[20],i,j,n,temp;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter elements into the array\n");
for(i=0;i<n;i++)
{
scanf("%d", &A[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(A[i]> A[j])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
printf("The sorted array is\n");
for(i=0;i<n;i++)
{
printf("%d\t",A[i]);
}
}
Output
Enter the size of the array
5
Enter elements into the array
77 11 99 33 66
The sorted array is
11 33 66 77 99
-----------------------------------------------------------------------------------------------------
8. ADD TWO 1D ARRAYS
/*Matrix addition*/
#include<stdio.h>
void main()
{
int A[20][20],B[20][20],C[20][20],r1,r2,c1,c2,i,j;
printf("Enter no.of rows of first matrix\n");
scanf("%d",&r1);
printf("Enter no.of cols of first matrix\n");
scanf("%d",&c1);
printf("Enter no.of rows of second matrix\n");
scanf("%d",&r2);
printf("Enter no.of cols of second matrix\n");
scanf("%d",&c2);
if(r1==r2 && c1==c2)
{
printf("Enter elements into first matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("Enter elements into second matrix\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&B[i][j]);
}
}
printf("Resultant matrix is\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
C[i][j]=A[i][j]+B[i][j];
printf("%d \t",C[i][j]);
}
printf("\n");
}
}
else
printf("Matrix addition is not possible");
Output 1
Enter the size of array 1
4
Enter the size of array 2
5
Addition is not possible
Output 2
Enter the size of array 1
4
Enter the size of array 2
4
Enter elements into array 1:
1 2 3 4
Enter elements into array 2:
1 2 3 4
The resultant array is
2 4 6 8
-----------------------------------------------------------------------------------------------------
9. COUNT THE NO.OF OCCURRENCE OF A GIVEN NUMBER
/*Count the occurence of a given number*/
#include<stdio.h>
void main()
{
int A[20],i,n,key,count=0;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter elements into the array\n");
for(i=0;i<n;i++)
{
scanf("%d", &A[i]);
}
printf("Enter the elements to be searched\n");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(A[i]==key)
{
count++;
}
}
printf("No.of occurrence of %d = %d \n",key,count);
}
Output
Enter the size of the array
6
Enter elements into the array
1 2 5 2 8 2
Enter the elements to be searched
2
No.of occurrence of 2 = 3
-----------------------------------------------------------------------------------------------------
10. OCCURRENCE OF EACH ELEMENT IN AN ARRAY
/* C program to find the frequency of each element of array */
#include <stdio.h>
void main()
{
int arr[100], freq[100];
int size, i, j, count;
/* Input size of array */
printf("Enter size of array: \n");
scanf("%d", &size);
/* Input elements in array */
printf("Enter elements in array: \n");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
/* Initially initialize frequencies to -1 */
freq[i] = -1;
}
for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
/* If duplicate element is found */
if(arr[i]==arr[j])
{
count++;
/* Make sure not to count frequency of same element again
*/
freq[j] = 0;
}
}
/* If frequency of current element is not counted */
if(freq[i] != 0)
{
freq[i] = count;
}
}
/* Print frequency of each element */
printf("Frequency of all elements of array :\n");
for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}
}
Output
Enter size of array:
7
Enter elements in array:
1 2 3 1 2 2 3
Frequency of all elements of array :
1 occurs 2 times
2 occurs 3 times
3 occurs 2 times
-----------------------------------------------------------------------------------------------------
11. DELETE AN ELMENT FROM THE ARRAY
// C program to delete an element from the array
#include <stdio.h>
int main() {
int A[20],n,i,x,pos;
printf("Enter the size of array\n");
scanf("%d",&n);
printf("Enter the elements into array\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
printf("Enter the element to be deleted\n");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(A[i]==x)
{ pos=i; //get the position of element to be deleted
break;
}
}
for(i=pos;i<n;i++)
{
A[i]=A[i+1];
}
printf("The array after deleting %d is\n",x);
for(i=0;i<n-1;i++)
{
printf("%d\t",A[i]);
}
return 0;
}
Output
Enter the size of array
5
Enter the elements into array
1 2 3 4 5
Enter the element to be deleted
3
The array after deleting 3 is
1 2 4 5
-----------------------------------------------------------------------------------------------------
12. SECOND LARGEST
/*Second largest*/
#include<stdio.h>
void main()
{
int A[20],i,j,n,temp;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter elements into the array\n");
for(i=0;i<n;i++)
{
scanf("%d", &A[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(A[j]> A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
}
printf("The second largest element is %d\n", A[n-2]);
Output
Enter the size of the array
4
Enter elements into the array
1 5 2 8
The second largest element is 5
TWO DIMENSIONAL ARRAYS
1. MATRIX ADDITION
/*Matrix addition*/
#include<stdio.h>
void main()
{
int A[20][20],B[20][20],C[20][20],r1,r2,c1,c2,i,j;
printf("Enter no.of rows of first matrix\n");
scanf("%d",&r1);
printf("Enter no.of cols of first matrix\n");
scanf("%d",&c1);
printf("Enter no.of rows of second matrix\n");
scanf("%d",&r2);
printf("Enter no.of cols of second matrix\n");
scanf("%d",&c2);
if(r1==r2 && c1==c2)
{
printf("Enter elements into first matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("Enter elements into second matrix\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&B[i][j]);
}
}
printf("Resultant matrix is\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
C[i][j]=A[i][j]+B[i][j];
printf("%d \t",C[i][j]);
}
printf("\n");
}
}
else
printf("Matrix addition is not possible");
Output
Enter no.of rows of first matrix
2
Enter no.of cols of first matrix
2
Enter no.of rows of second matrix
2
Enter no.of cols of second matrix
2
Enter elements into first matrix
1
2
3
4
Enter elements into second matrix
1
2
3
4
Resultant matrix is
2 4
6 8
-----------------------------------------------------------------------------------------------------
2. TRANSPOSE OF A MATRIX
/*Transpose of a matrix*/
#include<stdio.h>
void main()
{
int A[20][20],r,c,i,j;
printf("Enter no.of rows of matrix\n");
scanf("%d",&r);
printf("Enter no.of cols of matrix\n");
scanf("%d",&c);
printf("Enter elements into first matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("The matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("The transpose of the matrix is\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d\t",A[j][i]);
}
printf("\n");
}
}
Output
Enter no.of rows of matrix
2
Enter no.of cols of matrix
3
Enter elements into first matrix
1 2 3 4 5 6
The matrix is
1 2 3
4 5 6
The transpose of the matrix is
1 4
2 5
3 6
-----------------------------------------------------------------------------------------------------
3. MATRIX MULTIPLICATION
/*C program to perform matrix multiplication*/
#include<stdio.h>
void main()
{
int r1,r2,c1,c2,m1[20][20],m2[20][20],mul[20][20];
printf("Enter number of rows for First Matrix:\n");
scanf("%d",&r1);
printf("Enter number of columns for First Matrix:\n");
scanf("%d",&c1);
printf("Enter number of rows for Second Matrix:\n");
scanf("%d",&r2);
printf("Enter number of columns for Second Matrix:\n");
scanf("%d",&c2);
if(c1!=r2)
{
printf("Matrices Can't be multiplied together");
}
else
{
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
mul[i][j]=0;
// Multiplying i’th row with j’th column
for(int k=0;k<c1;k++)
{
mul[i][j]+=m1[i][k]*m2[k][j];
}
}
}
printf("Multiplied matrix\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
}
Output
Enter number of rows for First Matrix:
2
Enter number of columns for First Matrix:
2
Enter number of rows for Second Matrix:
2
Enter number of columns for Second Matrix:
2
Enter first matrix elements
1 2 3 4
Enter Second matrix elements
1 2 3 4
Multiplied matrix
7 10
15 22
-----------------------------------------------------------------------------------------------------
4. ROW SUM & COLUMN SUM
/*Individual row and column sum of a matrix*/
#include<stdio.h>
void main()
{
int A[20][20],i,j,r1,c1,sum=0;
printf("Enter the row and col size of the matrix\n");
scanf("%d%d",&r1,&c1);
printf("Enter elements into the matrix\n");
for(i=0;i<r1;i++)
{for(j=0;j<c1;j++)
{
scanf("%d", &A[i][j]);
}
}
printf("The matrix is\n");
for(i=0;i<r1;i++)
{for(j=0;j<c1;j++)
{
printf("%d\t", A[i][j]);
}
printf("\n");
}
for(i=0;i<r1;i++)
{sum=0;
for(j=0;j<c1;j++)
{
sum=sum+A[i][j];
}
printf("Sum(row %d)=%d\n",i,sum);
}
for(i=0;i<r1;i++)
{sum=0;
for(j=0;j<c1;j++)
{
sum=sum+A[j][i];
}
printf("Sum(col %d)=%d\n",i,sum);
}
Output
Enter the row and col size of the matrix
2
2
Enter elements into the matrix
1 2 3 4
The matrix is
1 2
3 4
Sum(row 0)=3
Sum(row 1)=7
Sum(col 0)=4
Sum(col 1)=6
-----------------------------------------------------------------------------------------------------
5. DIAGIONAL SUM
/*sum of diagonal elements of a matrix*/
#include<stdio.h>
void main()
{
int A[20][20],r,c,i,j,sum=0;
printf("Enter no.of rows of matrix\n");
scanf("%d",&r);
printf("Enter no.of cols of matrix\n");
scanf("%d",&c);
printf("Enter elements into first matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&A[i][j]);
if(i==j)
sum=sum+A[i][j];
}
}
printf("The matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("Sum of diagonal elements= %d \n", sum);
}
Output
Enter no.of rows of matrix
3
Enter no.of cols of matrix
3
Enter elements into first matrix
1 2 3 4 5 6 7 8 9
The matrix is
1 2 3
4 5 6
7 8 9
Sum of diagonal elements= 15
-----------------------------------------------------------------------------------------------------
6. SUM OF OPPOSITE DIAGONAL ELEMENTS OF A MATRIX
/*opposite diagonal sum*/
#include<stdio.h>
void main()
{
int A[20][20],r,c,i,j,sum=0;
printf("Enter no.of rows of matrix\n");
scanf("%d",&r);
printf("Enter no.of cols of matrix\n");
scanf("%d",&c);
printf("Enter elements into first matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&A[i][j]);
}
}
for(i=0;i<r;i++)
{
sum = sum + A[i][r-i-1];
}
printf("The matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("Sum of opposite diagonal elements= %d \n", sum);
}
Output
Enter no.of rows of matrix
3
Enter no.of cols of matrix
3
Enter elements into first matrix
1 2 3 4 5 6 7 8 9
The matrix is
1 2 3
4 5 6
7 8 9
Sum of opposite diagonal elements= 15
-----------------------------------------------------------------------------------------------------
7. SYMMETRIC OR SKEW-SYMMETRIC CHECKING
/*Symmetric and skew symmetric checking*/
#include<stdio.h>
void main()
{
int A[20][20],r,c,i,j,symmetric=1,skew=1;
printf("Enter no.of rows and cols of matrix\n");
scanf("%d%d",&r, &c);
printf("Enter elements into matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("The matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("The transpose of the matrix is\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d\t",A[j][i]);
}
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(A[i][j]!= A[j][i])
{ symmetric=0;
}
else if (A[i][j]!= (-A[j][i]))
{ skew=0;
}
}
}
if(symmetric==1)printf("matrix is symmetric");
else if(skew==1)printf("matrix is skew-symmetric");
else printf("matrix is neither symmetric nor skew-symmetric");
}
Output1
Enter no.of rows and cols of matrix
3 3
Enter elements into matrix
1 1 1 1 1 1 1 1 1
The matrix is
1 1 1
1 1 1
1 1 1
The transpose of the matrix is
1 1 1
1 1 1
1 1 1
matrix is symmetric
Output 2
Enter no.of rows and cols of matrix
3 3
Enter elements into matrix
0 -5 4 5 0 -1 -4 1 0
The matrix is
0 -5 4
5 0 -1
-4 1 0
The transpose of the matrix is
0 5 -4
-5 0 1
4 -1 0
matrix is skew-symmetric