假定,不一定是方阵,那么 转置前是 irow行,icol列, 转置后是icol行, irow列。
输入数组x, 输出数组y.
程序如下:
#include
void fun(int *x, int *y, int irow, int icol){
int i,j;
for (j=0;j
}
int main(){
int a[3][4]={{11,12,13,14},{21,22,23,24} ,{31,32,33,34}};
int b[4][3];
int i,j,nr=3,nc=4;
fun( &a[0][0], &b[0][0],nr,nc);
for (j=0;j<4;j++){
for (i=0;i<3;i++) printf("%d ",b[j][i]);
printf("\n");
}
return 0;
}
输出:
11 21 31
12 22 32
13 23 33
14 24 34
#include "stdio.h"
void transpose(int a[][4],int n){
int i,j,k;
for(i=0;ifor(j=0;j k=a[i][j],a[i][j]=a[j][i],a[j][i]=k;
}
int main(int argc,char *argv[]){
int a[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}},i,j;
printf("Before the transpose:\n");
for(i=0;i<4;i++){
for(j=0;j<4;printf("%3d",a[i][j++]));
printf("\n");
}
transpose(a,4);
printf("After the transpose:\n");
for(i=0;i<4;i++){
for(j=0;j<4;printf("%3d",a[i][j++]));
printf("\n");
}
return 0;
}
运行结果: