MANUEL V GALLEGO FOUNDATION COLLEGES, INC.
INSTITUTE OF INFORMATION AND COMPUTER TECHNOLOGY
Computer Programming 1 ICT113
C++ Multi-Dimensional Arrays
Multi-Dimensional Arrays
A multi-dimensional array is an array of arrays.
To declare a multi-dimensional array, define the variable type, specify the name of the array followed
by square brackets which specify how many elements the main array has, followed by another set of
square brackets which indicates how many elements the sub-arrays have:
string letters[2][4];
As with ordinary arrays, you can insert values with an array literal - a comma-separated list inside curly
braces. In a multi-dimensional array, each element in an array literal is another array literal.
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
Each set of square brackets in an array declaration adds another dimension to an array. An array like
the one above is said to have two dimensions.
Arrays can have any number of dimensions. The more dimensions an array has, the more complex the
code becomes. The following array has three dimensions:
string letters[2][2][2] = {
{
{ "A", "B" },
{ "C", "D" }
},
{
{ "E", "F" },
{ "G", "H" }
}
};
Access the Elements of a Multi-Dimensional Array
To access an element of a multi-dimensional array, specify an index number in each of the array's
dimensions.
This statement accesses the value of the element in the first row (0) and third column (2) of the letters
array.
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
cout << letters[0][2]; // Outputs "C"
Change Elements in a Multi-Dimensional Array
To change the value of an element, refer to the index number of the element in each of the dimensions:
Prepared by: Mr. John Carlo L. Azarcon Page 1 of 4
MANUEL V GALLEGO FOUNDATION COLLEGES, INC.
INSTITUTE OF INFORMATION AND COMPUTER TECHNOLOGY
Computer Programming 1 ICT113
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
letters[0][0] = "Z";
cout << letters[0][0]; // Now outputs "Z" instead of "A"
Loop Through a Multi-Dimensional Array
To loop through a multi-dimensional array, you need one loop for each of the array's dimensions.
The following example outputs all elements in the letters array:
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
cout<<letters[1][3];
column
Index number 0 1 2 3
row 0 A B C D
1 E F G H
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << letters[i][j] << "\n";
}
}
This example shows how to loop through a three-dimensional array:
string letters[2][2][2] = {
{
{ "A", "B" },
{ "C", "D" }
},
{
{ "E", "F" },
{ "G", "H" }
}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
cout << letters[i][j][k] << "\n";
}
}
}
Prepared by: Mr. John Carlo L. Azarcon Page 2 of 4
MANUEL V GALLEGO FOUNDATION COLLEGES, INC.
INSTITUTE OF INFORMATION AND COMPUTER TECHNOLOGY
Computer Programming 1 ICT113
A Three-Dimensional Array or 3D array in C is a collection of two-dimensional arrays. It can be
visualized as multiple 2D arrays stacked on top of each other.
We can declare a 3D array with x 2D arrays each having m rows and n columns using the syntax
shown below:
type arr_name[x][m][n];
• type: Type of data to be stored in each element.
• arr_name: name of the array
• x: Number of 2D arrays. (also called depth of the array)
• m: Number of rows in each 2D array.
• n: Number of columns in each 2D array.
Initialization in a 3D array is the same as that of 2D arrays. The difference is as the number of
dimensions increases so the number of nested braces will also increase.
To access an elements in 3D array, we use three indexes. One for depth, one for row and one for
column.
arr_name[d][i][j]
where, d, i and j are the indexes for depth (representing a specific 2D array.), the row within that 2D
array, and the column within that 2D array respectively.
To traverse the entire 3D array, you need to use three nested loops: an outer loop that goes through
the depth (or the set of 2D arrays), a middle loop goes through the rows of each 2D array and at last
an inner loop goes through each element of the current row.
Why Multi-Dimensional Arrays?
Multi-dimensional arrays are great at representing grids. This example shows a practical use for them.
In the following example we use a multi-dimensional array to represent a small game of Battleship:
// We put "1" to indicate there is a ship.
bool ships[4][4] = {
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 }
};
// Keep track of how many hits the player has and how many turns they have
played in these variables
int hits = 0;
int numberOfTurns = 0;
// Allow the player to keep going until they have hit all four ships
while (hits < 4) {
int row, column;
cout << "Selecting coordinates\n";
// Ask the player for a row
cout << "Choose a row number between 0 and 3: ";
Prepared by: Mr. John Carlo L. Azarcon Page 3 of 4
MANUEL V GALLEGO FOUNDATION COLLEGES, INC.
INSTITUTE OF INFORMATION AND COMPUTER TECHNOLOGY
Computer Programming 1 ICT113
cin >> row;
// Ask the player for a column
cout << "Choose a column number between 0 and 3: ";
cin >> column;
// Check if a ship exists in those coordinates
if (ships[row][column]) {
// If the player hit a ship, remove it by setting the value to zero.
ships[row][column] = 0;
// Increase the hit counter
hits++;
// Tell the player that they have hit a ship and how many ships are
left
cout << "Hit! " << (4-hits) << " left.\n\n";
} else {
// Tell the player that they missed
cout << "Miss\n\n";
}
// Count how many turns the player has taken
numberOfTurns++;
}
cout << "Victory!\n";
cout << "You won in " << numberOfTurns << " turns";
References:
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
Prepared by: Mr. John Carlo L. Azarcon Page 4 of 4