You will implement the Matrix class class matrix public mat
You will implement the Matrix class:
class matrix
{ public:
matrix();
// Copy ctor, assignment operator, destructor
// friend operator+ adds two matrices, returns result by value
// operator>> and operator<<
private:
int numRows = 0; // number of rows
int numCols = 0; // number of cols
double **mArray = nullptr;
};
A matrix object stores a two dimensional array (of doubles) with numRows rows and numCols columns. Space for the array should be dynamically allocated, for the exact amount needed.
Default ctor: Empty body.
Copy ctor: Makes an independent copy (deep copy).
Destructor: Releases all dynamically allocated space.
Assignment operator: Allows cascading (ex. m1 = m2 = m3).
friend operator+ adds two matrices, returns result by value
operator>>: Read a matrix from the input file, store it in a previously created object. Allow cascading (ex. cin >> m1 >> m2).
operator<<: Output the matrix. Allow cascading (ex. cout << m1 << m2).
The input file contains three matrices. For each matrix, one line gives the number of rows, followed by the number of columns. Immediately following that, the matrix entries are given row by row. See sample input file infile1.
Suggestion: Have private member functions void allocateSpace() and void deleteSpace(); they allocate and deallocate space for a matrix, respectively. allocateSpace() is called from copy ctor, operator=, and operator>>. deleteSpace() is called from dtor, operator=, and operator>>.
Further:
• For efficiency reasons, you should have friends and members directly access other data members.
• Declare const whenever appropriate. Use references (instead of values), for parameters and returned items, whenever appropriate.
• Remember to use #ifndef in your header file.
• No comments or other documentation is needed. Use meaningful names for all variables. You must use main.cpp; no changes are allowed.
main.cpp
#include <iostream>
using namespace std;
#include \"Matrix.h\"
int main()
{ Matrix m1, m2, m3;
cin >> m1 >> m2 >> m3;
cout << \"m3 is:\ \" << m3 << endl;
Matrix m4(m1);
cout << \"m4 is:\ \" << m4 << endl;
m4 = m4;
cout << \"m4 is:\ \" << m4 << endl;
Matrix m5;
m4 = m5 = m2;
cout << \"m4 is:\ \" << m4 << endl;
Matrix m6;
m6 = m1+m3;
cout << \"m6 is:\ \" << m6 << endl;
}
infile example
4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
6 7
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
4 5
20 19 18 17 16
15 14 13 12 11
10 9 8 7 6
5 4 3 2 1
outfile example
m3 is:
This is a 4 by 5 Matrix
20 19 18 17 16
15 14 13 12 11
10 9 8 7 6
5 4 3 2 1
m4 is:
This is a 4 by 5 Matrix
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
m4 is:
This is a 4 by 5 Matrix
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
m4 is:
This is a 6 by 7 Matrix
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
m6 is:
This is a 4 by 5 Matrix
21 21 21 21 21
21 21 21 21 21
21 21 21 21 21
21 21 21 21 21
Solution
# include<iostream.h>
# include<conio.h>
# include<process.h>
# include<ctype.h>
# include<iomanip.h>
class Matrix
{
float row,col;
float **arr;
public:
Matrix();
Matrix(float,float);
Matrix(Matrix &);
// ~Matrix();void inputDim(float,float);
Matrix inverse();
Matrix operator+(Matrix &);
Matrix operator-(Matrix &);
Matrix operator*(Matrix &);
floatoperator==(Matrix &);
voidoperator+=(Matrix &);
friend voidoperator<<(ostream &,Matrix &);
friend voidoperator>>(istream &,Matrix &);
};
Matrix::Matrix()
{
row=col=0;
arr=NULL;
}
Matrix::Matrix(float row,float col)
{
this->row=row;
this->col=col;
arr=newfloat*[row];
for(float i=0;i<row;i++)
arr[i]=newfloat[col];
}
Matrix::Matrix(Matrix &t)
{
this->row=t.row;
this->col=t.col;
this->arr=newfloat*[t.row];
for(float i=0;i<t.row;i++)
this->arr[i]=newfloat[t.col];
for(float j=0;j<t.row;j++)
for(float k=0;k<col;k++)
this->arr[j][k]=t.arr[j][k];
}
/*
Matrix::~Matrix()
{
delete arr;
}
*/
Matrix Matrix::inverse()
{
float temp,tt;
Matrix t,x;
x=*this;
t.inputDim(this->row,this->col);
for(int i=0;i<this->row;i++)
{
for(int j=0;j<this->col;j++)
{
if(i==j)
t.arr[i][j]=1;
else
t.arr[i][j]=0;
}
}
for(i=0;i<t.row;i++)
{
temp=x.arr[i][i];
if(temp==0)
{
for(int j=0;j<t.col;j++)
{
tt=x.arr[i][j];
x.arr[i][j]=x.arr[i+1][j];
x.arr[i+1][j]=tt;
tt=t.arr[i][j];
t.arr[i][j]=t.arr[i+1][j];
t.arr[i+1][j]=tt;
}
}
//Step 1:for(int j=0;j<t.col;j++)
{
x.arr[i][j]/=temp;
t.arr[i][j]/=temp;
}
cout<<endl<<\"MATRIX A\"<<endl;
cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\"<<endl;
cout<<x;
cout<<endl<<\"MATRIX B\"<<endl;
cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\"<<endl;
cout<<t;
getch();
//Step 2:for(int k=0;k<t.row;k++)
{
temp=x.arr[k][i];
if(k!=i)
{
for(j=0;j<t.col;j++)
{
t.arr[k][j]+=(t.arr[i][j]*(-temp));
x.arr[k][j]+=(x.arr[i][j]*(-temp));
}
}
}
cout<<endl<<\"MATRIX A\"<<endl;
cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\"<<endl;
cout<<x;
cout<<endl<<\"MATRIX B\"<<endl;
cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\"<<endl;
cout<<t;
getch();
}
return t;
}
void Matrix::inputDim(float row,float col)
{
this->row=row;
this->col=col;
this->arr=newfloat*[row];
for(float i=0;i<row;i++)
this->arr[i]=newfloat[col];
}
float Matrix::operator==(Matrix &t)
{
if(this->row!=t.row || this->col!=t.col)
{
cout<<endl<<\"Comparison not possible\";
getch();
// exit(1);
}
for(float i=0;i<this->row;i++)
{
for(float j=0;j<this->col;j++)
{
if(this->arr[i][j]!=t.arr[i][j])
return 0; //Unequal Matrix;
}
}
return 1; //Equal Matrix;
}
Matrix Matrix::operator+(Matrix &m)
{
if(this->row==m.row && this->col==m.col)
{
Matrix t;
t.inputDim(m.row,m.col);
for(float i=0;i<m.row;i++)
for(float j=0;j<m.col;j++)
t.arr[i][j]=this->arr[i][j]+m.arr[i][j];
return t;
}
else
{
cout<<endl<<\"Addition is not possible : \";
getch();
// exit(1);
}
}
void Matrix::operator+=(Matrix &m)
{
if(this->row==m.row && this->col==m.col)
{
for(float i=0;i<m.row;i++)
for(float j=0;j<m.col;j++)
this->arr[i][j]+=m.arr[i][j];
}
else
{
cout<<endl<<\"Addition is not possible : \";
getch();
// exit(1);
}
}
Matrix Matrix::operator-(Matrix &m)
{
if(this->row==m.row && this->col==m.col)
{
Matrix t;
t.inputDim(m.row,m.col);
for(float i=0;i<m.row;i++)
for(float j=0;j<m.col;j++)
t.arr[i][j]=this->arr[i][j]-m.arr[i][j];
return t;
}
else
{
cout<<endl<<\"Subtraction is not possible : \";
getch();
// exit(1);
}
}
Matrix Matrix::operator*(Matrix &m)
{
if(this->col==m.row)
{
Matrix t;
t.inputDim(this->row,m.col);
for(float i=0;i<this->row;i++)
{
for(float j=0;j<m.col;j++)
{
t.arr[i][j]=0;
for(float k=0;k<m.row;k++)
t.arr[i][j]+=this->arr[i][k]*m.arr[k][j];
}
}
return t;
}
else
{
cout<<endl<<\"Addition is not possible : \";
getch();
// exit(1);
}
}
voidoperator<<(ostream &mycout,Matrix &t)
{
for(float i=0;i<t.row;i++)
{
cout.width(4);
cout.precision(2);
for(float j=0;j<t.col;j++)
mycout<<setw(5)<<t.arr[i][j]<<\" \";
mycout<<endl;
}
}
voidoperator>>(istream &mycin,Matrix &t)
{
for(float i=0;i<t.row;i++)
{
for(float j=0;j<t.col;j++)
{
cout<<endl<<\"Enter m[\"<<i+1<<\"][\"<<j+1<<\"] : \";
mycin>>t.arr[i][j];
}
}
}
void main()
{
int r,c,choice,DataEntered=0;
Matrix a,b,x;
void getDimension(int &,int &);
void showResult(Matrix &,Matrix &,Matrix &);
void showResult1(Matrix &,Matrix &);
char ch=\'Y\';
do
{
clrscr();
gotoxy(20,6);
cout<<\"Matrix Operations\";
gotoxy(20,7);
cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\";
gotoxy(20,8);
cout<<\"Create two new matricies : 1\";
gotoxy(20,9);
cout<<\"Matrix addition : 2\";
gotoxy(20,10);
cout<<\"Matrix addition using += : 3\";
gotoxy(20,11);
cout<<\"Matrix subtraction : 4\";
gotoxy(20,12);
cout<<\"Matrix multiplicaton : 5\";
gotoxy(20,13);
cout<<\"Matrix Equality checking : 6\";
gotoxy(20,14);
cout<<\"Show both Matrices : 7\";
gotoxy(20,15);
cout<<\"Inverse Matrix : 8\";
gotoxy(20,16);
cout<<\"Exit : 0\";
gotoxy(20,17);
cout<<\"Select your choice : \";
cin>>choice;
clrscr();
/* if(!DataEntered && choice!=1 && choice !=0)
{
cout<<endl<<\"Matrices are empty\";
getch();
continue;
}
*/
switch(choice)
{
case 1:
cout<<endl<<\"Matrix-A Details\";
cout<<endl<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\";
getDimension(r,c);
a = Matrix(r,c);
cin>>a;
cout<<endl<<\"Matrix-B Details\";
cout<<endl<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\";
getDimension(r,c);
b = Matrix(r,c);
cin>>b;
DataEntered=1;
break;
case 2:
x=a+b;
showResult(a,b,x);
break;
case 3:
a+=b;
showResult1(a,b);
break;
case 4:
x=a-b;
showResult(a,b,x);
break;
case 5:
Matrix x=a*b;
showResult(a,b,x);
break;
case 6:
if(a==b)
cout<<\"Both matrix are equal\";
else
cout<<\"Both matrix are unequal\";
break;
case 7:
showResult1(a,b);
break;
case 8:
cout<<endl<<\"Matrix-A Details\";
cout<<endl<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\";
getDimension(r,c);
a = Matrix(r,c);
cin>>a;
b = a.inverse();
showResult1(a,b);
case 0:
exit(1);
default:
gotoxy(20,16);
cout<<\"Invalid selection\";
}
cout<<endl<<\"continue (y/n) : \";
cin>>ch;
ch=toupper(ch);
}while(ch==\'Y\');
getch();
}
void getDimension(int &r,int &c)
{
cout<<endl<<\"Enter row dimension : \";
cin>>r;
cout<<endl<<\"Enter col dimension : \";
cin>>c;
}
void showResult(Matrix &x,Matrix &y,Matrix &z)
{
clrscr();
cout<<endl<<\"Matrix A\";
cout<<endl<<\"ÍÍÍÍÍÍÍÍ\"<<endl;
cout<<x;
cout<<endl<<\"Matrix B\";
cout<<endl<<\"ÍÍÍÍÍÍÍÍ\"<<endl;
cout<<y;
cout<<endl<<\"Result\";
cout<<endl<<\"ÍÍÍÍÍÍ\"<<endl;
cout<<z;
}
void showResult1(Matrix &x,Matrix &y)
{
clrscr();
cout<<endl<<\"Matrix A\";
cout<<endl<<\"ÍÍÍÍÍÍÍÍ\"<<endl;
cout<<x;
cout<<endl<<\"Matrix B\";
cout<<endl<<\"ÍÍÍÍÍÍÍÍ\"<<endl;
cout<<y;
}













