Equation (sum) doesnt add up properly.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 25
Reputation: xiikryssiix is on a distinguished road 
Solved Threads: 0
xiikryssiix xiikryssiix is offline Offline
Light Poster

Equation (sum) doesnt add up properly.

 
0
  #1
Aug 25th, 2009
hello everyone...

i wrote a basic program for my C++ class that includes SUMs and while loops.
its fairly self explanatory, but i am not getting the correct SUMs for my outputs.
apparently, i am missing something extremely important somewhere here.
can someone take a look and give me a few hints as to why my outputs are coming out the way they are...

thank you.

note: why i have so much variables here is because my teacher wants it that way. i would use an array to make the program look neater, but i was instructed to keep it as simple and as basic as possible. =)


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int numX;
  7. int numY;
  8. int numZ;
  9.  
  10. int sum1 = numX - numZ;
  11. int sum2 = numX - numY + numZ;
  12. int sum3 = numX + numY;
  13.  
  14. int sum4 = numX + numY + numZ;
  15. int sum5 = numX;
  16. int sum6 = numX - numY - numZ;
  17.  
  18. int sum7 = numX - numY;
  19. int sum8 = numX + numY - numZ;
  20. int sum9 = numX + numZ;
  21.  
  22. int totalA = sum1 + sum2 + sum3;
  23. int totalB = sum4 + sum5 + sum6;
  24. int totalC = sum7 + sum8 + sum9;
  25. int totalD = sum1 + sum4 + sum7;
  26. int totalE = sum2 + sum5 + sum8;
  27. int totalF = sum3 + sum6 + sum9;
  28.  
  29. while (cin)
  30. {
  31.  
  32. cout << "Please enter 3 integers separated by spaces: (enter non-digit to quit)" << endl;
  33. cin >> numX >> numY >> numZ;
  34. cin.ignore(1000, 10);
  35.  
  36. cout << "The magic square is:" << endl;
  37. cout << sum1 << sum2 << sum3 << " = " << totalA << endl;
  38. cout << sum4 << sum5 << sum6 << " = " << totalB << endl;
  39. cout << sum7 << sum8 << sum9 << " = " << totalC << endl;
  40. cout << "---------------" << endl;
  41. cout << totalD << totalE << totalF << endl;
  42. cout << " " << endl;
  43. cout << " " << endl;
  44.  
  45. }
  46.  
  47. cin.get();
  48. return 0;
  49. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 16
Reputation: Agent-of-Chaos is an unknown quantity at this point 
Solved Threads: 1
Agent-of-Chaos Agent-of-Chaos is offline Offline
Newbie Poster

Re: Equation (sum) doesnt add up properly.

 
0
  #2
Aug 25th, 2009
Hi... it seems that it is precedence problem of operations....you are not using parenthesis when you are performing two operations at the same time subtraction and addition... do it this way int sum2 = (numX - numY) + numZ; hope it is going to solve your problem.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Equation (sum) doesnt add up properly.

 
0
  #3
Aug 25th, 2009
Move this

  1. int sum1 = numX - numZ;
  2. int sum2 = numX - numY + numZ;
  3. int sum3 = numX + numY;
  4.  
  5. int sum4 = numX + numY + numZ;
  6. int sum5 = numX;
  7. int sum6 = numX - numY - numZ;
  8.  
  9. int sum7 = numX - numY;
  10. int sum8 = numX + numY - numZ;
  11. int sum9 = numX + numZ;
  12.  
  13. int totalA = sum1 + sum2 + sum3;
  14. int totalB = sum4 + sum5 + sum6;
  15. int totalC = sum7 + sum8 + sum9;
  16. int totalD = sum1 + sum4 + sum7;
  17. int totalE = sum2 + sum5 + sum8;
  18. int totalF = sum3 + sum6 + sum9;

behind the

  1. cin.ignore(1000, 10);
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,328
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 166
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Equation (sum) doesnt add up properly.

 
0
  #4
Aug 25th, 2009
If you didn't realize this code :
  1. int numX;
  2. int numY;
  3. int numZ;
  4.  
  5. int sum1 = numX - numZ;
  6. int sum2 = numX - numY + numZ;
  7. int sum3 = numX + numY;
  8.  
  9. int sum4 = numX + numY + numZ;
  10. int sum5 = numX;
  11. int sum6 = numX - numY - numZ;
  12.  
  13. int sum7 = numX - numY;
  14. int sum8 = numX + numY - numZ;
  15. int sum9 = numX + numZ;
  16.  
  17. int totalA = sum1 + sum2 + sum3;
  18. int totalB = sum4 + sum5 + sum6;
  19. int totalC = sum7 + sum8 + sum9;
  20. int totalD = sum1 + sum4 + sum7;
  21. int totalE = sum2 + sum5 + sum8;
  22. int totalF = sum3 + sum6 + sum9;

Is useless because numX,numY,numZ is not initialized. You need
to first get num* then calculate it. What you are doing is
calculating junk, then getting num* then showing the junk you
calculated.

Again get user input first into numX,numY,numZ, then calculated
whatever you need to calculate.

-------
num* = numX, numY, numZ
Last edited by firstPerson; Aug 25th, 2009 at 3:09 pm.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 25
Reputation: xiikryssiix is on a distinguished road 
Solved Threads: 0
xiikryssiix xiikryssiix is offline Offline
Light Poster

Re: Equation (sum) doesnt add up properly.

 
0
  #5
Aug 26th, 2009
hey thanks for all the help. fixed it and all works well now. =) thank yous!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC