C++ Multidimensional Arrays

C++ programming language supports multi dimensional Arrays. Multidimensional arrays can be described as "arrays of arrays". For Example, a two dimensional array:

int matrix[3][4];

can be imagined as a table of 12 elements arranged in 3 rows and 4 columns. First dimension represents the number of row and second dimension represent number of vertical columns.

  • Multi dimensional arrays are array of arrays.
  • Multi dimensional arrays have more than one subscript variables.
  • Multi dimensional array is also called as matrix.
2D

Multidimensional Array Declaration

data_type array_name[size1][size2]...[sizeN];

Above statement will declare an array of N dimensions of name array_name, where each element of array is of type data_type. The maximum number of elements that can be stored in a multi dimensional array array_name is size1 X size2 X size3...sixeN.

For Example :
Declaration of two dimensional integer array
    int maze[8][8];
Declaration of three dimensional character array
    char rubix[50][60][30];

Multidimensional Array Initialization

Initialization of Two Dimensional Array

Two dimensional arrays can be initialized by specifying elements for each row inside curly braces. The following declaration initializes a two dimensional matrix of 4 rows and 3 columns.

int matrix[4][3] = {
                    {1, 2, 3},    /* Initialization of first row */
                    {4, 5, 6},    /* Initialization of second row */
                    {7, 8, 9},    /* Initialization of third row */
                    {10, 11, 12}, /* Initialization of fourth row */
                   };

Similarly, we can initialize any multi dimensional array.
A two dimensional matrix can be initialized without using any internal curly braces as follows:

int matrix[4][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

In above declaration, compiler will assign first four values(1, 2, 3 and 4) to first row of matrix and within a row it will populate elements from left to right. Then next four elements to second row and so on.

Initialization of Three Dimensional Array

Similarly, we can initialize any multi dimensional array.
Similar to two dimensional array, we can also initialize 3 dimensional array as follows :

int rubix[2][3][2] = {4, 6, 1, 0, 6, 12, 3, 10, 0, -4, 8, 9};

Two Dimensional Array in C++

Two dimensional arrays are most common type of multi dimensional array in C++. A two dimensional array in C++ language is represented in the form 2D matrix having rows and columns.

  • A two dimensional matrix is an array of one dimensional arrays.
  • Two dimensional array requires two subscript variables or indexes.
  • One subscript represents the row index while other represent column index of an element in matrix.
  • Elements of a two dimensional array are stored in contiguous memory location. First all elements of first row are store then all elements of second row and so on.
C++ Two Dimensional Array Memory Map

Any element in two dimensional matrix is uniquely identified by array_name[row_Index][column_Index], where row_index and column_index are the subscripts that uniquely specifies the position of an element in a 2D matrix.
An element in second row and third column of an two dimensional matrix whose name is board is accessed by board[1][2]. Like single dimensional array, indexing starts from 0 instead of 1.
We can declare a two dimensional matrix of R rows and C columns as follows:

data_type array_name[R][C];
For Example :

A two dimensional integer matrix of 3 rows and 3 columns can be declared as

int score[3][3];

C++ Two Dimensional Array Example program

#include <iostream>
using namespace std;
  
int main(){
    // Two dimentional array declaration
    int matrix[20][20];
    int rowCounter, colCounter, rows, cols, sum=0;
     
    cout << "Enter size of a matrix\n";
    cin >> rows >> cols;
    // Populating elements inside matrix
    cout << "Enter elements of a matrix\n";
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            cin >> matrix[rowCounter][colCounter];
        }
    }
    // Accessing all elements of matrix to calculate their sum
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            sum += matrix[rowCounter][colCounter];
        }
    }
     
    cout << "Sum of all elements = " << sum;

    return 0;
}

Output
Enter size of a matrix
3 3
Enter elements of a matrix
1 2 3
3 4 5 
6 7 8
Sum of all elements = 39
In above program, we first take number of rows and columns as input from user. Then using two for loops we take matrix elements input from user and store it in 2D array named matrix. To find the sum of each element of matrix we traverse the matrix row wise using two for loop.