Lesson 13: Two-dimensional arrays
Last lesson we met an array — a single row of cells. But much real-world data is tables: a chess board, a timetable, an image. int m[2][3]; defines a table of 2 rows and 3 columns — 6 int cells. You reach a cell with two indices: m[i][j] — row first (i), then column (j), both starting at 0. To visit
A 2D array is like a cinema hall: to find your place you need two numbers — row and seat. 'Row 1, seat 2' is m[1][2], and just like in C, if rows are numbered from 0 — row 1 is actually the second row.
- two-dimensional array
- A table of cells: int m[2][3]; — 2 rows, 3 columns, 6 cells.
- row
- The first dimension. In m[i][j], index i selects a row (from 0).
- column
- The second dimension. In m[i][j], index j selects a column within the row (from 0).
- nested loops
- A loop inside a loop: the outer one over rows, the inner one over each row's columns.
- nested initializer
- Inner curly braces per row: {{1, 2, 3}, {4, 5, 6}}.