1. Intro
A multidimensional array is simply an array of arrays. The most common form is the 2D array, which can be thought of as a table or grid with rows and columns — useful for representing matrices, game boards, or tabular data. C++ also supports 3D and higher-dimensional arrays, though 2D arrays are by far the most frequently used in everyday code.
Cricket analogy: A Duckworth-Lewis resource table is a grid of overs-remaining rows against wickets-lost columns — exactly the row-and-column structure of a 2D array, with rarer 3D tables adding a pitch-condition dimension.
2. Syntax
dataType arrayName[rowSize][columnSize];
// Example: a 3x4 grid (3 rows, 4 columns)
int grid[3][4];3. Explanation
int grid[3][4]; conceptually creates 3 rows, each containing 4 columns — 12 int elements in total. Even though we think of it as a table, C++ stores a 2D array in a single contiguous block of memory, arranged in row-major order: the entire first row is stored, followed immediately by the entire second row, and so on. An element is accessed using two indices, grid[row][col], both zero-based, so grid[0][0] is the top-left element and grid[2][3] is the bottom-right element of a 3x4 grid. You typically use nested for loops — an outer loop for rows and an inner loop for columns — to fill or traverse a 2D array.
Cricket analogy: A int grid[3][4] scoring table for 3 innings by 4 quarters stores all 12 cells contiguously, first innings-one's four quarters, then innings-two's, row-major — grid[0][0] is the first quarter scored, grid[2][3] the last, filled with a nested loop over innings then quarters.
4. Example
#include <iostream>
using namespace std;
int main() {
int grid[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 3; col++) {
cout << grid[row][col] << " ";
}
cout << endl;
}
return 0;
}5. Output
1 2 3
4 5 6 6. Key Takeaways
- A multidimensional array is an array of arrays; a 2D array models rows and columns.
- Declare a 2D array with
dataType name[rows][cols];, and access elements withname[row][col]. - C++ stores 2D arrays contiguously in row-major order — the whole first row, then the whole second row, and so on.
- Nested
forloops (outer for rows, inner for columns) are the standard way to fill or traverse a 2D array.
Practice what you learned
1. What does `int grid[3][4];` create?
2. In what order does C++ store the elements of a 2D array in memory?
3. For `int grid[2][3]`, which index accesses the element in the first row, second column?
4. How is a 2D array best conceptually described?
5. What loop structure is typically used to traverse every element of a 2D array?
Was this page helpful?
You May Also Like
Arrays in C++
Learn what an array is in C++, how to declare and access a fixed-size collection of same-type elements, and why zero-based indexing matters.
Array Initialization in C++
Learn the different ways to initialize a C++ array at declaration, including partial initialization, zero-filling, and omitting the size.
Passing Arrays to Functions in C++
Understand how arrays decay to pointers when passed to C++ functions, why size must be passed separately, and how this differs from pass-by-value.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics