Answer to Question #316825 in C++ for Ashiir

Question #316825

Write a program in C++take two matrix and compute their addition subtraction and multiplication. With output

1
Expert's answer
2022-03-23T13:44:36-0400
#include <iostream>
#include <iomanip>
using namespace std;


class Matrix {
public:
    Matrix(int n=3, int m=3);
    Matrix(int n, int m, int arr[]);
    Matrix(const Matrix& m);
    ~Matrix();
    Matrix& operator=(const Matrix& rhs);
    Matrix operator+(const Matrix& rhs) const;
    Matrix operator-(const Matrix& rhs) const;
    Matrix operator*(const Matrix& rhs) const;
    int& operator()(int i, int j) const {
        return data[i*col + j];
    }
    void print(ostream& os, int w=0) const;
    friend ostream& operator<<(ostream&, const Matrix&);


private:
    int row;
    int col;
    int *data;
};


Matrix::Matrix(int n, int m) {
    row = n;
    col = m;
    data = new int[m*n];
}


Matrix::Matrix(int n, int m, int arr[]) {
    row = n;
    col = m;
    data = new int[m*n];


    for (int i=0; i<row*col; i++) {
        data[i] = arr[i];
    }
}


Matrix::Matrix(const Matrix& m) {
    row = m.row;
    col = m.col;
    data = new int[row*col];


    for (int i=0; i<row*col; i++) {
        data[i] = m.data[i];
    }
}


Matrix::~Matrix() {
    delete [] data;
}


Matrix& Matrix::operator=(const Matrix& rhs) {
    if (this == &rhs) {
        return *this;
    }


    delete [] data;
    row = rhs.row;
    col = rhs.col;
    data = new int[row*col];


    for (int i=0; row*col; i++) {
        data[i] = rhs.data[i];
    }
    return *this;
}


Matrix Matrix::operator+(const Matrix& rhs) const {
    Matrix result(row, col);


    for (int i=0; i<row*col; i++) {
        result.data[i] = data[i] + rhs.data[i];
    }
    return result;
}


Matrix Matrix::operator-(const Matrix& rhs) const {
    Matrix result(row, col);


    for (int i=0; i<row*col; i++) {
        result.data[i] = data[i] - rhs.data[i];
    }
    return result;
}


Matrix Matrix::operator*(const Matrix& rhs) const {
    Matrix result(row, rhs.col);


    for (int i=0; i<row; i++) {
        for (int j=0; j<rhs.col; j++) {
            result(i, j) = 0;
            for (int k=0; k<col; k++) {
                result(i, j) += (*this)(i, k) * rhs(k, j);
            }
        }
    }
    return result;
}


void Matrix::print(ostream& os, int w) const {
    for (int i=0; i<row; i++) {
        for (int j=0; j<col; j++) {
            if (w) {
                os << setw(w);
            }
            os << (*this)(i, j);
        }
        os << endl;
    }
}


ostream& operator<<(ostream& os, const Matrix& m) {
    m.print(os, 3);
    return os;
}


int main() {
    int arr1[] = {1, 2, 3, 4, 5, 6};
    int arr2[] = {1, 1, 1, 2, 2, 2};
    int arr3[] = {1, 2, 2, 1, 1, 2};
    Matrix A(2, 3, arr1);
    Matrix B(2, 3, arr2);
    Matrix C(3, 2, arr3);


    cout << "A:" << endl;
    cout << A << endl;
    cout << "B:" << endl;
    cout << B << endl;
    cout << "A + B" << endl;
    cout << A + B << endl;
    cout << " A - B" << endl;
    cout << A - B << endl;


    cout << "A * C" << endl;
    cout << A * C << endl;


    return 0;


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog