944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 755
  • C++ RSS
Nov 9th, 2009
0

Matrix multiplication

Expand Post »
Hey, so I have to make a program that has to multiply two 3x3 matrices (among other things). I used classes to do the project, but I just can't seem to get this multiplication to work. Here is my code so far:
C++ Syntax (Toggle Plain Text)
  1. void matrix::mult(const matrix& u)
  2. {
  3. double c[9];
  4. double a[9];
  5. for (int i=0; i<rows; i++)
  6. {
  7. for (int j=0; j<columns; j++)
  8. {
  9. for(int r=0; r<columns; r++)
  10. {
  11. c[i*columns+j]+=a[i*columns+r]*u.data[r*columns+j];
  12. }
  13. }
  14. }
  15. }

Any advice would be great, and please let me know if there's anything I should clarify. Thank You.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
uncbball is offline Offline
2 posts
since Nov 2009
Nov 9th, 2009
0
Re: Matrix multiplication
First thing that comes to mind is that a[9] and c[9] are not initialized.

Next you are using variables columns and row. So check that this->rows == u.columns. Then set the data in this. as c[] is just ignored.

Other than that it looks ok.
Last edited by StuXYZ; Nov 9th, 2009 at 11:35 pm.
Reputation Points: 749
Solved Threads: 135
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008
Nov 10th, 2009
0
Re: Matrix multiplication
Click to Expand / Collapse  Quote originally posted by StuXYZ ...
Next you are using variables columns and row. So check that this->rows == u.columns. Then set the data in this. as c[] is just ignored.
Hey, thanks for replying. I'm not quite sure what you mean with the above though. And if it matters, rows and columns aren't actually variables, they're constants.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
uncbball is offline Offline
2 posts
since Nov 2009
Nov 10th, 2009
0
Re: Matrix multiplication
Why do you have 3 loops for a 1d vector?

If you want to emulate it as a 2d then all you need is 2 for loops.

C++ Syntax (Toggle Plain Text)
  1. int A[4] = {1,2,3,4};
  2. int B[4] = {1,2,3,4};
  3. int R[4] = {0};
  4.  
  5. for(int i = 0; i < 4; i++)
  6. {
  7. for(int j = 0; j < 4; j++)
  8. R[ j + 4*i] = A[j + 4*i] * B[j + 4*i];
  9. }

Its better to make a function that converts (j+4*i) into a 2d index so it becomes more readable.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Nov 10th, 2009
0
Re: Matrix multiplication
Hi.
Try this. Naive, but should give you some idea.
The way you architect the classes entirely depends on you.
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3. const int SIZE = 3;
  4.  
  5. class Matrix{
  6. public:
  7. int matrix[SIZE][SIZE];
  8. void read_matrix();
  9. };
  10.  
  11. class MatOps{
  12. public:
  13. Matrix mul_matrix(Matrix &, Matrix &);
  14. };
  15.  
  16. void Matrix :: read_matrix(){
  17. cout<<"Enter matrix(3 x 3)"<<endl;
  18. int i = 0;
  19. int j = 0;
  20. for(i = 0; i < SIZE; i++){
  21. for(j = 0; j < SIZE; j++){
  22. cin>>matrix[i][j];
  23. }
  24. }//for ends
  25. }//read_matrix() ends
  26.  
  27.  
  28. Matrix MatOps :: mul_matrix(Matrix & a, Matrix & b){
  29. Matrix result;
  30. int i = 0;
  31. int j = 0;
  32. int k = 0;
  33.  
  34. for(i = 0; i < SIZE; i++){
  35. for(j = 0; j < SIZE; j++){
  36. result.matrix[i][j] = 0;
  37. for(k = 0; k < SIZE; k++){
  38. result.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j];
  39. }
  40. }
  41. }//for ends
  42.  
  43. return result;
  44. }//read_matrix() ends
  45.  
  46.  
  47. int main(){
  48. Matrix a, b;
  49. a.read_matrix();
  50.  
  51. b.read_matrix();
  52.  
  53. MatOps obj;
  54. Matrix result = obj.mul_matrix(a, b);
  55.  
  56. for(int i = 0; i < SIZE; i++){
  57. for(int j = 0; j < SIZE; j++){
  58. cout<<result.matrix[i][j]<<" ";
  59. }
  60. cout<<endl;
  61. }
  62.  
  63. return 0;
  64. }//main() ends

Cheers.
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007

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: Internet Provider Switch Program Help
Next Thread in C++ Forum Timeline: Creating an array of linked lists -- seg fault :'(





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


Follow us on Twitter


© 2011 DaniWeb® LLC