Question
My code is working for the most part however I can get it to display multiplication of the matrices correctly. Please have a look. Thanks!
My code is working for the most part however I can get it to display multiplication of the matrices correctly. Please have a look.
Thanks!
#include
using namespace std;
//Declare class matrix
class matrix
{
public:
// Declare variables
int row, col, a[10][10];
public:
//Member functions
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);
};
//Function to get values of matrix
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 to get row and column size
matrix::matrix(int a, int b)
{
row = a;
col = b;
}
//function to perform multiplication of a matrix
matrix matrix::operator+(const matrix& m)
{
matrix c(row, col);
if ((row == m.row) && (col == m.col))
{
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
c.a[i][j] = a[i][j] + m.a[i][j];
cout << c.a[i][j] << endl;
}
}
}
else
{
cout << "order of two matrices should be same" << endl;
exit(0);
}
return c;
}
//function to perform subtraction of a matrix
matrix matrix::operator-(const matrix& m)
{
matrix c(row, col);
if ((row == m.row) && (col == m.col))
{
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
c.a[i][j] = a[i][j] - m.a[i][j];
}
}
}
else
{
cout << "order of two matices should be same" << endl;
exit(0);
}
return c;
}
//function to perform multiplication of a matrix
matrix matrix::operator*(const matrix& m)
{
matrix c(row, col);
if (col == m.row)
{
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
{
for (int k = 0; k < m.col; k++) {
c.a[i][j] += a[i][k] * m.a[k][j];
}
}
}
}
else
{
cout << "multiplication not possible" << endl;
exit(0);
}
return c;
}
// Overload <<
ostream& operator<<(ostream& os, const matrix& m)
{
for (int r = 0; r < m.row; r++)
{
os << endl;
for (int c = 0; c < m.col; c++)
{
os << m.a[r][c];
os << " ";
}
}
return os;
}
int main()
{
int r, p, x, y, c;
// Get information from user
cout << "Please choose one of the following opertations to perform" << endl;
cout << "1 addition" << endl << "2 subtraction" << endl << "3 multiplication" << endl << "4 exit" << endl;
cout << "enter the choice" << endl;
cin >> c;
cout << "enter the number of rows and columns of matrix1" << endl;
cin >> r >> p;
cout << "enter the number of rows and columns of matrix2" << endl;
cin >> x >> y;
// Create matrix
matrix d(r, p);
matrix e(x, y);
matrix f(r, p);
matrix s(r, p);
matrix m(r, y);
switch (c)
{
case 1:
d.getmatrix();
e.getmatrix();
f = d + e;
cout << "first matrix: ";
cout << d << endl;;
cout << "second matrix: " << e << endl;;
cout << "resultant matrix" << f << endl;
break;
case 2:
d.getmatrix();
e.getmatrix();
s = d - e;
cout << "First matrix: " << d << endl;
cout << "Second matrix: " << e << endl;
cout << "resultant matrix: " << s << endl;
break;
case 3:
d.getmatrix();
e.getmatrix();
m = d * e;
cout << "First matrix: " << d << endl;
cout << "Second matrix: " << e << endl;
cout << "resultant matrix: " << m << endl;
break;
default:
cout << "Invalid Choice" << endl;
break;
}
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started