English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

c++ Multidimensional arrays

In this article, you will learn about C ++How to declare them, access them, and use them effectively in programs.

In C ++In it, a multidimensionalArrays. For example:

int x[3][4];

Here, x is a two-dimensional array. It can accommodate at most12elements.

You can consider this array as having3rows table, each row has4columns, as shown below.

Three-dimensional arrays also work in a similar way. For example:

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

The array x can accommodate at most24elements. You can consider this example as:2Each element of the array can accommodate4elements, that is8elements, and this8Each element of the array can contain3elements. Therefore, the total number of elements this array can accommodate is24.

Initialization of multidimensional arrays

You can initialize a multidimensional array in multiple ways.

Initialization of a two-dimensional array

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

A better method to initialize this array with the same array elements as above.

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

Initialization of a 3D 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};

A better way to initialize this array with the same elements as above.

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}, {3, 1, 4, 9} }
                 };

Example1:2D array

C ++Program displays all elements of the initialized 2D array.

#include <iostream>
using namespace std;
int main()
{
    int test[3][2] =
    {
        {2, -5},
        {4, 0},
        {9, 1}
    };
    // Using to access the 2D array
    // Nested loops
    for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 2; ++j)
        {
            cout << "test[" << i << "][" << j << "] = " << test[i][j] << endl;
        }
    }
    return 0;
}

Output Result

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

Example2:2D array

C ++The program stores and displays the temperatures of two different cities for a week.

#include <iostream>
using namespace std;
const int CITY = 2;
const int WEEK = 7;
int main()
{
    int temperature[CITY][WEEK];
    cout << "Input the temperatures for all days of the week for the first and second cities. \n"
    // Insert values into the temperature array
    for(int i = 0; i < CITY; ++i)
    {
        for(int j = 0; j < WEEK; ++j)
        {
            cout << "City " << i + 1 << ", Day " << j + 1 << " : ";
            cin >> temperature[i][j];
        }
    }
    cout << "\n\nDisplay values:\n"
    // Access values in the temperature array
    for(int i = 0; i < CITY; ++i)
    {
        for(int j = 0; j < WEEK; ++j)
        {
            cout << "City " << i + 1 << ", Day " << j + 1 << " = " << temperature[i][j] << endl;
        }
    }
    return 0;
}

Output Result

Input the temperatures for all days of the week for the first and second cities.
City 1, Day 1 : 32
City 1, Day 2 : 33
City 1, Day 3 : 32
City 1, Day 4 : 34
City 1, Day 5 : 35
City 1, Day 6 : 36
City 1, Day 7 : 38
City 2, Day 1 : 23
City 2, Day 2 : 24
City 2, Day 3 : 26
City 2, Day 4 : 22
City 2, Day 5 : 29
City 2, Day 6 : 27
City 2, Day 7 : 23
Display value:
City 1, Day 1 = 32
City 1, Day 2 = 33
City 1, Day 3 = 32
City 1, Day 4 = 34
City 1, Day 5 = 35
City 1, Day 6 = 36
City 1, Day 7 = 38
City 2, Day 1 = 23
City 2, Day 2 = 24
City 2, Day 3 = 26
City 2, Day 4 = 22
City 2, Day 5 = 29
City 2, Day 6 = 27
City 2, Day 7 = 23

Example3:3D array

C ++Program to store user input values in a 3D array and display them.

#include <iostream>
using namespace std;
int main()
{
    // This array can store up to12elements (2x3x2)
    int test[2][3][2];
    cout << "Input"12values: \n";
    
        //Insert values into the test array
        //Using3Nested for loops.
    for(int i = 0; i < 2; ++i)
    {
        for(int j = 0; j < 3; ++j)
        {
            for(int k = 0; k < 2; ++k)
            {
                cin >> test[i][j][k];
            }
        }
    }
    cout << "\nDisplay stored values:" << endl;
    // Display values with indices.
    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 Result

Input12number of digits: 
1
2
3
4
5
6
7
8
9
10
11
12
Display the stored values:
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

With the increase in dimensions, although the concepts are very similar, the complexity also increases greatly.