| | |
Calculation problems..
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
This program is suppose to display MPG's (total), and the combined MPG of all the total MPG's you input.
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
C++ Syntax (Toggle Plain Text)
// Exercise 4. 12: ex04_12.cpp #include <iostream> using namespace std; int main() { double mpg; double gallons; double combinedMpg = 1; double total; while ( mpg != -1 ) { cout << "\nEnter miles (Enter -1 to quit): "; cin >> mpg; if ( mpg != -1) { cout << "Enter gallons: "; cin >> gallons; } cout << "\nMPG this tankful: " << (total = (mpg / gallons)) << endl; cout << "Total MPG: " << (combinedMpg = total / combinedMpg ) << endl; } }
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; 22 Days Ago at 1:19 am.
0
#2 22 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".
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
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#3 22 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 / gallonsbetter 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 sayingoverall_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.
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#4 22 Days Ago
still couldnt get it. I revised it and this is where i'm at.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { double miles; double gallons; double combinedMpg = 1; double mpg; double averageMpgs; while ( mpg != -1 ) { cout << "\nEnter miles (Enter -1 to quit): "; cin >> miles; if ( miles != -1) { cout << "Enter gallons: "; cin >> gallons; cout << "\nMPG this tankful: " << (mpg = (miles / gallons)) << endl; cout << "Total MPG: " << (averageMpgs = mpg / combinedMpg ) << endl; } averageMpgs += combinedMpg; } }
![]() |
Similar Threads
- Wage / employee self generated list / calculation (C++)
- Problems with Pi and also Calculation Problem (Java)
- Demographic database design help (Database Design)
- Problems with reading from a sequential file (C++)
- I'm having problems (Java)
- PDB analysing (Perl)
- First JLIST, used with GUI Inventory (Java)
- No time... bitch load of problems (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: operator= changes value just for scope of function
- Next Thread: How do I set shared ptr to NULL
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






