C – Two Dimensional Array 2017-08-05T20:47:49+00:00

# 2 Dimensional Array :

• Two-dimensional array are those type of array, which has finite number of rows and finite number of columns. The declaration form of 2-dimensional array is

Data_type    Array_name [row size][column size];

• The type may be any valid type supported by C.
• The rule for giving the array name is same as the ordinary variable.
• The row size and column size should be an individual constant.

The following declares a two-dimensional 3 by 3 array of integers and sets the first and last elements to be 10.

int matrix ;
matrix = 10;
matrix = 10;

The following Figure illustrates a two dimensional array, matrix. The array contains three rows and tree columns, so it is said to be a 3-by-3 array. In general, an array with m rows and n columns is called an m-by-n array.

     10   10

Every element in array matrix is identified by an element name of the form matrix[ i ][ j ]; matrix is the name of the array, and i and j are the subscripts that uniquely identify each element in matrix . Notice that the names of the elements in the first row all have a first subscript of 0; the names of the elements in the third column all have a second subscript of 2.

In the case of Two-dimensional array, during declaration the maximum number of rows and maximum number of column should be specified for processing all array elements.

The implementation of the array stores all the elements in a single contiguous block of memory. The other possible implementation would be a combination of several distinct one-dimensional arrays. That’s not how C does it. In memory, the array is arranged with the elements of the rightmost index next to each other. In other words, matrix comes right before matrix in memory.

The following array:

     1 2 3  4 5 6  7 8 9

would be stored:

 1 2 3 4 5 6 7 8 9

C – 2D ARRAY EXAMPLE PROGRAM :

Output :

 Enter the order of matrix 2 2 Enter elements of 2*2 matrix 1 2 3 4 Given matrix : 1           2 3               4 -29368