Kodewick
Start learning →
Starter←→0 / 50

Transpose a matrix

#039Starterb[j][i] = a[i][j]5 min

To transpose a matrix, rows become columns: the number at row i, column j goes to row j, column i of the new matrix. a has 2 rows and 3 columns, so b has 3 rows and 2 columns. The first row of a, 1 2 3, becomes the first column of b. Finish the line in the loop; the program shows b row by row.

Should show
1 4
2 5
3 6
main.c
#include <stdio.h>

int main(void) {
    int a[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };
    int b[3][2] = {0};
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            // your code here: put a[i][j] in its transposed place in b
        }
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) printf(j ? " %d" : "%d", b[i][j]);
        printf("\n");
    }
    return 0;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.