我的 C++ 作业有一个非常令人恼火的问题。
我们的任务是使用动态内存分配在 C++ 中执行矩阵乘法。在大多数情况下,该程序运行良好,但赋值运算符以及(可能)矩阵乘法似乎不起作用。
基本上,我的程序在运行时执行矩阵乘法没有问题,但是返回的 Matrix 对象似乎并没有表现得那么好!
这是 Matrix.h 文件的相关代码
class Matrix
{
//Pointer matrix
int **matrix;
//height and width of the matrix
int height;
int width;
//declaring all public functions to be included in the Matrix object
public:
Matrix();
Matrix(int, int);
~Matrix();
void constructor(int, int);
void destruct();
int getHeight() const;
int getWidth() const;
int getElement(int, int) const;
void setElement(int, int, int);
int* getColumn(int) const;
void setColumn(int, int*);
int* getRow(int) const;
void setRow(int, int*);
void fillMatrix(int);
Matrix& operator=(const Matrix &m);
const Matrix operator+(const Matrix &m);
const Matrix operator-(const Matrix &m);
const Matrix operator*(const Matrix &m);
bool operator==(const Matrix &m);
bool operator!=(const Matrix &m);
};
Matrix::Matrix()
{
matrix = new int*[1];
}
Matrix::Matrix(int x, int y)
{
constructor(x,y);
}
Matrix::~Matrix()
{
destruct();
}
/**
This function contructs the matrix using given heights and widths
(for colums and rows) and then allocates the memory for this
object
*/
void Matrix::constructor (int matrixHeight, int matrixWidth)
{
height = matrixHeight;
width = matrixWidth;
matrix = new int *[matrixHeight];
for ( int i = 0; i < matrixHeight; i++)
{
matrix[i] = new int[matrixWidth];
}
//initialising all areas in array
for ( int i = 0; i < matrixHeight; i++)
{
for ( int j = 0; j < matrixWidth; j++)
{
matrix[i][j] = 1;
}
}
}
/**
This function is for freeing any assigned memory to a matrix
for its columns and rows
*/
void Matrix::destruct ()
{
for ( int i = 0 ; i < getHeight() ; i++ )
{
delete [] matrix[i];
}
delete [] matrix;
matrix = NULL;
}
那是第一节,这里是矩阵乘法
/**
This function overloads the times operator
*/
const Matrix Matrix::operator*(const Matrix &m)
{
if(getWidth() == m.getHeight())
{
//i cant remember which way to do matrix mult, and im intoxicated
//so heres a shitty implementation
Matrix matrixResult(getHeight(), m.getWidth());
int result = 0;
int *row;
int *column;
cout << "\n\n matrix mult \n\n";
for ( int i = 0 ; i < getHeight() ; i++ )
{
for ( int j = 0 ; j < m.getWidth() ; j++ )
{
result = 0;
row = getRow(i);
column = m.getColumn(j);
for( int k = 0; k < m.getHeight() ; k++ )
{
result = result + (row[k] * column[k]);
}
matrixResult.setElement(i, j, result);
}
cout << "\n";
}
for ( int i = 0 ; i < getHeight() ; i++ )
{
for ( int j = 0 ; j < m.getWidth() ; j++ )
{
cout << std::setw(10) << matrixResult.getElement(i,j);
}
cout << "\n";
}
return matrixResult;
}
cout << "Not correct size to be multiplied";
return *this;
}
这是等号(这是我认为我的错误所在):
/**
Overloading the equals operator to be of
type Matrix. This function changes all
values of the currently instantiated matrix
to those of the one passed in via parameter
*/
Matrix& Matrix::operator=(const Matrix &m)
{
//destroy previous matrix value array
destruct();
//Create a new array the same size as the matrix to be copied
constructor(m.getHeight(), m.getWidth());
//looping through the current array
cout << "\n\n beginning equals\n\n";
for ( int i = 0 ; i < getHeight() ; i++ )
{
for ( int j = 0 ; j < getWidth() ; j++ )
{
//changing current array's elements to
//those of the paramter array
cout << std::setw(10) << m.getElement(i,j);
setElement(i, j, m.getElement(i, j));
}
cout << "\n";
}
return *this;
}
大家都知道,我对 C++ 完全陌生,不幸的是,我们的讲师似乎坚持不给我们任何关于 C++ 编码的实践课程,这非常棒:/
此外,这是一个合作伙伴的编程练习,如果代码不是 100% 符合标准,请道歉,但其中一些不是我自己的!
谢谢!
编辑:按要求实施 getColumn。
/**
This function returns the items in a specified column
within the matrix. The values are returns as a 1 dimensional
array, which has been malloc'ed.
*/
int* Matrix::getColumn(int col) const
{
//using malloc to assign the memoru for the returned array
int* result = new int[height];
//check to ensure column was within matrix range
if (col < width )
{
for( int i = 0 ; i < height ; i++ )
{
result[i] = getElement(i, col);
}
}
return result;
}
编辑 2:命令行输出。请注意,我正在使用 GCC 通过 CODE::BLOCKS 在 WINDOWS 上编译和运行它
913 918 -418
676 736 -25
-225 265 -382
890 -739 -452 87
-331 -564 -761 280
-375 -895 101 709
829 -661 742 442
679 567 377 -322
-356 -136 -614 -356
matrix mult
665462 -818349 -1153492 40109
367399 -892293 -868173 247167
-144715 358705 -138547 -216213
beginning equals
197792 20092813134236931095717471
200928 199968 2003201701603654
-144715 358705 -138547 -216213
Process returned 0 (0x0) execution time : 0.009 s
Press any key to continue.