## Question

## Transcribed Text

## Solution Preview

* File: Matrix.cpp

* Author:

*

*

*/

#include "Matrix.h"

#include <iomanip>

using namespace std;

Matrix::Matrix(int rows, int columns) {

this->rows = rows;

this->columns = columns;

p = new double * [rows];

// set up the dynamic array

for (int i = 0; i < rows; i++) {

p[i] = new double[columns];

}

}

/**

* copy constructor

* @param o

*/

Matrix::Matrix(const Matrix& o) {

copy(o);

}

/**

* destructor

*/

Matrix::~Matrix() {

clean();

}

bool operator!=(const Matrix& m1, const Matrix& m2) {

// bad case

if (m1.columns != m2.columns

|| m1.rows != m2.rows) {

return true;

}

// compare content of the array

for (int i = 0; i < m1.rows; i++) {

for (int j = 0; j < m1.columns; j++) {

if (m1.p[i][j] != m2.p[i][j]) {

return true;

}

}

}

return false;

}

Matrix Matrix::operator*(const Matrix& m) {

// bad case

if (this->columns != m.rows) {

throw "Error, columns of A doesn't equal to rows of B";

}

Matrix n(this->rows, m.columns);

// get the product

for (int i = 0; i < n.rows; i++) {

for (int j = 0; j < n.columns; j++) {

n.p[i][j] = 0.0;

for (int k = 0; k < this->columns; k++) {

n.p[i][j] += this->p[i][k] * m.p[k][j];

}

}

}

// return it

return n;

}

Matrix& Matrix::operator*=(const Matrix& m) {

Matrix tmp;

// get product

tmp = (*this) * m;

// reset current data

this->clean();

// copy data to this object

this->copy(tmp);

return *this;

}

Matrix Matrix::operator+(const Matrix& m) {

// bad case

if (this->rows != m.rows

|| this->columns != m.columns) {

throw "ERROR the dimension doesn't match";

}...

By purchasing this solution you'll be able to access the following files:

Solution.zip.