请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的Matrix是一个用于表示矩阵的类。其成员函数transpose的功能是实现矩阵的转置运算。 将矩阵A的行列互换,所得到的矩阵称为A的转置,记做AT。 例如,若有3×3矩阵

admin2015-06-27  20

问题 请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的Matrix是一个用于表示矩阵的类。其成员函数transpose的功能是实现矩阵的转置运算。
将矩阵A的行列互换,所得到的矩阵称为A的转置,记做AT
例如,若有3×3矩阵

请编写成员函数transpose,以实现矩阵转置功能。
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//Matrix.h
#include
#include
usingnamespacestd;
constintM=18;
constintN=18;
classMatrix{
intarray[M][N];
public:
Matrix(){}
intgetElement(inti,intj)const{
returnarray[j];)
voidsetElement(inti,intj,int
value){array[j]=value;)
voidtranspose();
voidshow(constchar。S)const
{
cout<for(inti=0;icout<for(intj=0;jcout<[j];
    }
  }
};
voidreadFromFile(constchar*,Matrix&);
voidwriteToFile(char*,constMatrix&);
//main.cpp
#include
#include"Matrix.h"
voidreadFromFile(constchar*
filename,Matrix&m)
{
ifstreaminfile(filename);
if(!infile){
cerr<<"无法读取输入数据文件!\n";
return;
}
intd;
for(inti=0;ifor(intj=0;jinfile>>d;
m.setElement(i,j,d);
  }
}
voidMatrix::transpose()
{
//********333********
//********666********
}
intmain()
{
Matrixm;
readFromFile("",m);
m.show("Beforetranspose:");
m.transpose();
m.show("Aftertranspose:");
writeToFile("",m);
return0;
}

选项

答案for(int i = 0; i < M; i++) for(int j = 0; j < i; j++) { int temp = array[i][j]; array[i][j] = array[j][i]; array[j][i] = temp; }

解析 主要考查考生对二维数组的掌握,程序用二维数组表示矩阵,编写矩阵转置功能。要实现矩阵的转置,只要使矩阵中的元素array[j]与array[j]交换,程序使用循环语句遍历矩阵元素,外层循环用于控制行下标,内层循环用于控制列下标。
转载请注明原文地址:https://jikaoti.com/ti/ngXiFFFM
0

最新回复(0)