编程题 编写程序,实现矩阵(3行列)的转置(即行列互换)
例如,输入如下的矩阵:
100 200 300
400 500 600
700 800 900
则程序输出:
100 400 700
200 500 800
300 600 900
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:#include <stdio.h>
#include <conio.h>
void fun(int array[3][3])
{
}
main()
{
int i,j;
int array[3][3]={{100,200,300},
{400,500,600},
{700,800,900}};
FILE *out;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%7d",array[i][j]);
printf("\n");
}
fun(array);
printf("Converted array:\n");
out = fopen("out.dat", "w");
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%7d",array[i][j]);
fprintf(out, "%7d",array[i][j]);
}
printf("\n");
fprintf(out, "\n");
}
fclose(out);
}
答案是: void fun(int array[3][3])
{
int i,j,temp;
for(i=0;i<3;i++)
for(j=0;j<I;j++)
{
temp=array[i][j];
array[i][j]=array[j][i];
array[j][i]=temp;
}
}