Multi-dimensional Array In C++

Multi-dimensional Array In C++

This article will teach you how to use multi-dimensional arrays in C++. More particularly, how to declare, access, and effectively utilize them in our program.

In C++ arrays, Multi-Dimensional Arrays are used to store data in the shape of a table with rows and columns. In this section, we can design single or multidimensional arrays to hold values in many contexts.

A single-dimensional array has one dimension, whereas a multidimensional array might have two, three, or more dimensions. The multidimensional array can be thought of as an array of arrays. The data is stored in tabular form here.

In this post, we will look at what a multi-dimensional array is, how to utilize one, how to access one, and how to use a multi-dimensional array successfully in our code.

We can create a multidimensional array in C++ by creating an array of an array. As an example:

To comprehend the multidimensional array, take the example of a matrix. A 2D matrix will have rows and columns. A 2D dimensional array is used to represent this.

Data is stored in multidimensional arrays in the form of a table, in row-major order. A 2-dimensional array’s general syntax is as follows.

int x[3][4];

x is a two-dimensional array in this case. It has a maximum capacity of 12 elements.

Three-dimensional arrays function similarly. As an example:

float x[2][4][3];

This array x can hold up to 24 elements.

Simply multiplying the array’s dimensions yields the total number of elements:

2 x 4 x 3 = 24

Multidimensional Array Initialization

A multidimensional array, like any other array, can be initialized in multiple ways.

Setup of a two-dimensional array

int test[2][3] = {2, 4, 5, 9, 0, 19};

The above method is not preferred. A better way to initialize this array with the same array elements is given below:

int  test[2][3] = { {2, 4, 5}, {9, 0, 19}};

Because this array contains two rows and three columns, we have two rows of elements with three elements each.

Setup of a three-dimensional array

int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23, 
                 2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};

This is not an appropriate method to begin a three-dimensional array. This is a better approach to initialize the array:

int test[2][3][4] = { 
                     { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
                     { {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} }
                 };

Take note of the three-dimensional array’s dimensions.

The value for the first dimension is 2. As a result, the two elements that comprise the first dimension are:

Element 1 = { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} }
Element 2 = { {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} }

The value is in the second dimension. 3. Take note that each of the first dimension’s elements has three elements:

{3, 4, 2, 3}, {0, -3, 9, 11} and {23, 12, 23, 2} for Element 1.
{13, 4, 56, 3}, {5, 9, 3, 5} and {5, 1, 4, 9} for Element 2.

Finally, each of the second dimension’s constituents contains four int numbers:

{3, 4, 2, 3}
{0, -3, 9, 11}
… .. …
… .. …

Example 1: Two-Dimensional Array

// C++ Program to display all elements
// of an initialised two dimensional array

#include <iostream>
using namespace std;

int main() {
    int test[3][2] = {{2, -5},
                      {4, 0},
                      {9, 1}};

    // use of nested for loop
    // access rows of the array
    for (int i = 0; i < 3; ++i) {

        // access columns of the array
        for (int j = 0; j < 2; ++j) {
            cout << "test[" << i << "][" << j << "] = " << test[i][j] << endl;
        }
    }

    return 0;
}

Output

test[0][0] = 2
test[0][1] = -5
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1

In the preceding example, we created a two-dimensional int array named test with three “rows” and two “columns.”

To display the array elements, we utilized a nested for loop.

The rows of the array are accessed by the outer loop from I == 0 to I == 2.

The inner loop from j == 0 to j == 1 accesses the array’s columns.

Finally, on each iteration, we print the array elements.

Example 2: Taking Input for Two-Dimensional Array

#include <iostream>
using namespace std;

int main() {
    int numbers[2][3];

    cout << "Enter 6 numbers: " << endl;

    // Storing user input in the array
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cin >> numbers[i][j];
        }
    }

    cout << "The numbers are: " << endl;

    //  Printing array elements
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << "numbers[" << i << "][" << j << "]: " << numbers[i][j] << endl;
        }
    }

    return 0;
}

Output

Enter 6 numbers:
1
2
3
4
5
6
The numbers are:
numbers[0][0]: 1
numbers[0][1]: 2
numbers[0][2]: 3
numbers[1][0]: 4
numbers[1][1]: 5
numbers[1][2]: 6

To take the input of the 2d array, we utilized a nested for loop. We used another nested for loop to output the array members once we had taken all of the input.

Example 3: Three-Dimensional Array

// C++ Program to Store value entered by user in
// three dimensional array and display it.

#include <iostream>
using namespace std;

int main() {
    // This array can store upto 12 elements (2x3x2)
    int test[2][3][2] = {
                            {
                                {1, 2},
                                {3, 4},
                                {5, 6}
                            }, 
                            {
                                {7, 8}, 
                                {9, 10}, 
                                {11, 12}
                            }
                        };

    // Displaying the values with proper index.
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 2; ++k) {
                cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl;
            }
        }
    }

    return 0;
}

Output

test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12

Printing elements of a 3d array is identical to printing elements of a 2d array.

However, because we are controlling three dimensions, we use a nested for loop with three total loops rather than simply two:

The outer loop from I == 0 to I == 1 accesses the array’s first dimension.

The middle loop from j == 0 to j == 2 accesses the array’s second dimension.

The innermost loop from k == 0 to k == 1 accesses the array’s third dimension.

As we can see, the array’s complexity grows exponentially with the number of dimensions.

you may like

C++ Break Statement with Example | C++ Programming

C++ Continue Statement with Example | C++ Programming

Leave a Reply