Implement a class Matrix as defined below. class Matrix { public: Matrix(); //create a matrix of size 2X2 Matrix(int r,int c); // create a matrix with r rows and c columns Matrix(const Matrix & m); //copy constructor , create a deep copy of //matrix m Matrix operator+(const Matrix &); Matrix operator-(const Matrix &); Matrix operator*(const Matrix &); bool operator==(const Matrix &); bool operator!=(const Matrix &); Friend ostream& operator<<(ostream& ,Matrix &); Friend istream& operator>>(istream& ,Matrix &); bool isIdentity(); //returns true if matrix is identity matrix 3 | P a g e void transpose(); //transpose of matrix void inverse(); // inverse of matrix void adjoint(); //adjoint of matrix int getElement(int r,int c); // returns element at row r and col c int determinant(); //find the determinant void fillMatrix(); // fill matrix by taking input from user void print(); ~Matrix(); private: int ** data; int rows; int cols; };
#include <iostream>
using namespace std;
#include <iostream>
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(a.Check(a, b)) //Calling the method Check() using Matrix object -- a
{
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!"<<endl;
}
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. \n ";
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 <ac; 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<<a.arr[i][j]<<" ";
}
cout<<endl;
}
}
Matrix::~Matrix(){ //Free each sub-array
//delete arr;
//Free the array of pointers
this->rows = 0;
this->columns = 0;
//cout<<"Destroyed"<<this->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."<<endl;
cin>>row1>>column1;
Matrix a(row1, column1); // Creating matrix 1
cout<<"Enter the values of Matrix 1. "<<endl;
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."<<endl;
cin>>row2>>column2;
Matrix b(row2, column2); // Creating matrix 2
cout<<"Enter the values of Matrix 2. "<<endl;
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<<" \nMatrix 1 "<<endl;
a.printMatrix(a);
// Print Martix 2
cout<<" \nMatrix 2 "<<endl;
b.printMatrix(b);
cout<<"\nThe addition of 1 and 2 is "<<endl; //Addition
if(Check(a,b))
{
Matrix c = a.add(a,b);
c.printMatrix(c);
}
else
cout<<"This cannot be add!"<<endl;
cout<<"\nThe subtraction of 1 and 2 is "<<endl; //Subtraction
Matrix d = a.subtract(a,b);
//condition
d.printMatrix(d);
cout<<"\nThe multiplication of 1 and 2 is "<<endl; //Multiplication
Matrix e = a.multiply(a,b);
e.printMatrix(e);
cout<<"\nThe scalar multiplication of 1 and 2 is "<<endl; //Scalar Multiplication
Matrix f = a.scalar(a,3);
f.printMatrix(f);
return 0;
}
Comments
Leave a comment