Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 17th, 2005, 10:56 PM   #1
strider496
Newbie
 
Join Date: Mar 2005
Posts: 10
Rep Power: 0 strider496 is on a distinguished road
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
strider496 is offline   Reply With Quote
Old Mar 17th, 2005, 11:26 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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)
instead of num1>=-1 because that would include -1, and the loop wouldn't end when you inputted -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.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Mar 19th, 2005, 11:03 PM   #3
strider496
Newbie
 
Join Date: Mar 2005
Posts: 10
Rep Power: 0 strider496 is on a distinguished road
how do i add num1 to totalgallon?
how do i add num2 to totalmiles?
strider496 is offline   Reply With Quote
Old Mar 19th, 2005, 11:15 PM   #4
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
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
gardon is offline   Reply With Quote
Old Mar 20th, 2005, 12:57 AM   #5
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Quote:
Originally Posted by strider496
how do i add num1 to totalgallon?
totalgallon = totalgallon + num1;

Or you could shorthand it by:

totalgallon += num1;

Quote:
how do i add num2 to totalmiles?
totalmiles = totalmiles + num2;

or

totalmiles += num2;
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Mar 21st, 2005, 1:00 PM   #6
strider496
Newbie
 
Join Date: Mar 2005
Posts: 10
Rep Power: 0 strider496 is on a distinguished road
thx mjordan
strider496 is offline   Reply With Quote
Old Mar 21st, 2005, 2:58 PM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
No problem. Any time.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC