@mmderbi
๐ C++ Programming โ Part 16: Multi-Dimensional Arrays ๐ข
So far, weโve used one-dimensional arrays (lists).
Now, letโs level up with multi-dimensional arrays โ like tables or grids ๐ก
---
๐น What Are They?
A 2D array is basically an array of arrays.
Think of it as rows and columns ๐
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
This creates 2 rows ร 3 columns = 6 elements.
---
๐น Accessing Elements:
cout << matrix[0][2]; // 3
cout << matrix[1][1]; // 5
The first index = row, the second = column.
---
๐น Looping Through a 2D Array:
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
โ
Output:
1 2 3
4 5 6
---
๐น Why Use Them?
2D arrays are perfect for grids, game maps, matrices, or seating charts ๐ฎ
---
๐น Coming next (Part 17):
Weโll explore vectors โ dynamic arrays that grow and shrink automatically ๐