一道C++矩阵题,请大佬帮看下,错误的地方。。。
1) 编写整数矩阵运算的程序 a) 编写 2 个整数矩阵(不一定是方阵)的相乘的函数,矩阵中的元素值可以是在程序中初始化好的,也可以是随机数;
b) 编写矩阵转置的函数;
c) 编写矩阵元素输出的函数;
在 main 函数中,编写测试代码。
#include <iostream>
using namespace std;
#define MATRIX_LENTH 3
#define MATRIX_WIDRH 3
class CMatrix
{
public:
CMatrix(int Lenth ,int Width);
~CMatrix();
void OutPut();
void TransposeMatrix();
void MultiplyMatrix();
protected:
private:
int m_Lenth;
int m_Width;
int *m_pMatrix;
int *m_pAncillaryMatrix;
private:
//void Multiply
};
CMatrix::CMatrix(int Lenth ,int Width)
{
m_pMatrix = new int[Lenth*Width];
for (int i = 0; i < Lenth * Width; i++)
{
m_pMatrix[i] = rand();
}
}
CMatrix::~CMatrix()
{
delete[] m_pMatrix;
}
void CMatrix::OutPut()
{
for (int i = 0; i < m_Lenth; i++)
{
for (int j = 0; j < m_Width; j++)
{
cout<<m_pAncillaryMatrix[i][j] << " ";
}
cout << endl;
}
}
void CMatrix::TransposeMatrix()
{
for (int i = 0; i < m_Lenth; i++)
{
for (int j = 0; j < m_Width; j++)
{
m_pAncillaryMatrix[i][j] = m_pMatrix[j][i];
}
}
}
void CMatrix::MultiplyMatrix(int *InputMatrix)
{
int v1 = 0;
int v2 = 0;
for (int i = 0; i < m_Lenth; i++)
{
for (int j = 0; j < m_Width; j++)
{
for (int k = 0; k < Lenth; k++)
{
v2 = m_pMatrix[i][v1]*InputMatrix[v1++][j]
}
m_pAncillaryMatrix[i][j] = v2;
}
}
}
int main()
{
CMatrix cMatrix(MATRIX_LENTH , MATRIX_WIDTH);
int *Matrix = new int[MATRIX_LENTH*MATRIX_WIDTH];
for (int i = 0; i < MATRIX_LENTH * MATRIX_LENTH; i++)
{
m_pMatrix[i] = rand();
}
cout << "原矩阵:" << endl;
cMatrix.OutPut();
cout << "转置矩阵:" << endl;
cMatrix.TransposeMatrix();
cMatrix.OutPut();
cout << "输入矩阵:" << endl;
for (int i = 0; i < Lenth; i++)
{
for (int j = 0; j < Width; j++)
{
cout<<Matrix[i][j] << " ";
}
cout << endl;
}
cout << "相乘后的矩阵:" << endl;
cMatrix.MultiplyMatrix(Matrix);
cMatrix.OutPut();
return 0;
}