Question: Please help in C++ How can I used in the following way : cout < >matrix1 in my main function. #include using namespace std; class

Please help in C++

How can I used in the following way : cout<>matrix1 in my main function.

#include

using namespace std;

class Matrix{

private:

int rows, columns;

public:

int **arr; // Creating a 2D array dynamically

Matrix(int r, int c);

void printMatrix(Matrix a);

friend Matrix operator +(Matrix a, Matrix b); // operator overloading for addition

friend Matrix operator -(Matrix a, Matrix b); // operator overloading for subtraction

friend Matrix operator *(Matrix a, Matrix b); // operator overloading for matrix multiplication

friend Matrix operator *(Matrix a, int s); // operator overloading for scalar multiplication

int getRows()const //Getters

{

return rows;

}

int getColumns()const

{

return columns;

}

~Matrix(); //Destructor

friend istream& operator>> (istream& in, const Matrix& m){

for(int i=0;i

for(int j=0;j

in>>m.arr[i][j];

}

return in;

}

friend ostream &operator<<(ostream &os, const Matrix &m){

for(int i=0;i

for(int j=0;j

os<

os<<" ";

}

return os;

}

};

Matrix::Matrix(int r, int c)

{

this->rows = r;

this->columns = c;

arr = new int*[this->rows];

for(int i=0; i < this->rows; i++)

arr[i]= new int[this->columns];

}

Matrix operator +(Matrix a, Matrix b)

{

// if either rows are not equal or columns are not equal return empty matrix

if(a.rows != b.rows || a.columns != b.columns)

{

cout<<"The addition is not possible!"<

Matrix d(0,0);

return d;

}

Matrix c(b.rows, b.columns); //holds matrix values.

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

{

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

{

c.arr[i][j] = a.arr[i][j]+b.arr[i][j];

}

}

return c;

}

Matrix operator -(Matrix a, Matrix b)

{

// if either rows are not equal or columns are not equal return empty matrix

if(a.rows != b.rows || a.columns != b.columns)

{

cout<<"The subtraction is not possible!"<

Matrix d(0,0);

return d;

}

Matrix c(b.rows, b.columns);//holds matrix subtraction values.

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

{

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

{

c.arr[i][j] = a.arr[i][j]-b.arr[i][j];

}

}

return c;

}

//Scalar Multiplication

Matrix operator *(Matrix a, int s)

{

int ar = a.rows; // matrix rows

int ac = a.columns; //matrix columns

Matrix c(ar, ac);

for(int i=0; i < ar; i++)

{

for (int j =0; j < ac; j++)

{

c.arr[i][j] = a.arr[i][j]*s;

}

}

return c;

}

// Matrix Multiplication

Matrix operator *(Matrix a, Matrix b)

{

int ar = a.rows; // first matrix rows

int ac = a.columns; //first matrix columns

int br = b.rows; // second matrix rows

int bc = b.columns; // second matrix columns

// if columns of first matrix not equals to 2nd matrix row return empty matrix

if(ac != br)

{

cout<<"Matrix multiplication is not possible. ";

Matrix d(0,0);

return d;

}

Matrix c(ar,bc); //create a new object

for (int i=0; i < ar; i++)

{

for(int j =0; j < bc; j++)

{

c.arr[i][j] =0;

for(int k =0; k

{

c.arr[i][j] += a.arr[i][k] * b.arr[k][j];

}

}

}

return c;

}

void Matrix::printMatrix(Matrix a)

{

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

{

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

{

cout<

}

cout<

}

}

Matrix::~Matrix()

{ //Free each sub-array

//delete arr;

//Free the array of pointers

this->rows = 0;

this->columns = 0;

//cout<<"Destroyed"<arr[0][0];

}

int main()

{

int row1, column1, row2,column2;

//Let the user enter the values for first Matrix1

cout<<"Please enter the Rows and Columns in Matrix1."<

cin>>row1>>column1;

Matrix a(row1, column1); // Creating matrix 1

cout<<"Enter the values of Matrix 1. "<

for(int i=0; i < row1; i++)

{

for(int j=0; j< column1; j++)

{

cin>>a.arr[i][j];

}

}

//Let the user enter the values for first Matrix2

cout<<"Please enter the Rows and Columns in Matrix2."<

cin>>row2>>column2;

Matrix b(row2, column2); // Creating matrix 2

cout<<"Enter the values of Matrix 2. "<

for(int i=0; i < row2; i++)

{ //Let the user to enter the values

for(int j=0; j< column2; j++)

{

cin>>b.arr[i][j];

}

}

// Print Matrix 1

cout<<" Matrix 1 "<

a.printMatrix(a);

// Print Matrix 2

cout<<" Matrix 2 "<

b.printMatrix(b);

cout<<" The addition of 1 and 2 is "<

Matrix c = a + b;

if(c.getRows() == 0 && c.getColumns() == 0)

cout << "This Cannot be add!" << endl;

else

c.printMatrix(c);

cout<<" The subtraction of 1 and 2 is "<

Matrix d = a - b;

if(d.getRows() == 0 && d.getColumns() == 0)

cout << "This Cannot be subtract!" << endl;

else

d.printMatrix(d);

cout<<" The Matrix multiplication of 1 and 2 is "<

Matrix e = a * b;

if(e.getRows() == 0 && e.getColumns() == 0)

cout << "This Cannot be Multiply!" << endl;

else

e.printMatrix(e);

cout<<" The Scalar multiplication of 1 and 2 of 5 is "<

Matrix f = a * 5;

f.printMatrix(f);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!