#include using namespace std; class matrix { public: int row,col,a[10][10]; publ
ID: 3623908 • Letter: #
Question
#include
using namespace std;
class matrix
{
public:
int row,col,a[10][10];
public:
matrix () { }
matrix (int, int);
friend ostream & operator<<(ostream& os, const matrix & m);
void getmatrix ();
matrix operator+(const matrix & m);
matrix operator-(const matrix & m);
matrix operator*(const matrix & s);
};
void matrix:: getmatrix()
{
cout<<"enter the elements of the matrix"<
for (int i=0; i
for (int j=0; j
cin>>a[i][j];
}
matrix::matrix(int a, int b)
{
row=a;
col=b;
}
matrix matrix::operator+(const matrix & m)
{
matrix c(row, col);
if ((row==m.row)&&(col==m.col))
{
for(int i=0;i
for (int j=0;j
c.a[i][j]=a[i][j]+m.a[i][j];
}
else
{
cout<<"Order of two matricies should be the same"<
exit(0);
}
return c;
}
matrix matrix::operator-(const matrix & m)
{
matrix c(row,col);
if ((row==m.row)&&(col==m.col))
{
for(int i=0;i
for(int j=0;j
c.a[i][j]=a[i][j]-m.a[i][j];
}
else
{
cout<<"Order of two matrices should be the same"<
exit(0);
}
return c;
}
Explanation / Answer
for (int r=0;r<m.row;r++)
{
os <<endl;
for (int c=0;c<m.col;c++)
{
os << m.a[r][c];
os << " ";
}
int r,p,x,y,c;//variable declaration
cout<<"1-->Addition"<<endl<<"2-->Substraction"<<endl<<"3-->Multiplication"<<endl<<"4-->Exit"<<endl;
//inputting choice
cout<<"Enter the choice"<<endl;
cin>>c;
//inputting number of rows columns for matrix1
cout<<"enter the number of rows and columns of matrix1"<<endl;
cin>>r>>p;
//inputting number of rows columns for matrix2
cout<<"enter the number of rows and columns of matrix2"<<endl;
cin>>x>>y;
//function calls
matrix d(r,p);
matrix e(x,y);
matrix f(r,p);
matrix s(r,p);
matrix m(r,y);
switch(c)
{
case 1:
//input two matrix
d.getmatrix();
e.getmatrix();
//operator overloading
f=d+e;
cout<<"First matrix:"<<d;
cout<<"second matrix:"<<e;
cout<<"Resultant matrix"<<m;
break;
case 2: //inputting two matrix
d.getmatrix();
e.getmatrix();
//operator overloading
s=d-e;
cout<<"First matrix:"<<d;
cout<<"second matrix:"<<e;
cout<<"Resultant matrix"<<m;
break;
case 3: //inputting two matrix
d.getmatrix();
e.getmatrix();
//operator overloading
m=d*e;
cout<<"First matrix:"<<d;
cout<<"second matrix:"<<e;
cout<<"Resultant matrix"<<m;
break;
default:
cout<<"Invalid Choice"<<endl;
break;
}//end switch
//pause system for a while
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.