Transpose
Completed
Read an r×c matrix and print its transpose.
Input
r c, then r rows.
Output
c rows.
Example
Input
2 3 1 2 3 4 5 6
Output
1 4 2 5 3 6
Editor
C
Hints
Print column j of the original as row j.
Solution explanation
Element (i, j) becomes (j, i).
Show solution
#include <stdio.h>
int main() {
int r, c, m[10][10];
scanf("%d %d", &r, &c);
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
scanf("%d", &m[i][j]);
for (int j = 0; j < c; j++) {
for (int i = 0; i < r; i++) {
printf("%d%s", m[i][j], i < r - 1 ? " " : "\n");
}
}
return 0;
}