C two-dimensional arrays
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].
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;
}