Learn two-dimensional arrays, nested element access, and the extension to three or more dimensions. Complete the array basics lesson first. Array basics →
Type name[rows][columns]; declares the array. int a[3][4]; // 3 rows × 4 columns = 12 int elements
a[row][column] is the access order; both indices start at zero . a[1][2] means the second row, third column: 7. for (int i = 0; i < 3; i++) // Outer loop: rows for (int j = 0; j < 4; j++) // Inner loop: columns printf("%d ", a[i][j]);
a[j][i] interchanges the row and column indices. Keep the outer loop for rows and the inner loop for columns to avoid confusion. int cube[2][3][4]; // 2 layers × 3 rows × 4 columns cube[Layer ][Row ][Column ] // Use three nested loops to visit all elements: for (int l = 0; l < 2; l++) for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) cube[l][i][j] = 0;
// 3 rows × 4 columns int m[3][4] = { { 1, 2, 3, 4}, { 5, 6, 7, 8}, { 9, 10, 11, 12} }; printf("%d\n", m[1][2]); // → 7 (row 1, column 2, zero-based)
// 2 layers × 3 rows × 4 columns int cube[2][3][4]; for (int l = 0; l < 2; l++) for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) cube[l][i][j] = l * 100 + i * 10 + j;
int m[3][4] = {0}; initializes all elements to zero. In int m[][4] = {{1,2,3,4},{5,6,7,8}};, only the outermost dimension is omitted; the column count is still required.int m[3][4]; sizeof(m) // 48 (3 * 4 * 4 bytes) sizeof(m[0]) // 16 (one row = 4 * 4 bytes) sizeof(m[0][0]) // 4 (one int) // Idioms for element counts: // Rows = sizeof(m) / sizeof(m[0]) // Columns = sizeof(m[0]) / sizeof(m[0][0])
int table[9][9]: fill this array with the 1–9 multiplication table and print it. table[i][j] = (i+1) * (j+1) is the assignment to perform in nested loops. int a[3][4] with values of your choice. Calculate and print the sum of each row, using an outer row loop and an inner column loop.int A[3][4], create int B[4][3]. Transpose with B[j][i] = A[i][j] and print the result.Check your understanding of this lesson!