Calculation problems..

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

Join Date: Oct 2009
Posts: 5
Reputation: BenJammin89 is an unknown quantity at this point 
Solved Threads: 0
BenJammin89 BenJammin89 is offline Offline
Newbie Poster

Calculation problems..

 
0
  #1
20 Days Ago
This program is suppose to display MPG's (total), and the combined MPG of all the total MPG's you input.
  1. // Exercise 4. 12: ex04_12.cpp
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. double mpg;
  8. double gallons;
  9. double combinedMpg = 1;
  10. double total;
  11.  
  12.  
  13. while ( mpg != -1 )
  14. {
  15. cout << "\nEnter miles (Enter -1 to quit): ";
  16. cin >> mpg;
  17.  
  18. if ( mpg != -1)
  19. {
  20. cout << "Enter gallons: ";
  21. cin >> gallons;
  22. }
  23. cout << "\nMPG this tankful: " << (total = (mpg / gallons)) << endl;
  24. cout << "Total MPG: " << (combinedMpg = total / combinedMpg ) << endl;
  25. }
  26. }

Any help?
Output should be along these lines...I just switched some of the words.

Enter the miles used (-1 to quit): 287
Enter gal l ons: 13
MPG this tankful : 22. 076923
Total MPG: 22. 076923

Enter the miles used (-1 to quit): 200
Enter gal l ons: 10
MPG this tankful : 20. 000000
Total MPG: 21. 173913

Enter the miles used (-1 to quit): 120
Enter gal l ons: 5
MPG this tankful : 24. 000000
Total MPG: 21. 678571
Last edited by BenJammin89; 20 Days Ago at 1:19 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,673
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
0
  #2
20 Days Ago
First, the code would be clearer if your variable names better expressed what they were.

What's "total"? The input of distance driven should be "miles", not "mpg". mpg = miles / gallons better expresses what you're doing.

The calculation of the mpg for a given tankful should occur inside the if condition - when the miles input is -1, you don't want to do a calculation, as would happen in your code.

For the overall mpg value, simply averaging the individual mpg calculations will not be accurate. And you're not doing that correctly. (Look at your code, you're saying overall_mpg = current_mpg / overall_mpg - does that make any sense?) You should keep a running total of miles driven and gallons used, and calculate the overall mpg with those totals.

Is it in the assignment to display the running overall mpg after each entry, or are you to display the summary after all input is concluded? If the latter case, the calculation and display of overall mpg should occur outside the loop.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: BenJammin89 is an unknown quantity at this point 
Solved Threads: 0
BenJammin89 BenJammin89 is offline Offline
Newbie Poster
 
0
  #3
20 Days Ago
Originally Posted by vmanes View Post
First, the code would be clearer if your variable names better expressed what they were.

What's "total"? The input of distance driven should be "miles", not "mpg". mpg = miles / gallons better expresses what you're doing.

The calculation of the mpg for a given tankful should occur inside the if condition - when the miles input is -1, you don't want to do a calculation, as would happen in your code.

For the overall mpg value, simply averaging the individual mpg calculations will not be accurate. And you're not doing that correctly. (Look at your code, you're saying overall_mpg = current_mpg / overall_mpg - does that make any sense?) You should keep a running total of miles driven and gallons used, and calculate the overall mpg with those totals.

Is it in the assignment to display the running overall mpg after each entry, or are you to display the summary after all input is concluded? If the latter case, the calculation and display of overall mpg should occur outside the loop.

haha yeah that was a dumb variable, since MPG is = miles per gallon.

ok well the assignment was to display the MPG of the entered tankful AND the average of all MPG's entered.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: BenJammin89 is an unknown quantity at this point 
Solved Threads: 0
BenJammin89 BenJammin89 is offline Offline
Newbie Poster
 
0
  #4
20 Days Ago
still couldnt get it. I revised it and this is where i'm at.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. double miles;
  7. double gallons;
  8. double combinedMpg = 1;
  9. double mpg;
  10. double averageMpgs;
  11.  
  12. while ( mpg != -1 )
  13. {
  14. cout << "\nEnter miles (Enter -1 to quit): ";
  15. cin >> miles;
  16.  
  17. if ( miles != -1)
  18. {
  19. cout << "Enter gallons: ";
  20. cin >> gallons;
  21. cout << "\nMPG this tankful: " << (mpg = (miles / gallons)) << endl;
  22. cout << "Total MPG: " << (averageMpgs = mpg / combinedMpg ) << endl;
  23. }
  24. averageMpgs += combinedMpg;
  25. }
  26. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC