Multidimensional arrays in c plus plus

Multidimensional arrays

The arrays discussed so far represent only one dimension. i.e only a set of values. Often, we may require the tables of values having rows and columns or values stored in different dimensions. This gives rise to “multidimensional arrays”. Most commonly used multidimensional arrays are 2 dimensional arrays. We can have arrays of higher dimensions also.
For e.g., 2 dimensional arrays can be used for all kinds of tables, e.g. a spreadsheet as shown in the Fig bellow.

week
1
2
3
4
5
1
12
8
10
9
23
2
9
11
12
4
18
3
13
9
8
7
19
4
8
4
6
6
13
5
3
2
7
7
15
6
9
11
13
10
19
7
0
0
0
0
0
Representing sales performed by the salesperson during the week in 5 different locations.

Definition and initialization of multi-dimensional arrays

In C++ two dimensional arrays are defined using the [ ] operator to specify each dimension. For e.g.,
Char screen[25][80];
Double datatable [7][11];
The first [] defines the number of rows; the second[] defines the number of columns. Therefore, screen[ ] [] is a two dimensional array of characters that could be used to record the information displayed in each character position of a terminal screen that had 25 rows each with 80 columns. Similarly, data table [] [] is an array with 7 rows each of 11 columns.

As in the case of one dimensional array, here also, the array indicates start at zero. So, the top left corner of the screen [] [] array is screen [0][0] and its bottom right corner is screen[24][79].
Two dimensional arrays can also be initialized. While initializing, the data for the first row has to be supplied (one value for each column), followed by all the data for row1, and so on. For e.g.,
Double mydata [4][5]={
{1.0, 0.54, 0.21, 0.11, 0.03},
{0.34, 1.04, 0.52, 0.16, 0.09}
{0.41, 0.02, 0.30, 0.49, 0.19}
{0.01, 0.68, 0.72, 0.66, 0.17}
};
You don’t have to put in the internal {begin and} end brackets around the data for each individual row. The compiler will understand exactly what you mean if you initialize as:
Double mydata[4][5]={ 1.0, 0.54, 0.21, 0.11, 0.03, 0.34, 1.04, 0.52, 0.16, 0.09, 0.41, 0.02, 0.30, 0.49, 0.19, 0.01, 0.68, 0.72, 0.66, 0.17};
With one dimensional arrays, it is possible to define an initialized array and leave it to the compiler to work out the size (e.g. double vec[] = { 2.0, 1.5, 0.5, 1.0};
But the same does not work with two dimensional arrays. For e.g.,
Double guess[ ] [ ] = ]={ 1.0, 0.54, 0.21, 0.11, 0.03, 0.34, 1.04, 0.52, 0.16, 0.09, 0.41, 0.02, 0.30, 0.49, 0.19, 0.01, 0.68, 0.72, 0.66, 0.17};

This may be interpreted as a 4×5 array, or a 5×4 array, or a 2×10 array. The compiler cannot guess, hence it is illegal. You must at least specify the number of columns you want in each row, and can leave it to the compiler to work out how many rows are needed. The following definition of an initialized array is valid.

Double mydata[] [5] = ]={
{1.0, 0.54, 0.21, 0.11, 0.03},
{0.34, 1.04, 0.52, 0.16, 0.09}
{0.41, 0.02, 0.30, 0.49, 0.19}
{0.01, 0.68, 0.72, 0.66, 0.17}
};
The compiler determines that mydata[] [5] must have four rows.

Accessing individual elements

Individual data elements in a two dimensional arrays are accessed by using two [] operators; the first [] operator picks the row, the second [] operator picks the column. For e.g. , to access the 4th row and 2nd column of the matrix A [5][6], the following C++ statement can be given:
X=A [3][1] // element in 4th  row and 2nd column is assigned to X.

Leave a Reply

Your email address will not be published.