C · Course navigation

C two-dimensional arrays

Store a matrix, transpose it, and multiply it with another one.

On this page
Before you start

C arrays and nested loops.

  • Store a matrix, transpose it, and multiply it with another one.
  • Predict the sample output and explain the state changes.
  • Complete the practice task and check the boundary cases.

The concept

A two-dimensional array in C is an array of arrays, stored row by row in one block of memory. int a[2][3] has 2 rows and 3 columns, and a[r][c] is the element in row r and column c. When an array is passed to a function, all dimensions except the first must be given, so the compiler can compute the address of a[r][c].

Transposing swaps rows and columns: t[c][r] = a[r][c]. The product of an m×n matrix and an n×p matrix is an m×p matrix whose entry (i, j) is the sum over k of a[i][k] times b[k][j]. Multiplying a matrix by its own transpose gives a symmetric matrix of dot products of the rows.

Code example

C
#include <stdio.h>

int main(void) {
  int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
  int t[3][2];

  for (int r = 0; r < 2; r++) {
    for (int c = 0; c < 3; c++) {
      t[c][r] = a[r][c];
    }
  }
  printf("transpose:\n");
  for (int r = 0; r < 3; r++) {
    printf("%d %d\n", t[r][0], t[r][1]);
  }

  int p[2][2] = {{0, 0}, {0, 0}};
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 3; k++) {
        p[i][j] += a[i][k] * t[k][j];
      }
    }
  }
  printf("a times its transpose:\n");
  for (int i = 0; i < 2; i++) {
    printf("%d %d\n", p[i][0], p[i][1]);
  }
  return 0;
}
Output
transpose:
1 4
2 5
3 6
a times its transpose:
14 32
32 77

Follow the code

Open the walkthrough to inspect the example one step at a time.

Show guided walkthrough
Guided walkthroughReady
1#include <stdio.h>
2
3int main(void) {
4 int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
5 int t[3][2];
6
7 for (int r = 0; r < 2; r++) {
8 for (int c = 0; c < 3; c++) {
9 t[c][r] = a[r][c];
10 }
11 }
12 printf("transpose:\n");
13 for (int r = 0; r < 3; r++) {
14 printf("%d %d\n", t[r][0], t[r][1]);
15 }
16
17 int p[2][2] = {{0, 0}, {0, 0}};
18 for (int i = 0; i < 2; i++) {
19 for (int j = 0; j < 2; j++) {
20 for (int k = 0; k < 3; k++) {
21 p[i][j] += a[i][k] * t[k][j];
22 }
23 }
24 }
25 printf("a times its transpose:\n");
26 for (int i = 0; i < 2; i++) {
27 printf("%d %d\n", p[i][0], p[i][1]);
28 }
29 return 0;
30}

Follow the example

Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.

View the complete dry-run table
Prepared trace of the published program. Some rows group repeated iterations.
Step / lineWhat happensState after stepOutput so far
Two rows of three columns, stored row by row in memory.—
—
Swap the row and column indexes.—
—
The 3×2 transpose: the columns of a become rows.—
transpose:
1 4
2 5
3 6
Entry (i, j) is the dot product of row i of a and column j of t. For (0, 0): 1·1 + 2·2 + 3·3 = 14.—
transpose:
1 4
2 5
3 6
Row 0 dot row 0 is 14, row 0 dot row 1 is 32, and row 1 dot row 1 is 77.—
transpose:
1 4
2 5
3 6
a times its transpose:
14 32
32 77

Behind the scenes

How C processes this code

The C compiler produces a native program. Variables occupy memory, and execution enters main.

Cost and common mistakes

Transpose O(m·n). Multiplication O(m·n·p).

  • C does not check array bounds. Writing t[3][0] on a 3-row array silently corrupts memory.
  • The accumulator p must start at zero. The initializer sets every entry to 0.
  • Matrix multiplication is not commutative: a × b is generally different from b × a, and the sizes must match.

Try it yourself

What are the dimensions of the product of a 2×3 matrix and a 3×4 matrix?

Reveal answer and reasoning

2×4: the result has the row count of the first matrix and the column count of the second.

Further reading