Declaring Two-Dimensional Array
Declaring a two-dimensional array is similar to declaring a one-dimensional array, but there is a small difference.
This is a t-by-12 array (5 rows, 12 columns). In general, an array with m rows and n columns is referred to as m-by-n array. But the arrangement in this figure is only conceptually true, because memory doesn’t actually contain rows and columns. The array elements are always stored in one continuous chain.
However, the program written should contain the logic of splitting this continuous chain of values into rows and columns.
You can initialize a two-dimensional array in the same way as you did for one-dimensional arrays.
Here is how:
long salary[5][12] = {
{1995, 123400, 123300….145680, 135780},
{1996, 133400, 133400, 133300….145680, 145780},
{1997, 143400, 143300…..144680, 145780},
{1998, 153400, 153300….155680,145780),
{1999, 164400, 165300, ….165680, 145780}
}
Printing Data in a Two-Dimensional Array
All the elements in a two-dimensional array are stored in a contiguous chain. The elements should be split into 5 rows and 12 columns to print the data in the form of a table.
The program to divide these elements into rows and columns and print them in the form of a table is quite similar to the process of storing the elements, but with a small change. Observe the code given, to find out what it is.
for(i=0; i<5; i++)
{
for(j=0; j<12; j++)
{
printf(“%d”, salary[i][j]);
}
printf(“\n”);
}
Matrices – Applying two-dimensional arrays
This is a 3x3 matrix (3 rows x 3 columns). In mathematics, you refer to an element of a matrix using double subscripts. Say, the name of the above matrix is 'S', the notation Sij would refer to the element in the ith row, jth column.
C program also uses the same notation. However, it places the subscripts in square brackets. For example: S[1][1] and S[1][2] would refer to 10 and 15 respectively. Remember, the first index number refers to the row and the second to the column.