In C programming it is also possible to have two or more than two dimension in array. Two dimensional array or 2D array is maximum use for matrix operation because you can operate it like a matrix. In two dimensional array we have rows and columns. The syntax of declaring two dimensional array is
Now if we write it in matrix format then it will looks like
25 36
55 98
This is a 2X2 matrix.
Now let’s write a program on two dimensional matrix
Now in programming it may happen you need only one row to print then just change the code for printing matrix in above example with
data_type array_name [row][column]
we can access the element in row wise or column wise. Now let see how rows and columns index help to store value in memory.| S[0][0] | S[0][1] | S[1][0] | S[1][1] |
|---|---|---|---|
| 25 | 36 | 55 | 98 |
25 36
55 98
This is a 2X2 matrix.
Now let’s write a program on two dimensional matrix
#include<stdio.h>
void main()
{
int a[3][2];
int i,j;
//creating 2D array
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("enter value for [%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("your matrix is\n");
//printing 2D array
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
In above program we use for loop to insert value in 2D array. Here variable “i” work as row index and “j” work as column index. As we declare the data type of array as an integer so we put value as integer. In this program we create a 3X2 matrix. Now let’s see the output of this programvoid main()
{
int a[3][2];
int i,j;
//creating 2D array
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("enter value for [%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("your matrix is\n");
//printing 2D array
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
enter value for [0][0] = 25
enter value for [0][1] = 45
enter value for [1][0] = 88
enter value for [1][1] = 32
enter value for [2][0] = 66
enter value for [2][1] = 78
your matrix is
25 45
88 32
66 78
As you can see in output, I arranger program in this way that it will looks like a matrix, if not then it will print all value in a single line. Here 25 stored in a[0][0] and 45 stored in a[0][1] and so on. By above program we understand a 2D array is nothing but a collection of data stored in memory as a matrix.enter value for [0][1] = 45
enter value for [1][0] = 88
enter value for [1][1] = 32
enter value for [2][0] = 66
enter value for [2][1] = 78
your matrix is
25 45
88 32
66 78
Now in programming it may happen you need only one row to print then just change the code for printing matrix in above example with
i=1;
for(j=0;j<2;j++)
{
printf("%d ",a[i][j]);
}
In this case only row number 2 will be printed that isfor(j=0;j<2;j++)
{
printf("%d ",a[i][j]);
}
88 32
Now if we want to print a single column then just change the code for printing matrix in above example with
j=1;
for(i=0;i<3;i++)
{
printf("%d ",a[i][j]);
}
In this case only column number 2 will be printed that isfor(i=0;i<3;i++)
{
printf("%d ",a[i][j]);
}
45 32 78
In this way we can print a single value from matrix.


