Multidimensional arrays:2D , 3D and 10D array


Multidimensional array in C++

An array is a way to store values of same type in a consecutive manner.This is justified by the difference of each array element address equal to the size of the type.To access an element in an array we simply specify the index value and we get the value store in that index position.If you are imaginative-which is necessary for learning multidimensional array- you will see array simply as many containers put together linearly and to access the value in each container/array we either traverse 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.
array
Fig array storage arrangement

If we consider 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.

Multidimensional array
Fig A multidimensional,2 dimensional array

To use a multidimensional array in our program we will add the subscript-square bracket- according to the number of requirements.We will add it twice for two dimensional array and thrice for three dimensional array and so on.So 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 in terms of number of row or column.The first index value will represent the number of rows so,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.

2 dimensional array rows and columns
Fig rows and columns of 2 dimensional array
 
If we look at the picture the rows and clumns arrangment becomes crystal clear.So 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 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 is assigned 0.

Note also when initializing a multidimensional array if you provide an initializers less than the number of array elements -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 “”.


 


Stop wasting time,earn money($$$) from your website-Join Now!

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 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 ;every body needs Love

In the above array st,only the second index value is mentioned-the number of columns-and the compiler allow such declaration because the compiler can guess the number of rows.However,you cannot mentioned the row number but 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
 





Pointers and multidimensional array

The relation between a pointer and a linear array is such that if the array’s first address is assigned to a pointer,the pointer can access all the array elements either using the array syntax or the pointer form.Consider the code below.

int ar[]={2,3,45,4};
int *ipt=arr ;

cout<< ipt[0] << *(ipt+2) ; //work fine

In case of multidimensional array simply assigning the array’s first address to a pointer is just plain wrong.

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

int *ipt=arrMul; ///error

Since a multidimensional array is an array of arrays,if we want the pointer to point to it we have to specify that the pointer which it is pointing to is a multidimensional array.We can do this by using the syntax (*pointer)[column number] .A code example is shown below.

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

int (*ipt)[5] = arrMul; ///Work fine

cout<< ipt[2][4] ;//prints 90

Output

90

Array of pointers and multidimensional array pointer

Sometimes people get confuse over the two syntax shown below.

int *ip[4] ;

int (*pt)[4] ;

The first syntax means an array of pointers and the second pointer is a pointer to a multidimensional array having 4 columns. An array of pointers means each element of the array,say ip[0] is simply a pointer that can point to another int variable or int array .And the element ip[1] is another pointer that can point to some other variable.Note here ip[0] and ip[1] have no relation at all.

Whereas in case of pointer ‘pt’ the pt[0] and pt[1] element will point to the first and second row of the same multidimensional array.Which means each element of pt is bound to point to some elements of the same multidimensional array.

/*Array of pointers example*/
int *ip[4] ;

int arr[]={3,34,545,45} ,
 value=9082;

ip[0]=arr ; //ip[0] points to arr array

ip[1]=&value ; //ip[1] points to value variable

cout<< ip[0][3] << endl ///prints 45
<< *ip[1] ; ///prints 9082

/*multidimensional array pointer example*/
int (*pt)[4] ;

pt=arr; ///error,can only point to multidimensional array
pt[0]=arr ; ///error

int arMul[2][4]={0};

pt=arMul ; ///work fine

 


 


Stop wasting time,earn money($$$) from your website-Join Now!

Multidimensional array with more than two indexes

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,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 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.


Related Link

->Passing and returning array and multidimensional array from a function.

-> C and C++ Using array :myths and conception.

->Relation between array and pointers.