I need to Construct a program that inputs the entries of two matrices and multiplies them. Construct the program so that it gives an error message if they cannot be multiplied.
What I have done so far

I need to Construct a program that inputs the entries of two matrices and multiplies them. Construct the program so that it gives an error message if they cannot be multiplied.
What I have done so far

#include <iostream>
#include <vector>
using namespace std;
class MatrixNxN
{
public:
   int   numRows;
   int   numCols;
 float index[4][4];
};
 
void        printMatrix(MatrixNxN a);
MatrixNxN   inputMatrix();
//This function returns true if the matrices are compatible for multiplication.
bool        canMultiplyMatrices( MatrixNxN a, MatrixNxN b );
//This function actually multiplies the matrices together.
MatrixNxN   multiplyMatrices(MatrixNxN a, MatrixNxN b);
void main()
{
   cout << "Please enter matrix A:" << endl;
   MatrixNxN A = inputMatrix();
   printMatrix( A );
   cout << " Please enter matrix B:" << endl;
   MatrixNxN B = inputMatrix();
   printMatrix (B);
   //I need help checking to see if I can multiply the two matrices.
   //      If not, say so and return.
   //I need help multiplying the matrices to get a result.
   //I need help printing the result.
}
MatrixNxN inputMatrix()
{
 MatrixNxN temp;
 //loop through rows
   int rows = 0, cols = 0;
   while( rows <= 0 || rows > 4 )
   {
      cout << "Please enter a row count between 1 and 4: ";
      cin >> rows;
   }
   while( cols <= 0 || cols > 4 )
   {
      cout << "Please enter a column count between 1 and 4: ";
      cin >> cols;
   }
   temp.numRows = rows;
   temp.numCols = cols;
 for(int i = 0; i < rows; ++i)
 {
  //loop through column at each row
  for(int j = 0; j < cols; ++j)
  {
   //i is the row & j is the column
   cout << "Enter row " << i << " column " << j << ": " << endl;
   cin >> temp.index[i][j];
    }
 }
 return temp;
}
void printMatrix(MatrixNxN a)
{
 for(int i = 0; i < a.numRows; ++i)
 {
  //loop through column at each row
  for(int j = 0; j < a.numCols; ++j)
  {
   //i is the row & j is the column
   cout << "row " << i << " column " << j << ":" << a.index[i][j] << endl;
    }
 }
}
bool canMultiplyMatrices( MatrixNxN a, MatrixNxN b )
{
   //What do I do here to find out if the matrices are compatible for multiplication?
   return false;
}
MatrixNxN   multiplyMatrices(MatrixNxN a, MatrixNxN b)
{
   MatrixNxN temp;
   //I need help filling in the entries of temp.  Don't forget to set its numRows and numCols fields!
   //I need to use a loops to multiply these matrices.  Follow what the other functions are doing
   //and I'll need a third loop in the middle to add up the terms for each entry in the result.
   return temp;
}

Recommended Answers

All 2 Replies

To test whether the matrices are compatible for multiplication, you have to check that the second dimension of the first matrix ( y1 ) is equal to the first dimension of the second matrix ( x1 ).

I need to Construct a program that inputs the entries of two matrices and multiplies them. Construct the program so that it gives an error message if they cannot be multiplied.
What I have done so far

I need to Construct a program that inputs the entries of two matrices and multiplies them. Construct the program so that it gives an error message if they cannot be multiplied.
What I have done so far

Department of Redundancy Department. :mrgreen:

And your question is?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.