2D Array
2D Array
2D Array
h>
main()
{
int A[20][20], m,n, i,j;
printf("\nEnter the number of rows and columns: ");
scanf("%d%d",&m,&n);
for(i=0;i<m; i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter element for %d row and %d column : ", i,j);
scanf("%d", &A[i][j]);
}
}
printf("\n The elements enetered are");
printf("\n*******************\n");
for(i=0;i<m; i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t", A[i][j]);
}
}
2. Sum of row elements : Program #include <stdio.h>
int main()
{
int A[20][20], m,n, i,j, sum;
printf("\nEnter the number of rows and columns: ");
scanf("%d%d",&m,&n);
for(i=0;i<m; i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter element for %d row and %d column : ", i,j);
scanf("%d", &A[i][j]);
}
}
for(i=0;i<m; i++)
{
sum =0;
for(j=0;j<n;j++)
{
sum+=A[i][j];
}
printf("\n sum of %d row is %d\t", (i+1), sum);
}
}
if(m==n)
{
for(i=0;i<m; i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
sum_main+=A[i][j];
}
}
}
for(i=0;i<m; i++)
{
for(j=0;j<n;j++)
{
if((i+j)==(m-1))
{
sum_alternate+=A[i][j];
}
}
} }
printf("\n The sum of main diagonal is : %d", sum_main);
printf("\n The sum of alternate diagonal is : %d", sum_alternate);
if(m==n)
{
for(i=0;i<m; i++)
{
for(j=0;j<n;j++)
{
if(i<j)
{
sum_upper+=A[i][j];
}
else if(i>j)
{
sum_lower+=A[i][j];
}
}
}
}
else
{ printf("\n Enter same number of rows and columns");}
}
for(i=0;i<n; i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",B[i][j]);
}
}
#include <stdio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,k,r,s, m,n;
printf("Enter the no of rows and columns of the first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the no of rows and columns of the second matrix\n");
scanf("%d%d",&r,&s);
if(n!=r)
printf("\n The matrix cannot multiplied");
else
{
printf("\n Enter the elements of first matrix ");
for(i= 0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);
}
printf("\n Enter the elements of second matrix ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&b[i][j]);
}
printf("\n The element of first matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\n The element of second matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n Multiplication of two matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",c[i][j]);
}
}