944,068 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4927
  • C++ RSS
Nov 4th, 2006
0

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

Expand Post »
C++ Syntax (Toggle Plain Text)
  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!!!
Similar Threads
hui
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hui is offline Offline
6 posts
since Nov 2006
Nov 4th, 2006
0

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

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 ?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

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: pass by help
Next Thread in C++ Forum Timeline: Standard Code Indentation





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


Follow us on Twitter


© 2011 DaniWeb® LLC