我的C ++作业有一个非常令人毛骨悚然的问题。
我们的任务是使用动态内存分配在C ++中执行矩阵乘法。在大多数情况下,该程序可以正常运行,但BUT,赋值运算符以及(也许)矩阵乘法似乎无法正常工作。
基本上,我的程序在运行时可以毫无问题地执行矩阵乘法,但是返回的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:命令行输出。请注意,我正在通过Windows上的Code :: Blocks使用GCC进行编译和运行
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.
最佳答案
更改为此void Matrix :: operator =(const Matrix&m){....},然后删除“返回* this”