Introduction to C++ | Multidimensional Arrays

C++ Multidimensional Arrays

A multidimensional array in C++ is an array of arrays. It allows you to store data in multiple dimensions, making it useful for tasks like working with matrices, grids, and tables. The most common multidimensional array is a two-dimensional array, but C++ allows you to create arrays with more than two dimensions.

What is a Multidimensional Array?

A multidimensional array is an array that can hold other arrays as its elements. Each element in the multidimensional array can be accessed using multiple indices. For example, in a 2D array, the elements are accessed by row and column indices.

To declare a multi-dimensional array, define the variable type, specify the name of the array followed by square brackets which specify how many elements the main array has, followed by another set of square brackets which indicates how many elements the sub-arrays have:

The syntax for declaring a multidimensional array is:

Syntax of Multidimensional Arrays


        dataType arrayName[size1][size2]...[sizeN];
                    

Example of a Two-Dimensional Array

A two-dimensional array can be visualized as a matrix with rows and columns. Here’s an example of how to declare, initialize, and access a 2D array:

Code Example: Two-Dimensional Array


            {% raw %}
#include <iostream>
using namespace std;

int main() {
// Declare and initialize a 2D array
int matrix[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

// Access and print elements of the 2D array
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        cout << "matrix[" << i << "][" << j << "] = " << matrix[i][j] << endl;
    }
}

return 0;
}
{% endraw %}
        

Output

matrix[0][0] = 1
matrix[0][1] = 2
matrix[0][2] = 3
matrix[1][0] = 4
matrix[1][1] = 5
matrix[1][2] = 6
matrix[2][0] = 7
matrix[2][1] = 8
matrix[2][2] = 9

Accessing Elements of a Multidimensional Array

Elements in a multidimensional array are accessed by specifying multiple indices, one for each dimension. In the case of a 2D array, the first index represents the row, and the second index represents the column.

For example, matrix[0][0] refers to the element in the first row and first column of the 2D array.

Example of a Three-Dimensional Array

A three-dimensional array can be visualized as a collection of 2D matrices. Here’s an example of how to declare and access a 3D array:

Code Example: Three-Dimensional Array


        {% raw %}
#include <iostream>
using namespace std;

int main() {
// Declare and initialize a 3D array
int array[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};

// Access and print elements of the 3D array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
    for (int k = 0; k < 2; k++) {
        cout << "array[" << i << "][" << j << "][" << k << "] = " << array[i][j][k] << endl;
    }
}
}

return 0;
}
{% endraw %}
    

Output

array[0][0][0] = 1
array[0][0][1] = 2
array[0][1][0] = 3
array[0][1][1] = 4
array[1][0][0] = 5
array[1][0][1] = 6
array[1][1][0] = 7
array[1][1][1] = 8

Dynamic Multidimensional Arrays

In some cases, the size of the multidimensional array may not be known at compile time. In these cases, you can create dynamic multidimensional arrays using pointers and memory allocation functions such as new.

Here’s an example of a dynamically allocated 2D array:

Code Example: Dynamic 2D Array


                        {% raw %}
        #include <iostream>
        using namespace std;
        
        int main() {
            int rows = 3, cols = 3;
            
            // Dynamically allocate memory for a 2D array
            int** matrix = new int*[rows];
            for (int i = 0; i < rows; i++) {
                matrix[i] = new int[cols];
            }
        
            // Initialize the array elements
            int value = 1;
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    matrix[i][j] = value++;
                }
            }
        
            // Print the array elements
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    cout << matrix[i][j] << " ";
                }
                cout << endl;
            }
        
            // Deallocate the memory
            for (int i = 0; i < rows; i++) {
                delete[] matrix[i];
            }
            delete[] matrix;
        
            return 0;
        }
        {% endraw %}
                    

Output

1 2 3
4 5 6
7 8 9

Important Points Of Multidimensional Array