944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 937
  • C++ RSS
Dec 19th, 2006
0

Assistace please

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. class MatrixNxN
  5. {
  6. public:
  7. int numRows;
  8. int numCols;
  9. float index[4][4];
  10. };
  11.  
  12. void printMatrix(MatrixNxN a);
  13. MatrixNxN inputMatrix();
  14. //This function returns true if the matrices are compatible for multiplication.
  15. bool canMultiplyMatrices( MatrixNxN a, MatrixNxN b );
  16. //This function actually multiplies the matrices together.
  17. MatrixNxN multiplyMatrices(MatrixNxN a, MatrixNxN b);
  18. void main()
  19. {
  20. cout << "Please enter matrix A:" << endl;
  21. MatrixNxN A = inputMatrix();
  22. printMatrix( A );
  23. cout << " Please enter matrix B:" << endl;
  24. MatrixNxN B = inputMatrix();
  25. printMatrix (B);
  26. //I need help checking to see if I can multiply the two matrices.
  27. // If not, say so and return.
  28. //I need help multiplying the matrices to get a result.
  29. //I need help printing the result.
  30. }
  31. MatrixNxN inputMatrix()
  32. {
  33. MatrixNxN temp;
  34. //loop through rows
  35. int rows = 0, cols = 0;
  36. while( rows <= 0 || rows > 4 )
  37. {
  38. cout << "Please enter a row count between 1 and 4: ";
  39. cin >> rows;
  40. }
  41. while( cols <= 0 || cols > 4 )
  42. {
  43. cout << "Please enter a column count between 1 and 4: ";
  44. cin >> cols;
  45. }
  46. temp.numRows = rows;
  47. temp.numCols = cols;
  48. for(int i = 0; i < rows; ++i)
  49. {
  50. //loop through column at each row
  51. for(int j = 0; j < cols; ++j)
  52. {
  53. //i is the row & j is the column
  54. cout << "Enter row " << i << " column " << j << ": " << endl;
  55. cin >> temp.index[i][j];
  56. }
  57. }
  58. return temp;
  59. }
  60. void printMatrix(MatrixNxN a)
  61. {
  62. for(int i = 0; i < a.numRows; ++i)
  63. {
  64. //loop through column at each row
  65. for(int j = 0; j < a.numCols; ++j)
  66. {
  67. //i is the row & j is the column
  68. cout << "row " << i << " column " << j << ":" << a.index[i][j] << endl;
  69. }
  70. }
  71. }
  72. bool canMultiplyMatrices( MatrixNxN a, MatrixNxN b )
  73. {
  74. //What do I do here to find out if the matrices are compatible for multiplication?
  75. return false;
  76. }
  77. MatrixNxN multiplyMatrices(MatrixNxN a, MatrixNxN b)
  78. {
  79. MatrixNxN temp;
  80. //I need help filling in the entries of temp. Don't forget to set its numRows and numCols fields!
  81. //I need to use a loops to multiply these matrices. Follow what the other functions are doing
  82. //and I'll need a third loop in the middle to add up the terms for each entry in the result.
  83. return temp;
  84. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
solomon grundy is offline Offline
6 posts
since Nov 2006
Dec 19th, 2006
0

Re: Assistace please

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 ).
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Dec 19th, 2006
0

Re: Assistace please

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?
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: is this matrix code working?
Next Thread in C++ Forum Timeline: Softwares developed with C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC