2 u012955033 u012955033 于 2014.03.25 16:49 提问

c++求两个矩阵的乘法,但运行出来的结果是一些不可预料的值

#include
#include
using namespace std;
class CreateMatrix
{
private:
int m,n,**p;
public:
void SetMatrix(int m,int n)
{
p=new int *[m]; //build m pointers and save m rows
for(int i=0;i if((p[i]=new int [n])==NULL)
exit(0);
for(int i=0;i {
for(int j=0;j cin>>p[i][j];
}
cout<<"the Matrix of you input:"<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cout<<p[i][j]<<"\t";
cout<<endl;
}
}
void DelMatrix(int m)
{
for(int i=0;i<m;i++)
delete[]p[i];
delete[]p;
}
friend void MatrixMul(CreateMatrix &E,CreateMatrix &F,int m,int n,int t);
};

void MatrixMul(CreateMatrix &E,CreateMatrix &F,int m,int n,int t)
{
int **q;
q=new int *[m]; //build m pointers and save m rows
for(int i=0;i<m;i++)
if((q[i]=new int [n])==NULL)
exit(0);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
for(int k=0;k<t;k++)
q[i][k]=q[i][k]+E.p[i][j]*F.p[j][k];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cout<<q[i][j]<<" ";
}

int main()
{
CreateMatrix A,B;
int a,b,c,d;
cout<<"you must ensure b=c,that is A_matrix's column equals B_matrix's row"< cout cin>>a>>b;
cout<<"input A_matrix elements:"< A.SetMatrix(a,b);
cout cin>>c>>d;
cout<<"input B_matrix elements:"<<endl;
B.SetMatrix(c,d);
MatrixMul(A,B,a,d,b);
system("pause");
return 0;
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!