No throw exception

I keep getting a no throw exception for the code I am writing for matrix multiplication. The error is happening in my display function where my cout statement is Im not sure if it is my syntax or if Im setting something to null in the array without knowing. Any help would be appreciated.

#include<iostream>
using namespace std;

const int ROWS = 3;
const int COLUMNS = 3;
class Matrix
{
int p[ROWS][COLUMNS];
int row, column;
public:
void InputMatrix();
void display();
friend Matrix operator*(Matrix matrix1, Matrix matrix2);
};

Matrix operator*(Matrix matrix1, Matrix matrix2)
{
Matrix temp;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
temp.p[i][j] = 0;
for (int k = 0; k < ROWS; k++)
{
temp.p[i][j] = temp.p[i][j] + (temp.p[i][k] * temp.p[k][j]);
}
}
}
return temp;
}

void Matrix::InputMatrix()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
cout << "Enter elements of row " << i + 1 << " and column "
<< j + 1 << endl;
cin >> p[i][j];
}
}
}

void Matrix::display()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; i++)
{
cout << " " << p[i][j];
}
cout << endl;
}
}


int main()
{
Matrix matrix1;
Matrix matrix2;
Matrix matrix3;
cout << "Enter elements for the first matrix: " << endl;
matrix1.InputMatrix();
system("cls");
cout << "Enter elements for the second matrix: " << endl;
matrix2.InputMatrix();
matrix3 = matrix1 * matrix2;
matrix3.display();

system("pause");
return 0;
}
Read carefully, letter by letter ...
for (int j = 0; j < COLUMNS; i++)
in routine Matrix::display().

(Please use code tags - it maintains the indentation, gives it line numbers to refer to, makes it easier to read, and allows complete code to be run in the cpp shell. Also, DON'T DOUBLE-POST.)

You might want to find a better method of input than asking the user to enter 18 items one-by-one.
Last edited on
THANK YOU! but now my outputs dont seem to be correct I think it is outputting addresses rather than the product
Please post your revised code ... in code tags (after fixing as below):

Your matrix multiplication operator appears to be trying to multiply temp by temp, not matrix1 by matrix2. (If you turn up the error messages on your compiler it will tell you about the unused arguments.)
Last edited on
void Matrix::OutMatrix()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
cout << p[i][j];
}
cout << endl;
}
}

-This was the fix with the for loop that got rid of the thrown exception but my output wast coming out correct I ended having to fix my operator overload. I was multiplying temp rather than multiplying matrix1 and matrix2

Matrix operator*(Matrix matrix1, Matrix matrix2)
{
Matrix temp;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
temp.p[i][j] = 0;
for (int k = 0; k < 3; k++)
{
temp.p[i][j] += matrix1.p[i][k] * matrix2.p[k][j];

}
}
}
return temp;

}
Topic archived. No new replies allowed.