For this homework exercise you will be exploring the implementation of matrix mu
ID: 3700498 • Letter: F
Question
For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called Matrix a. Private Members i. int rows; ii. int cols; ii. double" data; b. Provide NO default constructor c. Constructor i. Matrix(int,int)takes rows and cols ii. Inside constructor allocate and initialize data d. Destructor i. Matrix0 ii. Inside destructor delete memory allocation of data completely 2. [10pts] Overload Copy constructor 3. [10pts] ad a. For input, user inputs the values of the matrix in row then column format i. HINT: using a space you can separate column data and enter for row b. For output, use tab and return line to create a matrix-like output 4. [10pts] Overload the assignment operator (). 5. [20pts] Overload (+), ),(), and (-) operators and () for post and pre type 6. [30pts] Overload the () operator to perform matrix multiplication. a. Check the dimensions of both matrices to ensure proper dimensions, output an error on cerr and exit(1) if dimensions are invalid. b. Must be able to work on any 2 valid matrices. 7. [10pts] Program Correctness a. Prompt user for number rows and columns for two matrices. b. Prompt the user to enter data for these matrices.Explanation / Answer
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Matrix
{
private:
int rows;
int cols;
double** data;
public:
Matrix(int r, int c)
{
rows = r;
cols = c;
data = new double* [rows];
for(int i = 0; i < rows; i++)
{
data[i] = new double[cols];
}
}
Matrix()
{
rows = 0;
cols = 0;
}
~Matrix()
{
delete[] data;
}
Matrix(const Matrix &m)
{
rows = m.rows;
cols = m.cols;
data = new double* [rows];
for(int i = 0; i < rows; i++)
{
data[i] = new double[cols];
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
data[i][j] = m.data[i][j];
}
}
}
void operator = (const Matrix &m )
{
rows = m.rows;
cols = m.cols;
data = new double* [rows];
for(int i = 0; i < rows; i++)
{
data[i] = new double[cols];
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
data[i][j] = m.data[i][j];
}
}
}
Matrix operator+(const Matrix& m2);
Matrix operator-(const Matrix& m2);
friend ostream & operator << (ostream &out, const Matrix &m);
friend istream & operator >> (istream &in, Matrix &m);
friend Matrix operator*(Matrix m1, const Matrix& m2); };
//overloading << operator ostream & operator << (ostream &out, const Matrix &m)
{
out << "value of the matrix is"<<endl;
for(int i = 0; i < m.rows; i++)
{
for(int j =0; j < m.cols; j++)
{
out << m.data[i][j]<<" ";
}
out<<endl;
}
return out;
}
//overloading >> operator istream & operator >> (istream &in, Matrix &m)
{
cout << "Enter the matrix values"<<endl;
for(int i = 0; i < m.rows; i++)
{
for(int j =0; j < m.cols; j++)
{
in >> m.data[i][j];
}
}
return in;
}
//overloading * operator Matrix operator*(Matrix m1, const Matrix& m2)
{ if(m1.cols != m2.rows)
{
cerr << "invalid dimensions: Mutiplication cannot be performed"<<endl; exit(1);
}
Matrix result(m1.rows, m2.cols);
for (int i = 0; i < m1.rows; i++)
{
for (int j = 0; j < m2.cols; j++)
{
for (int k = 0; k < m1.cols; k++)
{
result.data[i][j] += m1.data[i][k]*m2.data[k][j];
}
}
}
return result;
}
//overoad + operator Matrix Matrix::operator+(const Matrix& m2)
{
if((this->rows != m2.rows) || (this->cols != m2.cols))
{
cerr << "invalid dimensions: Number of rows and columns must be equal for both matrix"<<endl; exit(1);
}
Matrix result(rows,cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result.data[i][j] = this->data[i][j]+m2.data[i][j];
}
}
return result;
}
Matrix Matrix::operator-(const Matrix& m2) {
if((this->rows != m2.rows) || (this->cols != m2.cols)
{
cerr << "invalid dimensions: Number of rows and columns must be equal for both matrix"<<endl; exit(1);
}
Matrix result(rows,cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0;
j < cols; j++)
{
result.data[i][j] = this->data[i][j]-m2.data[i][j];
}
}
return result;
} int main()
{
Matrix m3; int r, c; cout << "enter number of rows and columns for first matrix"<<endl; cin >> r >> c; Matrix m1(r,c); //calls overloaded >> operator cin >> m1;
cout << "enter number of rows and columns for Second matrix"<<endl;
cin >> r >> c; Matrix m2(r,c); //calls overloaded >> operator cin >> m2;
//calls overloaded operator * m3 = m1 * m2;
//calls overloaded << operator cout << m3; //calls overloaded + operator m3 = m1 + m2;
cout << m3; //calls overloaded - operator m3 = m1 - m2;
cout << m3;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.