Two dimensional array in c

Two Dimensional (2-D) Array in C

After know about One dimensional array, The syntax of declaration of 2-D array are similar to the one dimensional Array but in two dimensional array there are two subscript firstly see the syntax of Two dimensional (2-D) array.

data_type array_name[rowsize][column size];

In this example row size specifies the number of rows and the column size specifies the number of column in the array. The total number of elements in the array are rowsize * columsize. take the example declare the 2-d Array

int array[3][3];

Here array is the two dimensional (2-D)Array with 3 rows and 3 columns. here first subscript denotes the row and the second script denotes the columns. The array always start from [0] index, so starting element of Array is arr[0][0] and last element is arr[3][3]. The Total number of elements in this array is 3*3=9

col 0 col 1 col 2 col 3
row 0 arr[0][0] arr[0][1] arr[0][2] arr[0][3]
row 1 arr[1][0] arr[1][1] arr[1][2] arr[1][3]
row 2 arr[2][0] arr[2][1] arr[2][2] arr[2][3]
row 3 arr[3][0] arr[3][1] arr[3][2] arr[3][3]

Processing 2-d Array

for processing 2-D array, we use two nested for loops. The outer for loop used for row and the inner for loop use for the column

.

int array[3][4];

Reading value in arr

for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf(“%d”, &arr[i][j]);
}
}

print value in arr

for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(“%d”,arr[i][j]);
}
}

/* Input and Display the 3 * 3 matrix */
#include< stdio.h >
#define
ROW 3
#define COL 3
int main()
{
int mat[ROW][Col],i,j;
printf(“Enter The elements in 3*3 matrix”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&mat[i][j]);
}
}
printf(“\n The Matrix you are entered are”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d”,mat[i][j]);
}
}
return 0;
}

Initialization of 2-D array

2-D array can be initialize as similar to that of 1-D arrays for example

int mat[3][3]={11,22,33,44,33,45,67,43,22};

These values are assign to the mat 2-D array row and column wise , After initialization values are assign to elements like that

mat[0][0]:11 mat[0][1]:22 mat[0][2]:33
mat[1][0]:44 mat[1][1]:33 mat[1][2]:45
mat[2][0]:67 mat[2][1]:43 mat[2][2]:22