2D Array
2D Array
2D Array
2D array consists of element of same type arranged in rows & columns. It has two subscripts (indices).
Row major order: firstly all elements in 0th row are accessed, then all elements in 1st row areaccessed &
so on.
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
Column major order: firstly all elements in 0th column are accessed, then all elements in 1stcolumn are
accessed & so on.
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
scanf(“%d”,&a[i][j]);
}
}
#include<stdio.h>
int main()
{
int i,j ,m, n, a[20][20],b[10][10];
printf(“enter the number of rows & columns \n”);
scanf(“%d %d”,&m,&n);
printf(“enter array elements\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
b[i][j]=a[i][j];
}
}
printf(“transpose of the matrix”);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf(“%d\t”,b[i][j]);
}
}
return 0;
}
2. Sum
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
scanf("%d", &first[c][d]);
}
}
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
{
for (d = 0 ; d < n; d++)
{
scanf("%d", &second[c][d]);
}
}
for (c = 0; c < m; c++)
{
for (d = 0 ; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
}
printf("\n");
}
printf("Sum of entered matrices:");
for (c = 0; c < m; c++)
{
for (d = 0 ; d < n; d++)
{
scanf("%d", &second[c][d]);
}
}
return 0;
}
3. Difference
4. Product
}
printf("the product of two matrices is\n") ;
for(i=0 ;i<m ;i++)
{
for(j=0 ;j<q ;j++)
{
printf("%d\t",c[i][j]) ;
}
printf("\n")
}
return 0;
}