Assistace please

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 6
Reputation: solomon grundy is an unknown quantity at this point 
Solved Threads: 0
solomon grundy solomon grundy is offline Offline
Newbie Poster

Assistace please

 
0
  #1
Dec 19th, 2006
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 471
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Assistace please

 
0
  #2
Dec 19th, 2006
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 don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Assistace please

 
0
  #3
Dec 19th, 2006
Originally Posted by solomon grundy View 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
Department of Redundancy Department. :mrgreen:

And your question is?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC