C++矩阵乘法返回数组第一行和最后两行的指针

标签 c++ multidimensional-array matrix operator-overloading

我的 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++ 编码的实践类(class),这非常好:/

此外,这是一个合作伙伴的编程练习,如果代码不是 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.

最佳答案

更改为 void Matrix::operator=(const Matrix &m){ ....} 并删除“return *this”

关于C++矩阵乘法返回数组第一行和最后两行的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13777515/

相关文章:

c++ - 从公共(public)函数原型(prototype)到达私有(private)结构

c++ - 我的文件输出到屏幕两次 C++?

matlab - 提高许多子矩阵左除运算的性能(mldivide,\)

Android如何使用矩阵获取 ImageView 的边界

矩阵的 R 索引用于确定 R 中超过 8 小时的最大值的索引

c# - 如何实现二维矩阵的Kadane算法

c++ - 为什么 'make'改了一个文件就重新编译了几个文件?

c# - 如何找出最快的算法

swift - 访问从 Obj-C 桥接的二维数组时模糊使用下标

PHP多维数组搜索(按特定值查找键)