![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 10
Rep Power: 0
![]() |
help please!
I need to write a program that input the miles driven and gallons used . The program should calculate and display the miles per gallon. After processing all input information, the program should calculate and print the combined miles per gallon obtain for all tankful.
Here what i got so far
#include <stdio.h>
int main()
{
int num1,num2,miles/gallon,Avgmile/gallon;
printf("Enter the gallons used\n");
scanf("%d",num1);
while (num1>=-1) {
printf("Enter miles driven\n");
scanf("%d",num2);
miles/gallon=(num1/num2);
printf("Miles per gallon is %d\n", miles/gallon);
}
while (num1<=-1) {
Avgmile/gallon=??????? /*no idea what this should be*/
printf("Avgmile/gallon is %d", Avgmile/gallon);
}
return 0;
}Run result shoud be something like this.. Enter the gallon used (-1 to end): 12.8 Enter the miles driven:287 The miles/gallon for this tank was 22.421875 Enter the gallon used (-1 to end): 10.3 Enter the miles driven:200 The miles/gallon for this tank was 19.417475 Enter the gallons used (-1 to end): -1 Overall average miles/gallon was 20.919675 Please help me i have no idea how to write this program |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Ok, well, you only really need four numbers. I would use float or double as gallons and total gallon instead of ints, so to be more precise. Have num1 and num2, just as you have. Then have totalGallons, which will hold the sum of all the inputs so far, and then have a totalMiles variable.
Your while loop should be while(num1>-1) Inside the while loop, first of all, add num1 to totalGallons. Ask for num2. Add num2 to total miles. There's no need for that second loop. Simply print out miles per gallon by totalGallons/totalMiles. Use a float to be more precise.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 10
Rep Power: 0
![]() |
how do i add num1 to totalgallon?
how do i add num2 to totalmiles? |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
I'd help you, but I am not familiar with C, but c++
However, I'd write you a progarm in c++ if you wanted me to, but I got work in the morning so I have to go to bed. It's 11:15 pm right here where I am, and I'll be back on in 15 hours then. Peace out dude |
|
|
|
|
|
#5 | ||
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Quote:
Or you could shorthand it by: totalgallon += num1; Quote:
or totalmiles += num2;
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
||
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2005
Posts: 10
Rep Power: 0
![]() |
thx mjordan
|
|
|
|
|
|
#7 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
No problem. Any time.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|