 Two - Dimensional Arrays
There could be situations where a table of values will have to be stored.
Consider a student table with marks in 3 subjects.
Student Mathematics Physics Chemistry
Student # 1 89 77 84
Student # 2 98 89 80
Student # 3 75 70 80
Student # 4 60 75 80
Student # 5 84 80 75

The above table contains a total of 15 values.

We can think this table as a matrix consisting of 5 rows & 3 columns.

Each row represents marks of student # 1 in all (different) subjects.

Each column represents the subject wise marks of all students.

In mathematics we represent a particular value in a matrix by using two subscripts such as Vij. Here V denotes the entire matrix Vij refers to the value in “ i ”th row and “ j ”th column.

Example :

In the above table V23 refers to the value “80”.

C allows us to define such tables of items by using two-dimensional arrays.

Definition :

A list of items can be given one variable name using two subscripts and such a variable is called a two – subscripted variable or a two – dimensional array.

Two – Dimensional arrays can be declared as.

Syntax :
<datatype> <variable-name>[row-size][column-size];

The above table can be defined in “C” as

The above table can be defined in “C” as

Representation of a two Dimensional array in memory : Initializing Two- Dimensional Arrays :

Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces.

int table  = {0,0,0,1,1,1};

This initializes the elements of first row to zero and the second row to one.

This initialization is done row by row.

The above statement can be equivalently written as

int table ={{0,0,0},{1,1,1}};

we can also initialize a two-dimensional array in the form of a matrix as shown.

int table = {
{0,0,0},
{1,1,1}
};

Commas are required after each brace that closes of a row, except in case of last row.

If the values are missing in an initialize, they are automatically set to zero.

Ex: int table   = {
{1,1},   1 1 0
{2}   2 0 0
};

Program : Write a program to display the elements of a two dimensional array.
`````` # include<stdio.h>
# include<conio.h>
main( ) {
int  i,j;
int  a = { { 1,2,3}, {4,5,6}, {7,8,9}};
printf(“elements of an array \n \n”);
for( i=0; i<3; i++)  {
for ( j=0; j<3; j++){
printf (“%d\t”, a[ i ][ j ]);
}  printf(“\n”);
}
getch();
}
``````
Output :

Elements of an Array
1   2   3
4   5   6
7   8   9

Program : To perform addition and subtraction of two matrices.
``````# include<stdio.h>
# include<conio.h>
main( )
{
int i,j,r1,c1, a, b;
clrscr( );
printf(“Enter Order of Matrix A & B up to 10 x 10:”);
scanf(“%d  %d”, &r1, &c1);
printf(“Enter Elements of Matrix of A: \n”);
for( i=0; i<r1; i++)
{
for( j=0; j<c1; j++)
scanf(“ %d ”, &a[ i ][ j ]);
}
printf(“Enter Elements of Matrix of B: \ n”);
for( i=0; i<r1; i++)
{
for( j=0; j<c1; j++)
scanf(“ %d ”, &b[ i ][ j ]);
}
printf(“\n Matrix Addition \n”);
for( i=0; i<r1; i++)
{
for( j=0; j<c1; j++)
printf(“%d\t”, a[ i ][ j ] + b[ i ][ j ]);
printf (“ \n”);
}
printf(“n Matrix Subtraction \n”);
for( i=0; i<r1; i++)
{
for( j=0; j<c1; j++)
printf(“%d\t”, a[ i ][ j ] – b[ i ][ j ]);
printf(“\n”);
}
getch( );
}
``````
Output :

Enter order of Matrix A & B up to 10 x 10: 3   3
Enter Elements of Matrix of A :
4   5   8   2   9   8   2   9   4
Enter Elements of Matrix of B :
1   3   5   0   5   4   6   7   2