cpp / beginner
Snippet
2D Arrays: Tables of Data
A two-dimensional array is like a grid or table with rows and columns. The first index accesses the row, the second accesses the column. 2D arrays are perfect for representing matrices, game boards, or any tabular data structure.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>using namespace std;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++) {cout << matrix[i][j] << " ";}cout << endl;}cout << "Element [1][2]: " << matrix[1][2] << endl;return 0;}
Breakdown
1
int matrix[2][3] = {...}
Declares a 2D array with 2 rows and 3 columns initialized with values
2
matrix[i][j]
Accesses element at row i and column j