I would liike to multiply two matrices but my code is not working..help me out!

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

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

I would liike to multiply two matrices but my code is not working..help me out!

 
0
  #1
Nov 4th, 2006
  1. #include <iostream.h>
  2.  
  3. #include <fstream.h>
  4.  
  5. main(int argc, char *argv[])
  6. {
  7. //definition of the variables.
  8.  
  9. int mtx1[20][20], mtx2[20][20], mtx3[20][20];
  10.  
  11. int n, i, j, k;
  12.  
  13. //exit if the number of arguments is not 2.
  14.  
  15. if(argc != 3)
  16. {
  17.  
  18.  
  19. cerr << "Usage: mm <filename1> <filename2>\n";
  20.  
  21. return 1;
  22.  
  23. }
  24.  
  25. //open the input file 1. exit if an error occurs.
  26.  
  27. ifstream fin1(argv[1]);
  28.  
  29. if(!fin1) {
  30.  
  31. cerr << "Can't open file1!\n";
  32.  
  33. return 1;
  34.  
  35. }
  36.  
  37.  
  38. //read the input file 1.
  39.  
  40. fin1 >> n; //read the number of rows and columns.
  41.  
  42.  
  43. for(j=1; j<=n; j++) {
  44.  
  45.  
  46. for(i=1; i<=n; i++) {
  47.  
  48.  
  49. fin1 >> mtx1[i][j]; //read the elements of the matrix.
  50.  
  51.  
  52. }
  53.  
  54. }
  55.  
  56. fin1.close(); //close the file 1.
  57.  
  58. //open the input file 2. exit if an error occurs.
  59.  
  60. ifstream fin2(argv[2]);
  61.  
  62. if(!fin2) {
  63.  
  64. cerr << "Can't open file2!\n";
  65.  
  66. return 1;
  67.  
  68. }
  69.  
  70. fin2 >> n; //read the number of rows and columns.
  71.  
  72. for(j=1; j<=n; j++) {
  73.  
  74. for(i=1; i<=n; i++) {
  75.  
  76. fin2 >> mtx2[i][j]; //read the elements of the matrix.
  77.  
  78. }
  79.  
  80. }
  81.  
  82. fin2.close(); //close the file 2.
  83.  
  84. //fill the matrix 3 for the result with zero.
  85.  
  86. for(j=1; j<=n; j++) {
  87.  
  88.  
  89. for(i=1; i<=n; i++) {
  90.  
  91.  
  92. mtx3[i][j] = 0;
  93.  
  94.  
  95. }
  96.  
  97. }
  98.  
  99. //multiply the matrices.
  100.  
  101. for(j=1; j<=n; j++) {
  102.  
  103.  
  104. for(i=1; i<=n; i++) {
  105.  
  106.  
  107. for(k=1; k<=n; k++) {
  108.  
  109.  
  110. mtx3[i][j] += mtx2[i][k] * mtx1[k][j];
  111.  
  112.  
  113. }
  114.  
  115. }
  116.  
  117. }
  118.  
  119. //output the result.
  120.  
  121. cout << n << "\n";
  122.  
  123. for(j=1; j<=n; j++) {
  124.  
  125.  
  126. for(i=1; i<=n; i++) {
  127.  
  128.  
  129. cout << mtx3[i][j];
  130.  
  131.  
  132. if(i < n) {
  133.  
  134.  
  135. cout << "\t";
  136.  
  137.  
  138. }
  139.  
  140. }
  141.  
  142. cout << "\n";
  143.  
  144. }
  145.  
  146. return 0;
  147.  
  148. } //end of the program.
Last edited by Salem; Nov 4th, 2006 at 4:52 am. Reason: Added code tags - learn to use them yourself!!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: I would liike to multiply two matrices but my code is not working..help me out!

 
0
  #2
Nov 4th, 2006
A fat lot of good that did!
Added code tags, but it's still unindented crap.

>
I would liike to multiply two matrices but my code is not working..help me out!

Post a proper question.
Don't just dump the code and hope someone will figure it out.

Does it compile?
Does it run?
What symptoms are there (error messages etc)
What input do you give and what answers do you get / expect ?
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum


Views: 4083 | Replies: 1
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC