adding matrices in c++

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

Join Date: Jun 2006
Posts: 3
Reputation: amethystglow is an unknown quantity at this point 
Solved Threads: 0
amethystglow amethystglow is offline Offline
Newbie Poster

adding matrices in c++

 
0
  #1
Jul 5th, 2006
Hi again, all---

I'm trying to write a function that will add two matrices and output the resulting matrix.

I've got two matrices already and everything, now i just need to write the function that i can use to call in my switch loop (The program must read two matrices, and the user can then input if they want to add, subtract, or multiply them, and then see their resultant matrix.)

So i've just pasted the function definition i'm trying to use.

  1. double AddMatrix(double x[][5], double y[][5])
  2. {
  3. double matsum[5][5];
  4. int row, col;
  5. for(row=0; row<5; row++)
  6. for(col=0; col<5; col++)
  7. {
  8. matsum[5][5]=(x[row][col]+y[row][col]);
  9. }
  10.  
  11. return matsum[5][5];
  12. }

In my little command window, it shows me an answer but it's completely wrong!! i don't know how to make a new matrix that adds and i'm kind of lost and frustrated...if someone could give me tips or a skeleton of a function that might work, i'd be much obliged! thanks!

Actually, I'll show you the entire code here, it's incomplete, but as soon as I know how to add (and therefore subtract and multiply) matrices I can fill it all in.
  1. /*--------------------------------------------------------------------*/
  2. /* Final Exam- Matrix Operations */
  3. /* */
  4. /* This program will take two data files, open them, read them, save */
  5. /* their data in arrays, then let the user choose if they want to */
  6. /* add, subtract, or multiply the matrices as many times as they want*/
  7. /* until they choose the "quit" option, and show the results. */
  8. /* */
  9. #include <iostream> //Include the necessary libraries.
  10. #include <cstdlib>
  11. #include <fstream>
  12. #include <string>
  13. using namespace std;
  14.  
  15. double AddMatrix(double x[][5], double y[][5]);
  16. // Function Prototype(s) Will Go Here
  17. //Begin main function
  18. int main()
  19. {
  20. const int nrows=5; //Define the necessary constants
  21. const int ncols=5;
  22. //Define variables
  23. double Matrix1Info[nrows][ncols];
  24. double Matrix2Info[nrows][ncols];
  25. char ch;
  26. int row, col;
  27. ifstream matrix, matrix2;
  28. string filename, filename2;
  29.  
  30. // Ask user for name of input file 1.
  31. cout << "Enter first input file: " <<endl;
  32. cin >> filename;
  33. // Open file and read the info.
  34.  
  35. matrix.open(filename.c_str());
  36. if( matrix.fail() )
  37. {
  38. cout << "Error opening input file\n";
  39. }
  40. else
  41. {
  42. for (row=0; row<nrows; row++)
  43. for (col=0; col<ncols; col++)
  44. matrix >> Matrix1Info[row][col]; //Stores info into first matrix array
  45. for(int r=0; r<5; r++)
  46. {
  47. for(int c=0; c<5; c++)
  48. {
  49. cout << Matrix1Info[r][c] << ' ';
  50. }
  51. cout << endl;
  52. }
  53. cout << "Enter second input file: " <<endl; //Gets 2nd matrix file
  54. cin >> filename2;
  55. matrix2.open(filename2.c_str()); //Opens 2nd file
  56. if( matrix2.fail() )
  57. {
  58. cout << "Error opening input file\n";
  59. }
  60. else
  61. {
  62. for (row=0; row<nrows; row++)
  63. for(col=0; col<ncols; col++)
  64. matrix2 >> Matrix2Info[row][col]; //Stores info into 2nd matrix array
  65. for(int f=0; f<5; f++)
  66. {
  67. for(int g=0; g<5; g++)
  68. {
  69. cout << Matrix2Info[f][g] << ' ';
  70. }
  71. cout << endl;
  72. }
  73. cout << "Enter A for addition, S for subtraction, M for multiplication, or Q to quit: " << endl;
  74. cin >> ch;
  75. while(ch != 'Q')
  76. {
  77. switch(ch)
  78. {
  79. case 'A':
  80. cout << AddMatrix << endl;
  81. cout << "Enter A for addition, S for subtraction, M for multiplication, or Q to quit: " <<endl;
  82. case 'S':
  83. //function for subtraction goes here
  84. case 'M':
  85. //function for multiplication goes here
  86. break;
  87. default:
  88. cout << "Invalid operation choice." << endl;
  89. cout << "Enter A for addition, S for subtraction, M for multiplication, or Q to quit: " <<endl;
  90. }//end switch
  91. cin >> ch;
  92. }//end while loop
  93. }
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. //End of main file.
  104. return 0;
  105. }
  106. double AddMatrix(double x[][5], double y[][5])
  107. {
  108. double matsum[5][5];
  109. int row, col;
  110. const int nrows=5;
  111. const int ncols=5;
  112. for(row=0; row<5; row++)
  113. for(col=0; col<5; col++)
  114. {
  115. matsum[nrows][ncols]=(x[row][col]+y[row][col]);
  116. }
  117.  
  118. return matsum[5][5];
  119. }
Last edited by amethystglow; Jul 5th, 2006 at 4:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: adding matrices in c++

 
0
  #2
Jul 5th, 2006
Try doing it bit by bit and not all at one time.

Placing a few couts here and there should help debugging.

I'd probably create three matrices. One from matrix one, another for the second matrix and a third for where the result will be stored.

Matrix addition and subtraction is trivial. You may need more time to think about matrix multiplication however, there are so many example already on the net you should have no problem.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: adding matrices in c++

 
0
  #3
Jul 5th, 2006
Also be careful how you pass an array back to main. The syntax is slightly tricky.

And you may have to test if you are reading the information from your file into your 2d array properly.

Try changing this:-

  1. matsum[5][5]=(x[row][col]+y[row][col]);

to this:-

  1. matsum[row][col]=(x[row][col]+y[row][col]);
Last edited by iamthwee; Jul 5th, 2006 at 6:21 pm.
*Voted best profile in the world*
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
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC