Multi-dimensional arrays are those
which have more than one dimensions. Multi-dimensional arrays are defined in
much the same manner as one dimensional array, except that a separate pair of
square brackets is required for each subscript. Thus, two dimensional arrays
will require two pairs of square brackets; three dimensional arrays will
require three pairs of square brackets and so on. The two dimensional array is
also called matrix. Multi-dimensional array can be defined as following
storage_class data_type array_name[expression1] [expression2] ...[expression
n];
Here, expression1,
expression2…..expression n are positive valued integer expressions that
indicate the number of array elements associated with each subscript. An m*n
two dimensional array can be thought as tables of values having m rows and n
columns.
An example
int X[3][3] can be shown as follows
|
|
|
|
Row 1
|
X[0][0]
|
X[0][1]
|
X[0][2]
|
Row 2
|
X[1][0]
|
X[1][1]
|
X[1][2]
|
Row 3
|
X[2][0]
|
X[2][1]
|
X[2][2]
|
Initialization of
multi-dimensional array:
Like one
dimensional array, multi-dimensional arrays can be initialized at the time of
array definition or declaration. A few example of 2-D array initialization are:
|
All above examples are valid. While initializing an array, it is
necessary to mention the second dimension (column) whereas the first dimension
(row) is optional.
Thus, following are invalid
int marks3[3][]={ 2,4,6,8,
10, 12};
int marks4[][]={ 2,4,6,8,
10}; /* error!!!*/
Example: Write a program to read two 3*3 matrixes and display their sum.
|
Comments
Post a Comment