[toc]

## 1. What is Multi-Dimensional array?

We know how often one-dimensional and two-dimensional arrays used in C++. But the dimensions are not limited to only two dimensions. When we access an array with more than two indexes, then we call that array as a multi-dimensional array. When an array is accessed using three indexes, we call that as a **‘3D Array’** and when it is accessed using four indexes; we call it as a **‘4D Array’** and so on.

In this example, we will create a three-dimensional array and then populate values in it. And then we will read it and display the result in the console window.

## 2. Array Declaration

A Three-Dimensional array can be declared as shown below:

1 2 |
//Sample 01: Declare an three dimentional Array int ThreeDArray[3][3][2]; |

In the above example, we declared an array with 3 rows, 3 columns, and 2 slabs. Similarly, you can declare a 4-Dimensional and 5-Dimensional arrays. The first square bracket represents rows, the second square bracket means columns and the third one represents the slabs of rows and columns.

## 3. Writing Multi-Dimensional Arrays

We use three ‘for’ loops to populate the array. First, we populated the first slab with the value of 1. Next, we populated the second slab with the value of 2. To put it differently, we populated two 2-dimensional arrays and stacked it one over the other. Below is the code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Sample 02: Initialize the array. for(int row=0; row<3; row++) { for(int col=0; col<3; col++) { for(int depth = 0; depth<2; depth++) { if (depth == 0) ThreeDArray[row][col][depth] = 1; else ThreeDArray[row][col][depth] = 2; } } } |

Note, how we formed the loops. The first loop represents iteration over rows and the second one shows iteration over columns and the third one shows iteration over slabs. After we populate the array, it will look like the below:

## 4. Reading Multi-Dimensional Arrays

The array is read the same way it was written. In our example, we read the array slab by slab. In fact, we read our Three-Dimensional array treating it as a two 2-Dimensional arrays. Below is the code that reads the data from the array:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Sample 03: Read the array. for(int depth = 0; depth<2; depth++) { for(int row=0; row<3; row++) { for(int col=0; col<3; col++) { printf("3DArray[%d][%d][%d] = %d\n", row,col,depth, ThreeDArray[row][col][depth] ); } } } |

## 5. Completed Code Example

Below is the complete code example:

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// TestIt.cpp : Defines the entry point for //the console application. // #include "stdafx.h" #include "conio.h" int _tmain(int argc, _TCHAR* argv[]) { //Sample 01: Declare an three dimentional Array int ThreeDArray[3][3][2]; //Sample 02: Initialize the array. for(int row=0; row<3; row++) { for(int col=0; col<3; col++) { for(int depth = 0; depth<2; depth++) { if (depth == 0) ThreeDArray[row][col][depth] = 1; else ThreeDArray[row][col][depth] = 2; } } } //Sample 03: Read the array. for(int depth = 0; depth<2; depth++) { for(int row=0; row<3; row++) { for(int col=0; col<3; col++) { printf("3DArray[%d][%d][%d] = %d\n", row,col,depth, ThreeDArray[row][col][depth] ); } } } getch(); return 0; } |

Output of the above example is below:

Categories: C++

## Do you like this Example? Please comment about it for others!!