NIOS Computer Science: Chapter 11 – Array Computer Part 6
Download PDF of This Page (Size: 155K) ↧
Two Dimensional Array
It has two subscript or index, first for row and second for column. For example:
int
The above has five rows and four columns. The total numbers of elements are 20. The subscripts of 20 elements are:
Initialization of Two Dimensional Array
The initialization is done at the time of declaration of an array.
For example:
int A [2] [4] = {1, 2, 3, 4, 5, 6, 7, 8);
For more clarity
int A [2] [4] = { {1, 2, 3, 4 }, {5, 6, 7, 8} };
The above data can be grouped. The inner braces are ignored by the compiler.
For two dimensional array, two loops are required. The following program finds out the maximum value stored in two dimensional array.
# include , iostream.h >
const int M = 5;
void main ( )
{
int A [M] [M], i, j, T;
cout >> “\n Enter Array Elements”;
for (i = 0; i < = M - 1; i ++ )
for (j = 0; j < = M - 1; j ++ )
cin >> A [i] [j];
T = A [0] [0];
for ( i = 0; i < = M - 1; i + + )
for (j = 0; j < = M - 1; j + + )
{
If (T < A [ i ] [ j ]
T = A [ i ] [ j ];
}
cout << “Largest value” << T << “\n”
}