矩阵转置
C 实现
#include<stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main(){
int i, j, t;
printf("Before transposition:
");
for (i = 0; i < 4; ++i){
for (j = 0; j < 4; ++j){
printf ("%d ", M[i][j]);
}
printf("
");
}
for (i = 0; i < 4; ++i){
for(j = i;j < 4; ++j){
t = M[i][j];
M[i][j] = M[j][i];
M[j][i] = t;
}
}
printf("After transposition:
");
for (i = 0; i < 4; ++i){
for (j = 0; j < 4; ++j){
printf ("%d ", M[i][j]);
}
printf("
");
}
}
运行结果