Array Computer Solutions: Two-Dimensional Array: Initialization of Two-Dimensional Array

Glide to success with Doorsteptutor material for UGC : Get detailed illustrated notes covering entire syllabus: point-by-point for high retention.

Download PDF of This Page (Size: 111K)

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”

}

Developed by: