C++ Multidimensional Arrays

In this tutorial, you will learn about multidimensional arrays and how to declare and use a multidimensional array in C++ with the help of examples.


In C++ you can declare multidimensional arrays which are basically an array or arrays. You can define more than 2 or even 3-dimensional arrays. The syntax of declaring one is:

int arr[n][m];

Here the n and m are the numbers of rows and columns or the 2 dimensions of the array. The total size of such an array is equal to n*m. for example, an array with the declaration arr[5][10] can hold up to 5*10 or 500 elements in it. You can access them using two indexes where each refers to that corresponding dimension of the array.

The following table will show you how a table can be accessed. Let’s assume an array with 3 and 3 as its two dimensions. Then:

arr[3][3] Column 0 Column 1 Column 2
Row 0 arr[0][0] arr[0][1] arr[0][2]
Row 1 arr[1][0] arr[1][1] arr[1][2]
Row 2 arr[2][0] arr[2][1] arr[2][2]

How to initialize a multidimensional array?

There are two different ways of doing so which are :

Method 1:

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

Method 2:

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

In both the methods, the same array is declared but the method is different as with the help of curly braces, one can easily understand which are the rows and where the demarcation between two rows lies.

C++ Multidimensional Array Example

Two Dimensional(2D) Array

Let’s take a simple example of an array in C++ which declares, initializes, and traverses two-dimensional arrays.

#include<iostream>
using namespace std;
int main()
{
    // an array with 4 rows and 2 columns.
    int x[4][2] = {{0,1}, {2,3}, {4,5} , {6,7}};
    // output each array element's value
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << "Element at x[" << i << "][" << j << "]: ";
            cout << x[i][j]<<endl;
        }
    }
    return 0;
}

Output

Element at x[0][0]: 0
Element at x[0][1]: 1
Element at x[1][0]: 2
Element at x[1][1]: 3
Element at x[2][0]: 4
Element at x[2][1]: 5
Element at x[3][0]: 6
Element at x[3][1]: 7

Three Dimentional(3D) Array

The following code shows the method of declaring and printing a 3D array and with more dimensions, the number of loops increases.

#include<iostream>
using namespace std;
int main()
{
    // initializing the 3-dimensional array with proper braces
    int x[2][3][2] =
    {
        { {0,1}, {2,3}, {4,5} },
        { {6,7}, {8,9}, {10,11} }
    };

    // output each element's value with 3 loops as 3D array
    for (int i = 0; i < 2; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            for (int k = 0; k < 2; ++k)
            {
                cout << "Element at x[" << i << "][" << j
                     << "][" << k << "] = " << x[i][j][k]
                     << endl;
            }
        }
    }
    return 0;
}

Output

Element at x[0][0][0] = 0
Element at x[0][0][1] = 1
Element at x[0][1][0] = 2
Element at x[0][1][1] = 3
Element at x[0][2][0] = 4
Element at x[0][2][1] = 5
Element at x[1][0][0] = 6
Element at x[1][0][1] = 7
Element at x[1][1][0] = 8
Element at x[1][1][1] = 9
Element at x[1][2][0] = 10
Element at x[1][2][1] = 11

Please get connected & share!

Advertisement