Memory map of the two dimensional Arrays
We are assuming that the elements of a 2 dimensional array display in rectangular form. But in memory they are not stored in this particular format. They are stored in a contiguous memory location as shown below.
int arr[3][2];
row,col
|
arr[0][0]
|
arr[0][1]
|
arr[1][0]
|
arr[1][1]
|
arr[2][0]
|
arr[2][1]
|
Value
|
4
|
6
|
6
|
1
|
7
|
4
|
Address
|
3002
|
3004
|
3006
|
3008
|
3010
|
3012
|
Program to input and print a matrix.
main()
{
int mat[3][3],i,j,row,col;
printf("Enter the row of matrix");
scanf("%d",&row);
printf("Enter the column of matrix");
scanf("%d",&col);
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("Enter the number for row %d, column %d :",i+1,,j+1);
scanf("%d",&mat[i][j]);
}
}
printf("matrix is :\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
}