---
title: "C++ Multidimensional Arrays Tutorial"
url: https://weworkworldwide.com/tutorials/multidimensional-arrays-2/
description: "C++ Multidimensional Arrays Tutorial: Before reading this section we're assuming you already read the Arrays section. Instead of putting a single value…"
date: 2026-11-30T13:00:04+00:00
source: https://weworkworldwide.com/llms.txt
---

# C++ Multidimensional Arrays Tutorial

“There are risks and costs to action. But they are far less than the long range risks of comfortable inaction” John F. Kennedy

Before reading this section we’re assuming you already read the *Arrays section*.

Instead of putting a single value for each element of an array, in multi-dimensional array we can put another array for each of these elements.

In a single-dimensional array, we used one pair of bracket `[]` and put the index number in it to access the memory space in order to get or set the value there.

When it comes to multi-dimensional array, we need to add another pair of bracket `[]` as the level of arrays increases.

This is how we can declare such array:

``` line-numbers
Data-type array-name[number-of-elements-in-first-array][ number-of-elements-in-sub-array][ number-of-elements-in-sub-sub-array]…;
```

There’s no difference between using `data-type` and `array-name` in single and multi-dimensional arrays.

`[number-of-elements-in-first-array]`: the first pair of bracket is used to set the number of the elements that this array will have.

Note: here we put the number of elements and we don’t care if those elements are themselves another array or not.

`[ number-of-elements-in-sub-array]`: the second pair of bracket is used to declared that each element of the last array is itself another array and the number of elements in that array will be declared in this second pair of bracket.

`[ number-of-elements-in-sub-sub-array]`: We can declare as many brackets `[]` as we want, and that means each element of the last array is itself another array and via this pair of brackets we’re declaring the size of each of these elements.

Let’s see some examples:

``` line-numbers
int integerArray[220][10][30];
 double doubleArray[102][234];
 long longArray[100][200][300];
```

The first variable in the example above is `integerArray` and that is a 3 dimensional array or 3 levels array. The first level has `220` number of elements. Each of these elements is another array (the second level) and the number of elements for this sub-array is 10. Of course each of the elements of the sub-array is itself another array and the number of elements in this sub-sub-array is 30.

The second variable is `doubleArray` and that is a two dimensional array or 2 levels array. The first array (the first level array) has 102 elements and each of these elements is itself another array and the number of elements in that array (sub-array) is 234 which we used the second-pair of bracket to declare this number.

The third variable is `longArray` and that is a three dimensional array or 3 levels array. The first array has 100 elements and each of these elements is itself another array and the number of elements in that array (sub-array) is 200 which we used the second pair of bracket to declare this number. Finally each element of this `sub-array` is itself another array and we used the third pair of bracket `[]` to declare the number of elements for that `sub-sub-array` which is 300.

Note: when only declaring a multi-dimensional array, the number of elements for each array should be declared. But when initializing such array, the value of the first brackets `[]` can be ignored but the rest of brackets should be filled with numbers.

Example:

``` line-numbers
#include <iostream>
using namespace std;
int main() {
int multi[][]= {
{1,2,3,4,5},
{1,2,3,4,5},
{1,2,3,4,5}
}
return 0;
}
```

Output:

error: declaration of ‘multi’ as multidimensional array must have bounds for all dimensions except the first.

As the output shows, because we initialized a multi-dimensional array but didn’t fill the number of elements for the second pair of bracket, we’ve got a compile time error.

Initializing a multi-dimensional array is pretty much the same as initializing a single-dimensional array.

Example:

``` line-numbers
int age [5][10]= {
 {80,90,93,50,99,76,100,88,99,30},
 {50,96,97,50,91,74,59,83,93,38},
 {84,91,97,54,91,75,10,85,96,36},
 {60,81,90,54,56,89,78,98,78,98},
 {95,45,98,62,85,78,98,100,64,80}
 };
```

In this example, the `age` array is an array with 5 elements and each element is itself an array that contains 10 integer values and we put those values in a separated braces.

## Note: each sub-array is separated via comma `,`.

## Accessing elements in the arrays:

In order to access elements in a multi-dimensional array, we use brackets just like the way we used it for single-array.

## Note: the more brackets we use the deeper level we get.

For example let’s say we have a two-dimensional array:

``` line-numbers
double doubleArray[12][24];
```

Now let’s say we want to access the third element of the first array list in the `doubleArray`:

``` line-numbers
doubleArray[0][2];
```

Or let’s say we want to access the 20^(th) element of the second array list in the `doubleArray`:

``` line-numbers
doubleArray[1][19];
```

So the index that we set for the first bracket gives us the access to the elements of the first array and the second one gives us the access to the elements of the sub-array.

## Looping through elements in multi-dimensional array:

``` line-numbers
Note: we're about to use `for-loop` in this part, so if you're not familiar with this loop, please read the for-loop section first.
```

To loop through a multidimensional array, we need a for-loop for each dimension of the array.

For example if we have a 2 dimensional array, there should be 2 loops (the second one goes inside the first loop) in order to loop through all the elements within the array and the sub-arrays (the elements of the first array).

Example:

``` line-numbers
#include <iostream>
 using namespace std;
 int main() {
 
 int age [5][10]= {
 {80,90,93,50,99,76,100,88,99,30},
 {50,96,97,50,91,74,59,83,93,38},
 {84,91,97,54,91,75,10,85,96,36},
 {60,81,90,54,56,89,78,98,78,98},
 {95,45,98,62,85,78,98,100,64,80}
 };
 
 for (int i = 0; i<5; i++){
 
 for (int b = 0 ; b<10; b++){
 cout<<age[i][b]<< ", ";
 }
 }
 
 return 0;
 }
```

Output:

80, 90, 93, 50, 99, 76, 100, 88, 99, 30, 50, 96, 97, 50, 91, 74, 59, 83, 93, 38, 84, 91, 97, 54, 91, 75, 10, 85, 96, 36, 60, 81, 90, 54, 56, 89, 78, 98, 78, 98, 95, 45, 98, 62, 85, 78, 98, 100, 64, 80,

As you can see the variable set in the first loop `i` is set as the index number of the first brackets `[]` and the second variable that is set in the second loop `b` is set as the index number of the second brackets `[]` of the array.
