# 9.5: Multidimensional Arrays

$$\newcommand{\vecs}{\overset { \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$

In C++, we can define multidimensional arrays, an array of arrays. Data in multidimensional arrays are stored in tabular form (in row major order).

I general, the form of declaring multidimensional arrays:

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

data_type: This is the type of data to be stored in the array.
data_type MUST be a valid C++ data type
array_name: Name of the array
size1, size2,... ,sizeN: The size of each of the dimensions


Examples:

Two dimensional array:
int two_d;

Three dimensional array:
int three_d;


### Size of multidimensional arrays

Total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions.
For example:
The above array named two_d can store total (10*20) = 200 elements.
Similarly the array three_d can store total (10*20*30) = 6000 elements.

### Two-Dimensional Array

A two dimensional array is the simplest form of a multidimensional array. We can see a two dimensional array as an array of one dimensional array for easier understanding.

• The basic form of declaring a two-dimensional array of size row, col.
Syntax:
data_type array_name[row][col];
data_type: Type of data to be stored. Valid C/C++ data type.

• We can declare a two dimensional integer array say ‘newArray’ of size 10,20 as:
int newArray;

• Elements in two-dimensional arrays are commonly referred with a syntax of newArray[row][col].
• A two dimensional array can be seen as a table with rows and columns where the row number ranges from 0 to (row-1) and column number ranges from 0 to (col-1). A two dimensional array ‘newArray’ with 3 rows and 3 columns is shown below: Figure $$\PageIndex{1}$$: Two dimensional array ("Two dimensional array" by Pat McClanahan, Wikimedia Commans is licensed under CC BY-SA 4.0)

#### Initializing Two Dimensional Arrays

There are two ways in which a Two-Dimensional array can be initialized.

First Method:

int x = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}


The above array has 3 rows and 4 columns. The elements in the braces from left to right are stored in the table also from left to right. The elements will be fill the array, the first 4 elements from the left in first row, the next 4 elements in second row and so on.

This is a valid approach, but it can be difficult to read and to debug in the case of a large array.

Better Method:

int x = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};


This type of initialization make use of nested braces. Each set of inner braces represents one row. In the above example there are total three rows so there are three sets of inner braces.

#### Accessing Elements of Two-Dimensional Arrays

Elements in two dimensional arrays are accessed using the both of the indexes.

int newArray;


The above example represents the element present in third row and second column.

Remember: Arrays always count from 0 therefore, row index 2 is actually the third row, and column index 1 is actually the second column.

To output all the elements of two dimensional array we can use nested for loops. One loop to traverse the rows and another to traverse columns.

// C++ Program to print the elements of a
// Two-Dimensional array
#include<iostream>
using namespace std;

int main()
{
// an array with 3 rows and 2 columns.
int newArrray = {{0,1}, {2,3}, {4,5}};
// output each array element's value
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 2; col++)
{
cout << "Element at newArrray[" << row
<< "][" << col << "]: ";
cout << newArrray[row][col]<<endl;
}
}

return 0;
}


Output:

Element at newArrray: 0
Element at newArrray: 1
Element at newArrray: 2
Element at newArrray: 3
Element at newArrray: 4
Element at newArrray: 5


### Three-Dimensional Array Figure $$\PageIndex{1}$$: Three-Dimensional Array ("Three-Dimensional Array" by Harsh Agarwal, Geeks for Geeks is licensed under CC BY-SA 4.0)

#### Initializing Three-Dimensional Array

Initialization in three dimensional array is same as that of two dimensional arrays. The difference is as the number of dimension increases so the number of nested braces will also increase.

As with the tewo dimensional arrays, it is allowable to simply initialize a series of values that equals the length of the array.

Method 1:

int x = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23};


Better Method:

int x =
{
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
};


#### Accessing elements in Three-Dimensional Arrays

Accessing elements in three dimensional arrays is also similar to accessing two dimensional array elements. The difference is we have to use three loops instead of two loops for one additional dimension in three dimensional rrays.

// C++ program to print elements of Three-Dimensional
// Array
#include<iostream>
using namespace std;

int main()
{
// initializing the 3-dimensional array
int newArray =
{
{ {0,1}, {2,3}, {4,5} },
{ {6,7}, {8,9}, {10,11} }
};

// output each element's value
for (int level1 = 0; level1 < 2; ++level1)
{
for (int level2 = 0; level2 < 3; ++level2)
{
for (int level3 = 0; level3 < 2; ++level3)
{
cout << "Element at newArray[" << level1 << "][" << level2
<< "][" << level3 << "] = " << newArray[level1][level2][level3]
<< endl;
}
}
}
return 0;
}


Output:

Element at newArray = 0
Element at newArray = 1
Element at newArray = 2
Element at newArray = 3
Element at newArray = 4
Element at newArray = 5
Element at newArray = 6
Element at newArray = 7
Element at newArray = 8
Element at newArray = 9
Element at newArray = 10
Element at newArray = 11


In similar ways, we can create arrays with any number of dimension. However the complexity also increases as the number of dimension increases.