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

1 |
int array[3][5]; // a 3-element array of 5-element arrays |

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:

1 |
array[2][3] = 7; |

**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:

1 2 3 4 5 6 |
int array[3][5] = { { 1, 2, 3, 4, 5 }, // row 0 { 6, 7, 8, 9, 10 }, // row 1 { 11, 12, 13, 14, 15 } // row 2 }; |

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.

1 2 3 4 5 6 |
int array[3][5] = { { 1, 2 }, // row 0 = 1, 2, 0, 0, 0 { 6, 7, 8 }, // row 1 = 6, 7, 8, 0, 0 { 11, 12, 13, 14 } // row 2 = 11, 12, 13, 14, 0 }; |

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

1 2 3 4 5 6 |
int array[][5] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 } }; |

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

1 2 3 4 5 |
int array[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; |

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

1 |
int array[3][5] = { 0 }; |

Note that this only works if you explicitly declare the length of the array! Otherwise, you will get a two-dimensional array with 1 row.

**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.

1 2 3 |
for (int row = 0; row < numRows; ++row) // step through the rows in the array for (int col = 0; col < numCols; ++col) // step through each element in the row std::cout << array[row][col]; |

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:

1 |
int array[5][4][3]; |

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:

1 |
std::cout << array[3][1][2]; |

**A two-dimensional array example**

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> int main() { // Declare a 10x10 array const int numRows = 10; const int numCols = 10; int product[numRows][numCols] = { 0 }; // Calculate a multiplication table for (int row = 0; row < numRows; ++row) for (int col = 0; col < numCols; ++col) product[row][col] = row * col; // Print the table for (int row = 1; row < numRows; ++row) { for (int col = 1; col < numCols; ++col) std::cout << product[row][col] << "\t"; std::cout << '\n'; } return 0; } |

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.

6.6 -- C-style strings |

Index |

6.4 -- Sorting an array using selection sort |

Can someone explain the output for the below program??

Output:

21

18

21

Hi KKR!

@Alex Do you have an explanation for why Line 8 in @KKR's code is a thing? I can't find any information about it. Probably because I don't know what to search for.

Thank you nascardriver!!

And please post if you find info regarding that syntax. I find it quite weird!

Found it!

a[b] is *(a + b)

and since *(a + b) == *(b + a)

a[b] == b[a]

Source: https://stackoverflow.com/questions/381542/with-arrays-why-is-it-the-case-that-a5-5a#381549

Wow nice.. Thanks again!!

Hello, why when I make a multidimensional array like this

std::array<std::array<int, 3>, 3> arr{ { { 1, 2, 3 },{ 4, 5, 6 },{ 7, 8, 9 } } }

I cant access it as in your example std::cout << array[0][1][2]

The last one says "Expression must have pointer to object"

Hi Will!

Two issues,

1] Your array is two-dimensional (3x3), but you're trying to access it like a three-dimensional array. The deepest you can go on a two-dimensional array is arr[x][y], not arr[x][y][z].

2] You named your variable 'arr' but you're trying the access 'array'. That's what's causing your error.

Ahh I see lol I thought I was doing a 3d one

Yeah the name I noticed, was only curious about the last one asking for a pointer ^^

Thanks

Maybe it's just for education purposes, but otherwise wouldn't you keep it simple and create a "int multiply(int x, int y){}" function and just make it so "array[row][col] = multiply(row, col)" in a for loop?

You could, but multiplying 2 integers is such a simple operation I'm not sure why you'd create a function for it.

how can I set the value of 2d array from the user?

int a, b;

cout << "Enter first value : " ;

cin >> a ;

cout << "Enter second value : " ;

cin >> b ;

// i will get error

int myArray[a][b]

You have to use dynamic allocation. I talk about this in a later lesson in this chapter.

can we use vectors as multi-dimensional arrays?

if yes, how?

You could have a vector of vectors. But you're maybe better off allocating a single dimensional vector and use math to map two coordinates down to one.