Search

10.5 — Multidimensional Arrays

The elements of an array can be of any data type, including arrays! An array of arrays is called a multidimensional array.

Since we have 2 subscripts, this is a two-dimensional array.

In a two-dimensional array, it is convenient to think of the first (left) subscript as being the row, and the second (right) subscript as being the column. This is called row-major order. Conceptually, the above two-dimensional array is laid out as follows:

[0][0]  [0][1]  [0][2]  [0][3]  [0][4] // row 0
[1][0]  [1][1]  [1][2]  [1][3]  [1][4] // row 1
[2][0]  [2][1]  [2][2]  [2][3]  [2][4] // row 2

To access the elements of a two-dimensional array, simply use two subscripts:

Initializing two-dimensional arrays

To initialize a two-dimensional array, it is easiest to use nested braces, with each set of numbers representing a row:

Although some compilers will let you omit the inner braces, we highly recommend you include them anyway, both for readability purposes and because of the way that C++ will replace missing initializers with 0.

Two-dimensional arrays with initializer lists can omit (only) the leftmost length specification:

The compiler can do the math to figure out what the array length is. However, the following is not allowed:

Just like normal arrays, multidimensional arrays can still be initialized to 0 as follows:

Accessing elements in a two-dimensional array

Accessing all of the elements of a two-dimensional array requires two loops: one for the row, and one for the column. Since two-dimensional arrays are typically accessed row by row, the row index is typically used as the outer loop.

In C++11, for-each loops can also be used with multidimensional arrays. We’ll cover for-each loops in detail later.

Multidimensional arrays larger than two dimensions

Multidimensional arrays may be larger than two dimensions. Here is a declaration of a three-dimensional array:

Three-dimensional arrays are hard to initialize in any kind of intuitive way using initializer lists, so it’s typically better to initialize the array to 0 and explicitly assign values using nested loops.

Accessing the element of a three-dimensional array is analogous to the two-dimensional case:

A two-dimensional array example

Let’s take a look at a practical example of a two-dimensional array:

This program calculates and prints a multiplication table for all values between 1 and 9 (inclusive). Note that when printing the table, the for loops start from 1 instead of 0. This is to omit printing the 0 column and 0 row, which would just be a bunch of 0s! Here is the output:

1    2    3    4    5    6    7    8    9
2    4    6    8    10   12   14   16   18
3    6    9    12   15   18   21   24   27
4    8    12   16   20   24   28   32   36
5    10   15   20   25   30   35   40   45
6    12   18   24   30   36   42   48   54
7    14   21   28   35   42   49   56   63
8    16   24   32   40   48   56   64   72
9    18   27   36   45   54   63   72   81

Two dimensional arrays are commonly used in tile-based games, where each array element represents one tile. They’re also used in 3d computer graphics (as matrices) in order to rotate, scale, and reflect shapes.


10.6 -- C-style strings
Index
10.4 -- Sorting an array using selection sort

143 comments to 10.5 — Multidimensional Arrays

  • Dylan

    Hey again thanks so much for this tutorial. I'm enjoying learning c++.

    For initializing 3 dimensional, can't you just use blocks?

  • empleat

    I absolutely don't understand usage of asterisk here whatsoever!!! Code is related to: when populating arrays using "nested for loops"

    Also I Am trying to make a simple program, which uses grid. How do I change one char in bidimensional char array? When I want to access like third row and 2nd character e.g. char arr[3][2], how would I change that single character here, or if I had int array? I tried like all C++ sites I know and google 50 links and found only 1 site, which was only partially useful. I don't understand... Nothing is found literally ever...

    • beaners

      The code in question

      This code is critical in the formation of the multiplication table. What it is doing is that an element(just pick 1  like product[5][3]) and evaluating it using the asterisk for multiplication to produce a value (in this case is 15). This is fundamental to a multiplication table as any number you can spot in the multiplication table can be formed by multiplying the row by the col to product a val.

      Lets pick 81 the bottom right value. In the evaluation process with the code you highlighted, it would be product[9][9] = 9*9 which is 81 and 81 is shown there. Perhaps maybe looking at google images of multiplication table would help.
      For your question on how to access arrays, which is covered in learncpp, and to modify them- see the following:  you can write arr[3][2] = 150 . That is it

  • Apurva

    Output of two dimensional array example is as below.
    1       2       3       4       5       6       7       8       9
    2       4       6       8       10      12      14      16      18
    3       6       9       12      15      18      21      24      27
    4       8       12      16      20      24      28      32      36
    5       10      15      20      25      30      35      40      45
    6       12      18      24      30      36      42      48      54
    7       14      21      28      35      42      49      56      63
    8       16      24      32      40      48      56      64      72
    9       18      27      36      45      54      63      72      81
    Please correct loops and array indices.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]