9.5: Multidimensional Arrays
- Page ID
- 53883
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\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]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\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]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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[10][20]; Three dimensional array: int three_d[10][20][30];
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[10][20] can store total (10*20) = 200 elements.
Similarly the array three_d[10][20][30] 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[10][20];
- 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:
Initializing Two Dimensional Arrays
There are two ways in which a Two-Dimensional array can be initialized.
First Method:
int x[3][4] = {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[3][4] = {{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[2][1];
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[3][2] = {{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][0]: 0 Element at newArrray[0][1]: 1 Element at newArrray[1][0]: 2 Element at newArrray[1][1]: 3 Element at newArrray[2][0]: 4 Element at newArrray[2][1]: 5
Three-Dimensional Array
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[2][3][4] = {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[2][3][4] = { { {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[2][3][2] =
{
{ {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][0][0] = 0 Element at newArray[0][0][1] = 1 Element at newArray[0][1][0] = 2 Element at newArray[0][1][1] = 3 Element at newArray[0][2][0] = 4 Element at newArray[0][2][1] = 5 Element at newArray[1][0][0] = 6 Element at newArray[1][0][1] = 7 Element at newArray[1][1][0] = 8 Element at newArray[1][1][1] = 9 Element at newArray[1][2][0] = 10 Element at newArray[1][2][1] = 11
In similar ways, we can create arrays with any number of dimension. However the complexity also increases as the number of dimension increases.
Adapted from:
"Multidimensional Arrays in C / C++" by Harsh Agarwal, Geeks for Geeks is licensed under CC BY-SA 4.0