MCS-201 · Unit 4

Arrays and Strings

Section 5 of 9

Block 1 · Unit 4Arrays and Strings7 min · Section 5 of 9

4.5Multi-Dimensional Arrays

Covers IGNOU MCS-201 §4.6, 4.6.1, 4.6.2

Much real data is a table: marks of several students in several subjects, a chessboard, a spreadsheet. A two-dimensional array stores such data with one index for the row and one for the column.

Two-dimensional arrays

Syntax
data-type array-name[rows][columns];

int table[2][3]; has 2 rows and 3 columns, 2 × 3 = 6 elements, with row indices 0–1 and column indices 0–2. An m × n array holds m × n elements and needs

m×n×size of one elementm \times n \times \text{size of one element}

bytes. A chessboard is int board[8][8]; with 64 elements. Arrays with more dimensions are declared the same way, for example int cube[3][4][5];.

Row-major storage

C stores a multi-dimensional array row by row in consecutive memory, called row-major order: all of row 0, then all of row 1, and so on. The last subscript changes fastest.

Memory layout of int table[2][3]
table[0][0] table[0][1] table[0][2] table[1][0] table[1][1] table[1][2]
|<------------ row 0 ----------->| |<------------ row 1 ----------->|

Initialisation

DeclarationRow 0Row 1
int t[2][3] = {1, 2, 3, 4, 5, 6};1 2 34 5 6
int t[2][3] = {{1, 2, 3}, {4, 5, 6}};1 2 34 5 6
int t[2][3] = {{1, 2, 3}, {4}};1 2 34 0 0
int t[][3] = {{1, 2, 3}, {4, 5, 6}};1 2 34 5 6

Inner braces group the values of one row; missing values in a row become 0. Only the first dimension may be left empty; the number of columns must always be given.

Key takeaways

  • type a[rows][cols]; holds rows × cols elements; both indices start at 0.
  • Storage is row-major: row 0 first, then row 1, and so on.
  • Inner braces initialise one row each; missing values are 0.
  • Process with nested loops: rows outside, columns inside.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many elements does int m[4][6]; have?

Show answer

Answer: B. 24

Rows × columns = 4 × 6 = 24. Adding the sizes (10) is a common slip.

Question 2 · multiple choice

For int t[2][3] = {{1, 2, 3}, {4}};, what is t[1][2]?

Show answer

Answer: D. 0

Row 1 lists only one value, so its remaining elements are set to 0.

Question 3 · multiple choice

In row-major order, which element is stored immediately after a[0][2] in int a[2][3];?

Show answer

Answer: C. a[1][0]

Row 0 ends at column 2, so the next element in memory is the start of row 1. a[0][3] does not exist.

Question 4 · multiple choice

Which declaration is valid?

Show answer

Answer: B. int t[][2] = {{1, 2}, {3, 4}};

Only the first dimension may be omitted; the compiler needs the column count to locate each row.

Question 5 · multiple choice

What does this program print?

C · diag.c
#include <stdio.h>

int main(void)
{
    int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int i, s = 0;
    for (i = 0; i < 3; i++)
        s += a[i][i];
    printf("%d\n", s);
    return 0;
}
Show answer

Answer: C. 15

a[i][i] selects the diagonal elements 1, 5 and 9, whose sum is 15. 45 is the sum of all nine elements.

Question 6 · short answer

Write nested loops to add two 2 × 3 integer matrices a and b into c, and compute c[1][2] when a[1][2] is 6 and b[1][2] is 9.

Show worked answer

for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) c[i][j] = a[i][j] + b[i][j];. The outer loop visits the 2 rows and the inner loop the 3 columns, so each of the 6 element pairs is added once. For row 1, column 2: c[1][2] = 6 + 9 = 15.

↑ ↓ move↵ openTab switch type