Overloading operators, matrix arithmetic. Result problem.

Mar 24, 2017 at 4:54am
Hello guys, I am currently working on an assignment that is due in 4 days. The point is to make a program that performs matrix arithmetic (addition, subtraction and multiplication) between two matrices. I have managed to make most of the program but I am not understanding why the multiplication method is not working. The result seems to always return wild numbers like -858993452...

Suppose I make two matrices 2X2 each
they both contain 2 in each element, that is:

|2 2| (times) |2 2| (equals) |-858993452 -858993452|
|2 2| |2 2| |-858993452 -858993452|

This is my first time dealing with overloaded operators. I don't know where the error is but I think it's in the operator * overload. I will provide code below.
Any hints/solutions/suggestions are greatly appreciated.

source.cpp
#include "iostream"
#include "Matrix.h"

using namespace std;

int main()
{
int rows1 = 0, columns1 = 0, rows2 = 0, columns2 = 0, rows3 = 0, columns3 = 0, option = 0;

cout << "--------------------------------------------------------" << endl;
cout << "|| WELCOME TO THE MATRIX PROGRAM! ||" << endl;
cout << "|| We will create two matrices to perform arithmetic ||" << endl;
cout << "|| operations on them. ||" << endl;
cout << "||----------------------------------------------------||" << endl;
cout << "|| Before creating matrices please choose operation. ||" << endl;
cout << "||----------------------------------------------------||" << endl;
cout << "|| addition (1) substraction (2) ||" << endl;
cout << "|| multiplication (3) exit (4) ||" << endl;
cout << "--------------------------------------------------------" << endl << endl;
cin >> option;

cout << "--------------------------------------------------------" << endl;
cout << "|| Please enter Matrix 1 rows: ||" << endl;
cout << "--------------------------------------------------------" << endl << endl;
cin >> rows1;
cout << "--------------------------------------------------------" << endl;
cout << "|| Matrix 1 has " << rows1 << " rows! ||" << endl;
cout << "--------------------------------------------------------" << endl << endl;
cout << "--------------------------------------------------------" << endl;
cout << "|| Please enter Matrix 1 columns: ||" << endl;
cout << "--------------------------------------------------------" << endl << endl;
cin >> columns1;
cout << "--------------------------------------------------------" << endl;
cout << "|| Matrix 1 has " << columns1 << " columns! ||" << endl;
cout << "--------------------------------------------------------" << endl << endl;

cout << "--------------------------------------------------------" << endl;
cout << "|| Please enter Matrix 2 rows: ||" << endl;
cout << "--------------------------------------------------------" << endl << endl;
cin >> rows2;
cout << "--------------------------------------------------------" << endl;
cout << "|| Matrix 2 has " << rows2 << " rows! ||" << endl;
cout << "--------------------------------------------------------" << endl << endl;
cout << "--------------------------------------------------------" << endl;
cout << "|| Please enter Matrix 2 columns: ||" << endl;
cout << "--------------------------------------------------------" << endl << endl;
cin >> columns2;
cout << "--------------------------------------------------------" << endl;
cout << "|| Matrix 2 has " << columns2 << " columns! ||" << endl;
cout << "--------------------------------------------------------" << endl << endl;



Matrix m1(rows1, columns1);
Matrix m2(rows2, columns2);
Matrix multMatrix(rows1, columns2);


switch (option)
{
case 1:
{
//this works
break;
}
case 2:
{
//this works
break;
}
case 3:
{
cout << "--------------------------------------------------------" << endl;
cout << "|| You have chosen to perform multiplication! ||" << endl;
cout << "--------------------------------------------------------" << endl << endl;

m1.setMatrix();
m2.setMatrix();

multMatrix = m1 * m2;

cout << "--------------------------------------------------------" << endl;
cout << "Matrix one display: " << m1 << endl;
cout << "--------------------------------------------------------" << endl;
cout << "Matrix two display: " << m2 << endl;
cout << "--------------------------------------------------------" << endl;

cout << "Matrices result: " << multMatrix << endl;
cout << "--------------------------------------------------------" << endl << endl;
system("pause");
break;
}
default:
{
cout << "You did not enter a menu option correctly, please try again." << endl;
}
}
return 0;
}

Matrix.h

#pragma once
#include "iostream"

using namespace std;

class Matrix
{

public:
int rows, columns, a[10][10];

public:
Matrix();
Matrix(int, int);

friend ostream& operator<<(ostream& os, const Matrix& m);

void setMatrix();
Matrix operator*(const Matrix& s);
};

Matrix.cpp

#include "Matrix.h"



Matrix::Matrix()
{

}

Matrix::Matrix(int i, int j)
{
rows = i;
columns = j;
}

void Matrix::setMatrix()
{
cout << "Please enter matrix row elements separated by space: " << endl;

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cin >> a[i][j];
}
}
}

Matrix Matrix::operator*(const Matrix& m)
{
Matrix multMatrix(rows, columns);

if (columns == m.rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
for (int k = 0; k < m.columns; k++)
{
multMatrix.a[i][j] += a[i][k] * m.a[k][j];
}
}
}

}
else
{
cout << "The matrices seem to be out of order..." << endl;
cout << "please try to make them (mXn)*(nXw)." << endl;
exit(0);
}
return multMatrix;
}

ostream& operator<<(ostream& osObject, const Matrix& m)
{
for (int r = 0; r < m.rows; r++)
{
osObject << endl;
for (int c = 0; c < m.columns; c++)
{
osObject << m.a[r][c];
osObject << " ";
}
}
return osObject;
}


Thanks for your time.
Mar 24, 2017 at 5:16am
multMatrix.a[i][j] += a[i][k] * m.a[k][j];multMatrix hasn't been initialized with 0 in its cells, so the program could be picking up spurious values. see here for an implementation example:
http://ecomputernotes.com/cpp/classes-in-c/write-a-c-program-for-addition-multiplication-of-two-matrices-by-overloading-and-operators
Mar 24, 2017 at 6:08pm
Thank you gunnerfunner, this was useful. I was missing an initialization in the loop.
Topic archived. No new replies allowed.