The program works, but the output of the resultant matrix keeps coming up with w
ID: 3622493 • Letter: T
Question
The program works, but the output of the resultant matrix keeps coming up with wacky numbers. The program is below, need help ASAP
#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);
};
// get matrix member function
void matrix::getmatrix()
{
cout<<"Enter the elements of the matrix"<
for(int i=0;ifor(int j=0;j
cin>>a[i][j];
}
void matrix::getmatrix2()
{
cout<<"Enter the elements of the second matrix"<
for(int i=0;ifor(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;ifor(int j=0;jc.a[i][j]=a[i][j]+m.a[i][j];
}
else
{
cout<<"Order of two matrices should be the same"}
return c;
}
//operator -
matrix matrix::operator-(const matrix&m)
{
matrix c(row,col);
if((row==m.row)&&(col==m.col))
{
for(int i=0;ifor(int j=0;jc.a[i][j]=a[i][j]-m.a[i][j];
}
else
{
cout<<"Order of the two matrices should be the same"}
return c;
}
//operator *
matrix matrix::operator*(const matrix& m)
{
matrix c(row,col);
if(col==m.row)
{
for(int i=0;i
for(int j=0; j
{
for(int k=0; k
c.a[i][j] += a[i][k] * m.a[k][j];
}
}
else
{
cout<<"Multiplication is not possible, enter matrix of order m(pXq) and n(qXr)"}
return c;
}
ostream& operator<<(ostream& os,const matrix& m)
{
for (int r=0;r{
os os<< m.a[r][c];
os << " ";
}
}
return os;
}
void main()
{
//declare variables
int r,p,x,y,c;
//menu
cout<<"1-->Addition"cout<<"Enter the choice">c;
//input number of rows and columns for matrix1
cout<<"Enter the number of rows and columns of Matrix 1">r>>p;
//input number of rows and columns for Matrix 2
cout<<"Enter the number of rows and columns of Matrix 2">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.getmatrix2();
//Overload operator
f=d+e;
cout<<"First Matrix:"
case 2: //input two matrix
d.getmatrix();
e.getmatrix2();
//Overload operator
s=d-e;
cout<<"First Matrix:"
case 3: //input two matrix
d.getmatrix();
e.getmatrix2();
//Overload operator
m=d*e;
cout<<"First Matrix;"
default:
cout<<"Invalid Choice"}
system("pause");
}
Explanation / Answer
//Header file section #include "stdafx.h" #include using namespace std; class matrix { public: int row,col,a[10][10]; public: matrix() { } matrix(int,int); //member function declarations friend ostream& operatorRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.