Multidimensional Arrays in C++

Multidimensional arrays are arrays of arrays. The most common are two-dimensional arrays, which can be thought of as tables with rows and columns.

Example: Declaring a 2D Array

int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

Accessing Elements in a 2D Array

Example: Printing a 2D Array

#include <iostream>

int main() {
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            std::cout << matrix[i][j] << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}

Key Takeaways

  • Multidimensional arrays store data in multiple dimensions.
  • Access elements using multiple indices array[i][j].
  • Nested loops are commonly used to traverse multidimensional arrays.