C/PLEASE HELP
Expert: Joydeep Bhattacharya - 3/5/2007
QuestionI cannot figure this out!!!!!
Write a C program that determines the kilometers per liter for 4 tanks of gasoline that a user fills in his/her car.
We will explain to the user very clearly what the program will be doing and how they should enter data.
The program should prompt the user to enter the number of kilometers driven, and the number of liters used for each of the 4 tanks of gas. The program should then calculate and display the kilometers per liter obtained for each tank. Once processing is complete for the 4 tanks, the program will calculate the overall kilometers per liter (total liters / total kilometers) and display a friendly "Goodbye" message as shown below.
Once you create, compile, link and run your program, your program should present the following dialog to the user:
Welcome to the Sears kilometers per liter calculator.
The program will calculate kilometers per liter for 4 tanks for you after
you have entered the kilometers driven and liters used.
Enter the number of kilometers driven for tank #1: 200
Enter the number of liters used for tank #1: 12.1
*** The kilometers per liter for tank 1 is 16.5
Enter the number of kilometers driven for tank #2: 300
Enter the number of liters used for tank #2: 13.5
*** The kilometers per liter for tank 2 is 22.2
Enter the number of kilometers driven for tank #3: 120
Enter the number of liters used for tank #3: 8
*** The kilometers per liter for tank 3 is 15.0
Enter the number of kilometers driven for tank #4: 122
Enter the number of liters used for tank #4: 9.1
*** The kilometers per liter for tank 4 is 13.4
Your overall average kilometers per liter for 4 tanks is 17.4
Thanks for using the Sears KPL calculator program.
Note: The coral text represents the "output" from your program and is shown for clarity only here. It is not required that your output be in this color. (Do not even attempt to try!). Also note that what the user types in is indicated by the blue area above. Again, this is for clarity only. The first two lines may be any suitable message to the user. After that the program should run as above. The output spacing is important.
I did not provide an algorithm for this assignment; however I have included several hints below. You are not required to reference these hints for your solution. They are here if you need some help. It is okay to completely ignore them, if you so choose. Some students have told me that the hints actually confused them! :-)
Hints:
* This program requires 1 variable of type int, and 5 or 6 of type float.
* This program requires an introductory statement to the user.
* This program requires a loop structure (for, while or do -- your choice).
* The loop requires 3 printf statements: 1 to prompt the user for the "number of kilometers driven"; 1 to prompt for "number of liters used"; and a third to display the calculated "kilometers per gallon for this tank".
* The loop also requires 2 scanf statements: 1 to "read in" the number of kilometers driven which is input by the user; and a second to "read in" the number of liters used, which is also input by the user.
* This program requires 3 calculations within the loop - one for the current kpl calculation, and two accumulators to add up the total kilometers and total liters used for the final calculation.
* This program requires another printf statement (outside the for loop) to display the average for all tanks.
* The loop index is used to display the current tank calculation.
AnswerHi Mark
thanks for the hints though it was not needed anyways here is the program for you;
#include<stdio.h>
#include<conio.h>
void main()
{
int km, i;
float res, lt;
clrscr();
printf("Welcome to the Sears kilometers per liter calculator.\n\n");
for(i=1;i<=4;i++)
{
printf("\n\nEnter the number of kilometer for tank %d#: ",i);
scanf("%d",&km);
printf("Enter the number of liters used by tank %d#: ",i);
scanf("%f",<);
res = (float)km / lt;
printf("*** The kilometers per liter for tank %d# is %.1f",i,res);
}
getch();
}
If you stuck anywhere please feel free to ask me..... and I guess this will output what you have asked for.
regards
Joydeep Bhattacharya