思路:定了两个二维数组分别存储转置前后的矩阵,接着for循环依次转置即可。
//参考代码#include<stdio.h>
int main()
{
int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}},b[4][3]
int i,j
for(i=0i<3i++)
for(j=0j<4j++)
b[j][i]=a[i][j]
for(i=0i<4i++)
{
for(j=0j<3j++)
printf("%2d ",b[i][j])
printf("\n")
}
return 0
}
/*
运行结果:
1 5 9
2 6 10
3 7 11
4 8 12
*/
#include<stdio.h>
int main()
{
int i,j,a[3][5],b[5][3]
//..
for(i=0i<3i++)
for(j=0j<5j++)
scanf("%d",&a[i][j])
//..
for(i=0i<5i++)
for(j=0j<3j++)
b[i][j]=a[j][i]
//..
for(i=0i<5i++)
{
for(j=0j<3j++)
printf("%d ",b[i][j])
printf("\n")
}
return 0
}
用一个双重for循环,行、列下标对调逐一拷贝就完成了。举例代码如下:
//#include "stdafx.h"//If the vc++6.0, with this line.#include "stdio.h"
int main(void){
int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}},va[4][3],i,j
for(i=0i<3i++)//转置
for(j=0j<4va[j][i]=a[i][j++])
for(i=0i<4i++){//输出
for(j=0j<3printf("%3d",va[i][j++]))
printf("\n")
}
return 0
}