944,171 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 32773
  • C++ RSS
Jul 5th, 2006
0

adding matrices in c++

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amethystglow is offline Offline
3 posts
since Jun 2006
Jul 5th, 2006
0

Re: adding matrices in c++

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 5th, 2006
0

Re: adding matrices in c++

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:-

C++ Syntax (Toggle Plain Text)
  1. matsum[5][5]=(x[row][col]+y[row][col]);

to this:-

C++ Syntax (Toggle Plain Text)
  1. matsum[row][col]=(x[row][col]+y[row][col]);
Last edited by iamthwee; Jul 5th, 2006 at 6:21 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 19th, 2011
0
Re: adding matrices in c++
hi dear web master i have read this page(adding 2 matrices)and i had no problem to underestanding it ..but i need more complecated programs regarding to matrices(more profetional programs )
sincerly thanks..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shamadgff is offline Offline
1 posts
since Aug 2011

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: Socket IF Statement
Next Thread in C++ Forum Timeline: constructor with pointer or not?





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


Follow us on Twitter


© 2011 DaniWeb® LLC