There are, unfortunately,
a lot of things wrong here... (I’m so sorry).
The point of the matrix class is to
encapsulate everything that has to do with a matrix. That way your main function can look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int main()
{
cout << "Enter the size of the matrix as rows and columns, followed by the elements.\n";
cout << "For example, a 2 by 3 matrix could be entered as:\n";
cout << " 2 3\n";
cout << " 1 2 3\n";
cout << " 4 5 6\n";
cout << "\n";
matrix A;
cout << "Please enter matrix A: ";
cin >> A;
matrix B;
cout << "Please enter matrix B: ";
cin >> B;
matrix C = A * B;
cout << "A * B is: " << C << "\n";
}
|
To get to this happy coolness, you need to do a number of things.
First, the matrix class should have the dimensions and the elements array.
1 2 3 4 5
|
struct matrix
{
int rows;
int columns;
double elements[MAX_ROWS][MAX_COLUMNS];
|
You can declare those MAX_ROWS and MAX_COLUMNS constants anywhere. I would make them part of the class.
1 2 3 4 5 6 7 8
|
struct matrix
{
constexpr static int MAX_ROWS = 10;
constexpr static int MAX_COLUMNS = 10;
int rows;
int columns;
double elements[MAX_ROWS][MAX_COLUMNS];
|
Next, you should have a constructor for your matrix.
1 2 3 4 5 6 7
|
matrix( int m, int n ): rows(m), columns(n)
{
// I personally recommend you set all the elements of the matrix to zero
for (int i = 0; i < rows; ++i)
for (...)
...
}
|
Setting the elements to zero here in the constructor makes your life easier when you write the multiplication. If you don’t do it here, you must do it there.
The function to multiply matrices can either be a member or non-member function (inside or outside the class). Since multiplying matrices produces a new matrix from two source matrices, I recommend you make it non-member.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
}; // end of matrix class
matrix operator * ( const matrix& a, const matrix& b )
{
matrix result( a.rows(), b.columns() );
// Remember that result must now be a a.rows() by b.columns() matrix with all elements
// equal to zero. If you didn’t do that in the constructor, you must do it here now.
// Make sure you do the multiplication correctly.
// https://www.mathsisfun.com/algebra/matrix-multiplying.html
return result;
}
|
You can make it a member function if you wish. Just be aware that a few things will change
1 2 3 4
|
matrix operator * ( const matrix& b )
{
matrix result( this->rows(), b.columns() );
...
|
Next you’ll want to create a function to
read a matrix from the user. I’ll give it to you as a freebee:
1 2 3 4 5 6 7 8 9 10
|
istream& operator >> ( istream& ins, matrix& m )
{
ins >> m.rows >> m.columns;
for (int i = 0; i < m.rows; ++i)
for (int j = 0; j < m.columns; ++j)
ins >> m.elements[i][j];
return ins;
}
|
Finally you’ll want a function to print a matrix.
1 2 3 4 5 6
|
ostream& operator << ( ostream& outs, const matrix& m )
{
// You must figure this out yourself. Use 'outs' instead of 'cout'.
return outs;
}
|
So far we have ignored a number of important things, like error checking. Personally, I would also add checking to make sure that:
• the user cannot make a matrix with too many rows and columns
check this in the constructor and the input function
• A.columns() == B.rows() when multiplying
check this in the multiplication function
The simplest way for you to do that is to just
throw
if something is wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
istream& operator >> ( istream& ins, matrix& m )
{
ins >> m.rows >> m.columns;
if ((m.rows > m.MAX_ROWS) || (m.columns > m.MAX_COLUMNS))
throw "matrix too big";
for (int i = 0; i < m.rows; ++i)
for (int j = 0; j < m.columns; ++j)
ins >> m.elements[i][j];
return ins;
}
|
And you can complain properly to the user by wrapping the code in main with a try..catch block:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int main()
{
try {
// all the normal stuff in main goes here
}
catch (const char* msg)
{
cerr << msg << "\n";
return 1;
}
}
|
Hope this helps.