Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I use bool check in the main function to call bool Matrix::Check(Matrix a, Matrix b){ if (a.rows == b.rows && a.columns == b.columns)

How can I use bool check in the main function to call

bool Matrix::Check(Matrix a, Matrix b){

if(a.rows == b.rows && a.columns == b.columns)

return true;

return false;

}

#include

using namespace std;

#include

using namespace std;

class Matrix{

private:

int rows, columns;

public:

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

bool Check(Matrix a, Matrix b); //Check function

Matrix(int r, int c);

Matrix add(Matrix a, Matrix b);

Matrix subtract(Matrix a, Matrix b);

Matrix multiply(Matrix a, Matrix b);

Matrix scalar(Matrix a, int b);

void printMatrix(Matrix a);

int getRows()const{

return rows;

}

int getColumns()const{

return columns;

}

~Matrix(); //Destructor

};

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];

}

bool Matrix::Check(Matrix a, Matrix b){

if(a.rows == b.rows && a.columns == b.columns)

return true;

return false;

}

Matrix Matrix:: add(const Matrix a, Matrix b){

Matrix c(a.rows, a.columns);

if(Check(a, b))

{

if((a.rows == b.rows)&&(a.columns == b.columns)){

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 Matrix:: subtract(const Matrix a, Matrix b){ //subtraction

if(a.rows == b.rows && a.columns == b.columns){

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

}

Matrix c(a.rows, a.columns);

{

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 Matrix::multiply(const Matrix a, Matrix b)

{

int ar=a.rows;

int ac=a.columns;

int br=b.rows;

int bc=b.columns;

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;

}

Matrix Matrix::scalar(Matrix a, int s){ //Scalar

Matrix c(a.rows, a.columns);

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

{

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

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

}

}

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;

bool check;

//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 Martix 1

cout<<" Matrix 1 "<

a.printMatrix(a);

// Print Martix 2

cout<<" Matrix 2 "<

b.printMatrix(b);

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

if(Check(a,b))

{

Matrix c = a.add(a,b);

c.printMatrix(c);

}

else

cout<<"This cannot be add!"<

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

Matrix d = a.subtract(a,b);

//condition

d.printMatrix(d);

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

Matrix e = a.multiply(a,b);

e.printMatrix(e);

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

Matrix f = a.scalar(a,3);

f.printMatrix(f);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions