How to Initialize Multi-dimensional Arrays in C++

An array having more than one subscripts is called multidimensional array. A multidimensional array is an “array of arrays”. It is not limited to two indices i.e.. two dimensional, but can also have three-dimensional and four-dimensional etc.

Concept of two dimensional arrays:

The most communally used multidimensional array is two dimensional array which is also called bi-dimensional array. It is used for storing of tabular arranged in rows and columns. It can be represented by two indices i.e., row index and column index.
 
All the elements of two dimensional array are of the same data type. Consider the following array:
 
int X[3][5];
 
Array X consisting of three rows and five columns.
 It can be represented as:
 
How to Initialize Multi-dimensional Arrays in C++

 

 
Two dimensional array is also called matrix in mathematical and table in business applications.
 

Definition and initialization of two dimensional arrays.

Like one-dimensional array, a two-dimensional is defined and initialized before using it in a program.
 
The following sections describes its definition and initialization with the help of examples
 

Defining two Dimensional Array

The general syntax for the declaration of two dimensional array is given below.
 
Data Type Array name [row index] [column index];
 
In the above syntax, the data type is any valid data type, such as int, float, long int etc, which is followed by array name and two indices.
 
The first index is for number of rows and the second index is for number of columns in the array.
 
A float type array, tdArray, consisting of 2 rows and 2 columns can be defined by using the following statement.
float tdArray[2][2];


Read: C++ Arrays (Complete Guide)

Initializing two Dimensional Arrays.

To initialize a tow dimensional array, it is easiest to use nested braces, with each set of numbers representing a row;
 
//two dimensional array initialization
int anArray[3][5]=
{
{1,2,3,4,5}, // row 0
{6,7,8,9,10}, //row 1
{11,12,13,14,15}// row 2
};
 
If we want to initialize a two-dimensional array to 0 then it can be done as follows:
 
int anArray[3][5]={0};
 
Accessing and writing at an index in a two dimensional array.
To operate on the elements of two dimensional array, the elements should be first accessed and then updated.
 
Writing at an index of an array is also when a new two dimensional array is declared.
 
Accessing and writing at an index of a two dimensional array is similar to one dimensional array.
 
The following section discuss them with the help of examples.
 

Accessing two Dimensional Arrays.

Accessing two dimensional arrays need two indices, one for the row and the other for the column. For example, if we have an array anArry[3][5] and want to access an element at second row and third column. 
Then it can be written as:
 
anArray[1][2];
 
Accessing elements of a two dimensional array individually is very difficult and time consuming. A better solution to this is the application of loop.
 
Normally, nested loops are used for this purpose: outer loop for the row, and inner for the columns. Since, two dimensional array are typically accessed by row, therefore, the row index is used as the outer loop an the column index is used as the inner loop.
 
consider the following program used to access the element of the array. anArray[3][5], initialized in the above section and display them on the screen in a matrix form.
 
//accessing two dimensional array using loop
#include<iostream.h>
#include<conio.h>
int main()
{
in anArray[3][5]=
{{1,2,3,4,5}’
{6,7,8,9,10},
{11,12,13,14,15}};
for(int row=0; col<3; ++row)
{
for(int col=0; col<5;++col)
cout<<anArray[row][col]<<“,”;
cout<<endl;
}
getch();
return 0;
}
 
Output of the program
 
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15

Writing at an Index of two Dimensional array.

Loop is a better way of writing to the indices of a two dimensional array. 
 
Consider the following example:
 
//writing in two dimensional array using loop
#include<iostream.h>
#include<conio.h>
int main()
{
int A[2][3];
cout<<“Kindly enter values for the array”<<endl;
for(int i=0; i<2; i++)
for(int j=0; j<3; j++)
cin>>a[i][j]; //writing to the array
cout<<“Values of the array are:”<<endl;
for(int i=0; i<2; i++)
cout<<A[i][j]<<“t”; // accessing of the array
cout<<endl;
}
getch();
return 0;
}
 
Output of the program
 
2 -17 10 10
values of the array are:
2 -17
10 10