C Programs Module 2 Arrays
C Programs Module 2 Arrays
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
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
-----------------------------------------------------------------------------------------------------
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
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;
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
/*Bubble sort*/
#include<stdio.h>
void main()
{
int A[20],i,j,n,temp;
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
/*Selection sort*/
#include<stdio.h>
void main()
{
int A[20],i,j,n,temp;
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
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
-----------------------------------------------------------------------------------------------------
for(i=0;i<n;i++)
{
if(A[i]==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
-----------------------------------------------------------------------------------------------------
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
-----------------------------------------------------------------------------------------------------
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;
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
-----------------------------------------------------------------------------------------------------
3. MATRIX MULTIPLICATION
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
-----------------------------------------------------------------------------------------------------
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
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
-----------------------------------------------------------------------------------------------------
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
-----------------------------------------------------------------------------------------------------
Output1
Output 2