bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/C++/C++ Tutorial
C++•C++ Tutorial

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.

Example

string letters[2][4] = { { "A", "B", "C", "D" }, { "E", "F", "G", "H" }
};
cout << letters[0][2];  // Outputs "C"

Remember that: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

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:

Example

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:

Example

string letters[2][4] = { { "A", "B", "C", "D" }, { "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:

Example

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";
    }
}
}

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:

Example

// 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: ";
 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";

Previous

C++ Array Size

Next

C++ Structures (struct)

This chapter

Overview
64

Lessons

138m

Read time

1. C++ Tutorial2. C++ Introduction3. C++ Getting Started4. C++ Syntax5. C++ Statements6. C++ Output (Print Text)7. C++ Output Numbers8. C++ New Lines9. C++ Comments10. C++ Variables11. C++ Declare Multiple Variables12. C++ Identifiers13. C++ Constants14. C++ User Input15. C++ Data Types16. C++ Numeric Data Types17. C++ Boolean Data Types18. C++ Character Data Types19. C++ String Data Types20. C++ auto21. C++ Operators22. C++ Arithmetic Operators23. C++ Assignment Operators24. C++ Comparison Operators25. C++ Logical Operators26. C++ Operator Precedence27. C++ Strings28. C++ String Concatenation29. C++ Numbers and Strings30. C++ String Length31. C++ Access Strings32. C++ Special Characters33. C++ User Input Strings34. C++ String Namespace35. C++ C-Style Strings36. C++ Math37. C++ Booleans38. C++ Boolean Expressions39. C++ If ... Else40. C++ Else41. C++ Else If42. C++ Short Hand If Else43. C++ Nested If44. C++ Logical Operators in Conditions45. C++ Switch46. C++ While Loop47. C++ Do/While Loop48. C++ For Loop49. C++ Nested Loops50. C++ The foreach Loop51. C++ Break and Continue52. C++ Arrays53. C++ Arrays and Loops54. C++ Omit Array Size55. C++ Array Size56. C++ Multi-Dimensional Arrays57. C++ Structures (struct)58. C++ Enumeration (enum)59. C++ Memory Address60. C++ Pointers61. C++ Dereference62. C++ Modify Pointers63. C++ Memory Management64. C++ new and delete

On this page

Multi-Dimensional ArraysAccess the Elements of a Multi-Dimensional ArrayChange Elements in a Multi-Dimensional ArrayLoop Through a Multi-Dimensional ArrayWhy Multi-Dimensional Arrays?