Mahmoud Derbi pfp
Mahmoud Derbi

@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 ๐Ÿš€
1 reply
2 recasts
3 reactions