C++ multidimensional arrays:2D , 3D and 10D array

In this post we shall discuss what is multidimensional array? or an array we like to call it specifically as 2D(2 dimensional array),3D(3 dimensional array) or even 10D(10 dimensional array).Before you learn what is multidimensonal array you should be accustomed with linear array or the simple array.A detail discussion of linear array is done in the two links given below.

Link : C++ array myths and conception
 
Link Accessing array


In linear array everything is straightforward,all the data are store in sequential manner and to access them we uses different index value.In linear array since the data are store linearly to access them we simply traversed forward or backward.To traverse forward we increase the index value and to go backward we decrease the index value.Consider an int type array that can hold 5 integer value,

 int arr[5]={2,3,54,67,8}; 

The graphical representation of this array is shown below.



 

If we consider a linear array as a linearly aligned storage,then in C++ by multidimensional array we mean an array that have a storage that is also aligned in the downward position.So a multidimensional array is an array of arrays.The graphical representation below may help you understand better.

To use a multidimensional array in our program we will add the subscript([]) according to the number of requirements.Say for two dimensional array we will add it twice and thrice for three dimensional array and so on.This means a two dimensional array will have two index value.An int type 2 dimensional array is given below.

int arrMul[3][5] ; 

In a two dimensional array we usually refer to each storage/value in terms of number of row or column.
 
The first index value will represent the number of rows which mean the arrMul will have 3 rows.
 
The second index value will represent the number of columns so arrMul will have 5 columns.A picture shown below represents how rows and columns of arrMul array is classified.


If we look at the picture the rows and columns arrangement becomes crystal clear.Whenever you use a multidimensional array it is a good habit to draw out the rows and columns first.The picture will give you a clear view of how you are going to deal with the array.In the next topic we will see how to initialize a multidimensional array.


Initializing multidimensional array

To initialize each element of multidimensional array we can use the normal form of initialization which is use also for the linear array.

/*** Linear array initialization ***/
int arr[5]={ 2 , 4 , 55 , 67 , 8668 };

/*** 2D array initialization ***/
int arrMul[3][5]={ 12 , 21 , 454 , 45 , 0 , 9 ,89 , 67 ,78 , 899, 9129, 0 , 1 , 2 , 90 }

For ‘arrMul’ 15 values is given inside the braces .
 
The first five values: 12 , 21 , 454 , 45 , 0 will be initialized to the first row array elements.For the first row the index value is 0 so the first row array elements are arrMul[0][0]=12 , arrMul[0][1]=21 ,arrMul[0][2]=454 ,arrMul[0][3]=45, arrMul[0][4]=0 ,note here only the second index value is changed.
 
The next five values is assigned to the second row array elements ,i.e. arrMul[1][0]=9 , arrMul[1][1]=89 ,arrMul[1][2]=67 ,arrMul[1][3]=78, arrMul[1][4]=899.
 
The last five elements is assigned to the third row array elements and so on.


Initializing multidimensional array using initializer list

There is another way to initialize the multidimensional array using the braces syntax.But in this method we separate each row initializers again with a braces which is also known as initializer list.The syntax is shown below.

int arrMul[3][5]={ {12 , 21 , 454 , 45 , 0} , //first row initializers
  { 9 ,89 , 67 ,78 , 899} , //Second row initializers
  { 9129, 0 , 1 , 2 , 90 } } ; //Third row initializers

I am sure this syntax of initializing the array is more organize and easily specifiable as which initializer is assigned to which array element.You should prefer this method.


Initializing all elements with 0

Sometimes it happens that you want to initialize all the multidimensional array elements with 0.Then in such case you simply give the value inside the braces.

int arrMul[3][5]={ 0 } ; //Third row initializers

Now all the array elements are assigned 0.

Note also when initializing a multidimensional array if you provide an initializers less than the number of array elements(calculated as rows*columns) then the elements for which the initializers is not provided is assigned 0.And if the type is string type then empty string is assigned to the array elements.Consider the code below.

int arrMul[3][5]={ 12 , 21 , 454 , 45 , 0 } ;

string arrMulSt[2][2]={“New” , “Old” };

For arrMul only the first row array elements are assigned with the value provided the second and third row array elements are assigned 0.And for arrMulSt the second row elements is assigned “”(an empty string).


Accessing the multidimensional array

Accessing the multidimensional array elements is quite simple.To access the specific element in an array,say you want to access the element in second row and third column.Then you simply provide the first index as the row number and the second index as the column number.Consider the code below.

string st[][3]={ { “New” , “Old” , “young” } ,
  { “Happy” ,”Sad”, “Angry” },
  { “Love” , “Hatred” , “Jealousy” } };

cout<< st[1][2] << endl //prints 'Angry'
  << st[2][0] ;//prints 'Love' 

If you noticed in the above array ‘st’,only the second index value is mentioned(the number of columns).The compiler allow such declaration because the compiler can guess the number of rows.However,you cannot mentioned the row number and also leave the column index blank.In this case the compiler will complain.

char arC[2][]; //error

Accessing all the array elements

If you want to access all the elements of the array you can use the for() statement.Here you will be needing two for() statements ,the first one to count the number of rows and the second to count the number of columns.A code example is shown below.

string st[][]={ { “New” , “Old” , “young” } ,
{ “Happy” ,”Sad”, “Angry” },
{ “Love” , “Hatred” , “Jealousy” } };

int row=3,col=3 ;

for(auto rowCount=0; i<row ; i++ )
{
 for(auto colCount=0; ii< col ; ii++ )
 {
  cout<< st[rowCount][colCount] << ” ” ;
 }
}

Output,
 
New Old young Happy Sad Angry Love Hatred Jealousy


Multidimensional array with more than two indexes,3d,10d,100D

The multidimensional array with two index is considered as an array of arrays.If the multidimensional array have more then two indexes say three indexes,10 indexes,then how do we interpret it?

We call an array having two indexes as an array of arrays due to the fact that if we keep the first index value(the row) constant then it turns out that the array obtain is a linear array .This means if the row value say 0 is kept unchanged then to access the value we only change the column value which is in fact same as accessing a linear array.This same behavior is exhibited for the second row and so on.So we call two index array as an array of-linear-arrays.

In a three index array if the first index is kept unchanged we will be varying only the second and third index value to access the elements,which in fact is same as accessing the two index multidimensional array.So we can call three index multidimensional array as an array of two index multidimensional array.The same interpretation can be also made for a multidimensional array having four or more indexes.

A program below shows three index multidimensional array.

int ar[2][3][4]={0} ;

int arr[1][1][1][1][1][1][1][1][1][1]={0} ; //10 dimensional array

cout<< ar[1][2][1] << endl
<< arr[0][0][0][0][0][0][0][0][0][0] ;


Output
 
0
0

You can create a multidimensional array having as many index as you desire.