debug the errors: #include<iostream> using namespace std; //class declartion cla
ID: 3634462 • Letter: D
Question
debug the errors:#include<iostream>
using namespace std;
//class declartion
class matrix
{
//Variable declaration
public:
int row,col,a[10][10];
public:
matrix() { }
matrix(int,int);
//member function declarations
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);
};
//get matrix member function
void matrix::getmatrix()
{
//cout<<"Enter the elements of the matrix"< for(int i=0;i for(int j=0;j cin>>a[i][j];
}
//constructor
matrix::matrix(int a,int b)
{
row=a;
col=b;
}
//operator +
matrix matrix::operator+(const matrix& m)
{
matrix c(row,col);
if((row==m.row)&&(col==m.col))
{
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
c.a[i][j]=a[i][j]+m.a[i][j];
}
}
else
{
cout<<"Order of the two matrices should be same";
}
return c;
}
matrix matrix::operator-(const matrix& m)
{
matrix c(row,col);
if((row==m.row)&&(col==m.col))
{
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{ c.a[i][j]=a[i][j]-m.a[i][j];}
}
else
{
cout<<"Order of the two matrices should be same";
}
return c;
}
//operator *
matrix matrix::operator*(const matrix& m)
{
matrix c(row,col);
if(col==m.row)
{
for(int i=0;i<10;i++)
for(int j=0; j<10; j++)
{
for(int k=0; k<10; k++)
c.a[i][j] += (a[i][k] * m.a[k][j]);
}
else
{
cout<<"Multiplication not possible, enter matrix of order m(pXq) and n(qXr)"<exit(0);
}
return c;
}
ostream& operator<<(ostream& os,const matrix& m)
{
for (int r=0;r {
os < for (int c=0;c {
os << m.a[r][c];
os << " ";
}
}
return os;
}
//main function
void main()
{
int r,p,x,y,c;
//Menu display
cout<<"1-->Addition"<Substraction"<Multiplication"<Exit"<cout<<"Enter the choice"<cin>>c;
//inputting number of rows columns for matrix1
cout<<" Enter the number of rows and columns of matrix1"<cin>>r>>p;
//inputting number of rows columns for matrix2
cout<<" Enter the number of rows and columns of matrix2"<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:"< cout<<" second matrix:"< cout<<" Resultant matrix"< break;
case 2: //inputting two matrix
d.getmatrix();
e.getmatrix();
//operator overloading
s=d-e;
cout<<" First matrix:"< cout<<" second matrix:"< cout<<" Resultant matrix"< break;
case 3: //inputting two matrix
d.getmatrix();
e.getmatrix();
//operator overloading
m=d*e;
cout<<" First matrix:"< cout<<" second matrix:"< cout<<" Resultant matrix"< break;
default:
cout<<" Invalid Choice"< break;
}//end switch
//pause system for a while
system("pause");
}
Explanation / Answer
Dear,
Corrected code is:
#include <iostream>
using namespace std;
//class declartion
class matrix
{
//Variable declaration
public:
int row,col,a[10][10];
public:
matrix() { }
matrix(int,int);
//member function declarations
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);
};
//get matrix member function
void matrix::getmatrix()
{
//
cout<<"Enter the elements of the matrix: "<<endl;
for(int i=0;i <row; i++)
for(int j=0;j < col; j++)
cin>>a[i][j];
}
//constructor
matrix::matrix(int a,int b)
{
row=a;
col=b;
}
//operator +
matrix matrix::operator+(const matrix& m)
{
matrix c(row,col);
if((row==m.row)&&(col==m.col))
{
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
c.a[i][j]=a[i][j]+m.a[i][j];
}
}
else
{
cout<<"Order of the two matrices should be same";
}
return c;
}
matrix matrix::operator-(const matrix& m)
{
matrix c(row,col);
if((row==m.row)&&(col==m.col))
{
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{ c.a[i][j]=a[i][j]-m.a[i][j];}
}
else
{
cout<<"Order of the two matrices should be same";
}
return c;
}
//operator *
matrix matrix::operator*(const matrix& m)
{
matrix c(row,col);
if(col==m.row)
{
for(int i=0;i<10;i++)
for(int j=0; j<10; j++)
{
for(int k=0; k<10; k++)
c.a[i][j] += (a[i][k] * m.a[k][j]);
}
} // missed closing brace
else
{
cout<<"Multiplication not possible, enter matrix of order m(pXq) and n(qXr)";
exit(0);
}
return c;
}
ostream& operator<<(ostream& os,const matrix& m)
{
for (int r=0;r < m.row; r++){
for (int c=0;c < m.col; c++) {
os << m.a[r][c];
os << " ";
}
}
return os;
}
//main function
void main()
{
int r,p,x,y,c;
//Menu display
cout<<"1-->Addition"<<" 2-->Substraction"<<" 3-->Multiplication"<<" 4-->Exit"<<endl;
cout<<"Enter the choice: ";
cin >> c;
//inputting number of rows columns for matrix1
cout<<" Enter the number of rows and columns of matrix1: ";
cin>>r>>p;
//inputting number of rows columns for matrix2
cout<<" Enter the number of rows and columns of matrix2: ";
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 " << f; 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 " << s; 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"; break;
}//end switch
//pause system for a while
system("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.