C two-dimensional arrays
Store a matrix, transpose it, and multiply it with another one.
On this page
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
#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;
}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
#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;}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
| Step / line | What happens | State after step | Output 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.