Multidimensional Arrays In C++

Role Of Multidimensional Array In C++ With Examples.

Until now, in our earlier tutorials, we have seen all about one-dimensional arrays.

C++ also supports arrays with more than one dimension. These are called multi-dimensional arrays. Multidimensional arrays are usually arranged in tabular form i.e. in row-major order.

=> Look For The Entire C++ Training Series Here.

Multidimensional Array in c++

Multidimensional Arrays In C++

Consider a multidimensional array of dimensions 3×2 i.e. 3 rows and 2 columns.

We represent this array as follows:

R1c1R2c2
R2c1R2c2
R3c1R3c2

As shown in the above representation, each cell E.g. R1C1 will hold the contents of the array.

The number of elements present in a multidimensional array is the product of its dimensions. This means that if the dimensions of an array are 3×2 then the number of elements in that array is the product of 3 and 2 i.e. 6. Similarly, if the dimensions of the multidimensional array are [10][20][10] then the number of elements in that array are 10*20*10 = 2000.

A two-dimensional array is the simplest form of the multidimensional array used by C++.

Declaring An Array

General declaration of the multidimensional array in C++ is shown below:

dataType arrayName [size1][size2]….[sizen];

Here, dataType is the data type of the array. The datatype should be supported by C++.

arrayName is the name of the multidimensional array.

Size1, size2….sizen are the sizes of each of the array dimensions.

For Example, let's declare an array of size 3×2 i.e. a two-dimensional array, myarray_2d.

int myarray_2d [3][2];

A two-dimensional array is represented in the form of rows and columns.

So the above declaration of the array can be represented as follows:

myarray_2d[0][0]myarray_2d[0][1]
myarray_2d[1][0]myarray_2d[1][1]
myarray_2d[2][0]myarray_2d[2][1]

As shown in the above representation, each element of a 2-dimensional array is accessed as myarray_2d[i][j] where i is the row number and it ranges from 0 to n-1 where n is the dimension size and j is the column number and it ranges from 0 to m-1 where m is the dimension size of the column.

Initializing Multidimensional Arrays

We can initialize multi-dimensional arrays similar to the one-dimensional arrays. Multidimensional arrays are initialized row-wise.

There are two ways through which we can initialize multi-dimensional arrays.

#1) Consider an array with dimensions [3][2] named myarray:

int myarray[3][2] = {1, 2,3,4,5,6};

Here, initialization of the array is from left to right in a row by row manner. As the dimensions are [3][2], the first two elements will form the first row and so on.

Pictorial representation of this initialization will look as shown below:

12
34
56

#2) The same array shown above can be initialized using a different way as shown below:

int myarray[3][2] = { {1,2}, {3,4}, {5,6} };

Here, initialization is done from the left to right and each element corresponds to one row. As there are three rows for this array, we have three inner elements enclosed in curly braces ({}).

This way of initialization is more readable and is advantageous when the array dimensions grow.

Accessing Multi-dimensional Arrays

Multidimensional array elements are accessed using the row index and column index.

Let’s see an example of a two-dimensional array with dimensions [3][3]. Below is the code to initialize it.

int newarray [3][3] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

If I want to access the second element in the first row and assign it to an integer variable, then the line of code will be:

int val_2d = newarray[0][1];

Below is a sample program that shows declaration, initialization and accessing of a two-dimensional array.

#include <iostream>
using namespace std;
 
int main() {
 
   int myarray[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
 
   for(int i=0;i <3;i++)
   {
     for(int j=0;j<3;j++)
       {
         cout<<myarray[i][j]<<" ";
       }
 }
return 0;
 }

This program produces the following output:

1 2 3 4 5 6 7 8 9

As shown in the above program, a two-dimensional array is declared and then initialized. Then to access and print all the array elements, we have set two for loops. The outer for loop is to access the elements row-wise. The inner for loop acts as a counter to access the elements in each column.

In this way, we access each element of this two-dimensional array and display it on the screen.

Conclusion

Multidimensional arrays can have any number of dimensions and as the number of dimensions increase, the complexity also increases accordingly.

Though we have used two-dimensional arrays in all the above examples, we can code arrays with higher dimensions in a similar manner.

=> Click Here For The Free C++ Course.