QuestionQuestion

Transcribed TextTranscribed Text

Create a class called Marix for performing arithmetic with matrices. The class has the private members rows, columns represent the matrix's dimensions and **p a double pointer of type double. The class has the public members set( ). print( ). getDimensions( ). getPointer( ). and a default parameterize constructor, a destructor, and a copy constructor. A matrix of dimensions n x IN has the form an a --- an : --- : a2m : an anz --- a, Overload the - *- and - matrix operations as members of the class Matrix. Overload the ,>>, << matrix operations as non-member functions of the class Matrix Sample input / output: Enter the elements of row number 1: 1 2 3 Enter the elements of row number 2: 4 5 6 Enter the elements of row number 3:789 Enter the elements of row number 1:321 Enter the elements of row number 2:654 Enter the elements of row number 3:987 The first matrix is: 1 2 3 4 5 6 7 8 9 The second matrix is: 3 2 1 6 5 4 9 8 7 The sum of the two matrices is: 4 4 4 10 10 10 16 16 16 The subtraction of the two matrices is: -2 2 -2 2 -2 2 The product of the two matrices is: 42 36 30 96 81 66 150 126 102 The new value of the first matrix is: 42 36 30 96 81 66 150 126 102 The two matrices are not equal The two matrices are not equal

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/*
* 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.

$50.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats