The loop seems to work ok but when trying to resolve the overall average part I'm scratching my head trying to figure out what variable to define or how to get the right answer to arrive at 17.4 for 4 tanks. Example or suggestion? Yes, this is homework.
/ This program calculates kilometers per liter for each of the four tanks and then averages the four tanks by kilometers per liter /
include <stdio.h>
int main(void)
{
/ Declare variables. /
int i;
float km;
float liter;
float kmpl;
float average;
printf ("\nWelcome to the Sears kilometers per liter calculator.\n\n");
/ Comment about the function of the program. /
printf ("The program will calculate kilometers per liter for 4 tanks\n");
printf ("after you have entered the kilometers driven and liters used.\n\n");
for( i=1; i<=4; ++i ) / Define loop. /
{
/ Enter and display kilometers driven. /
printf ("Enter the number of kilometers driven for tank #%i: \n", i);
scanf ("%f", &km);
/ Enter and display liters of fuel used in tanks. /
printf ("Enter the number of liters used for tank #%i: \n", i);
scanf ("%f", &liter);
kmpl = km /liter;
/ Display kilometers per liter for each tank. /
printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
}
average = kmpl / 4;
/ Display overall average kilometers per liter /
printf ("Your overall average kilometers per liter for 4 tanks is %.1f \n\n", kmpl, average);
/ Comment thanking user for executing program. /
printf ("Thanks for using the Sears KPL calculator program.\n\n");
return 0;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
> printf ("Your overall average kilometers per liter for 4 tanks is %.1f\n\n",
> totalKm, totalLiter, average);
Three arguments but only one format specifier.
I suggest that you add teh following to your compiler options (habitually):
-Wall -Werror -Wformat
It will allow the compiler to perform stronger error checking on your code, but specifically it would have trapped the formatted I/O error. Let teh compiler help you write better code.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This might be a little better but I still don't have the variables defined just right...can someone point me in the right direction? Am I close to the answer?
include <stdio.h>
int main(void)
{
/ Declare variables. /
int i, totalKm, totalLiter;
float km;
float liter;
float kmpl;
float average;
printf ("\nWelcome to the Sears kilometers per liter calculator.\n\n");
/ Comment about the function of the program. /
printf ("The program will calculate kilometers per liter for 4 tanks\n");
printf ("after you have entered the kilometers driven and liters used.\n\n");
for( i=1; i<=4; ++i ); / Define loop. /
{
/ Enter and display kilometers driven. /
printf ("Enter the number of kilometers driven for tank #%i: \n", i);
scanf ("%f", &km);
/ Enter and display liters of fuel used in tanks. /
printf ("Enter the number of liters used for tank #%i: \n", i);
scanf ("%f", &liter);
kmpl = km /liter;
totalKm =
totalLiter =
/ Display kilometers per liter for each tank. /
printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
}
average = totalKm / totalLiter;
/ Display overall average kilometers per liter /
printf ("Your overall average kilometers per liter for 4 tanks is %.1f \n\n", average);
/ Comment thanking user for executing program. /
printf ("Thanks for using the Sears KPL calculator program.\n\n");
return 0;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
printf ("Welcome to the Sears kilometers per liter calculator.\n\n");
printf ("The program will calculate kilometers per liter for 4 tanks\n");
printf ("after you have entered the kilometers driven and liters used.\n\n");
for( i=1; i<=4; i++ )
{
printf ("Enter the number of kilometers driven for tank #%i: \n", i);
scanf ("%f", &km);
printf ("Enter the number of liters used for tank #%i: \n", i);
scanf ("%f", &liter);
kmpl = km /liter;
km <= totalKm;
liter <= totalLiter;
printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
}
average = totalKm / totalLiter;
printf ("Your overall average kilometers per liter for 4 tanks is %.1f\n\n", average);
printf ("Thanks for using the Sears KPL calculator program.\n\n");
return 0;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
They are boolean expressions that will evaluate to true or false and then be discarded since they are not assigned to anything.
Perhaps you intended:
totalKm += km ;
totalLiter + liter ;
but I have no idea what you might have thought you were doing!?
You will need to initialise the totals first, local variables have undefined initial values otherwise.
Note that you seemed to get pretty far on your own, to the point that it became difficult to know what to answer with your serial posting. Next time work at it a while until you are really stuck!
Previously you had:
totalKm =
totalLiter =
/ Display kilometers per liter for each tank. /
printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
which is identical to :
totalKm = totalLiter = printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
which would have caused both totals to contain a value equal to the number of characters displayed by the printf()!
Just think, and read your code.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hopefully this closer to the correct code? The totalKm = and the totalLiter = were place holders where I wasn't sure what to put beside them. You're right, the <= was nonsense, I don't know why I thought that might be plausible.
The loop works but I'm try to get an overall average of the 4 tanks to equal 17.4 and I'm missing the mark. What am I screwing up, suggestions?
printf ("Welcome to the Sears kilometers per liter calculator.\n\n");
printf ("The program will calculate kilometers per liter for 4 tanks\n");
printf ("after you have entered the kilometers driven and liters used.\n\n");
for( i=1; i<=4; i++ )
{
printf ("Enter the number of kilometers driven for tank #%i: \n", i);
scanf ("%f", &km);
fflush(stdin);
printf ("Enter the number of liters used for tank #%i: \n", i);
scanf ("%f", &liter);
fflush(stdin);
kmpl = km / liter;
totalKm = km + totalKm;
totalLiter = liter + totalLiter;
printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
}
average = (float) totalKm / totalLiter;
printf ("Your overall average kilometers per liter for 4 tanks is %.1f\n\n", totalKm, totalLiter, average);
printf ("Thanks for using the Sears KPL calculator program.\n\n");
return 0;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The loop seems to work ok but when trying to resolve the overall average part I'm scratching my head trying to figure out what variable to define or how to get the right answer to arrive at 17.4 for 4 tanks. Example or suggestion? Yes, this is homework.
/ This program calculates kilometers per liter for each of the four tanks and then averages the four tanks by kilometers per liter /
include <stdio.h>
int main(void)
{
/ Declare variables. /
int i;
float km;
float liter;
float kmpl;
float average;
printf ("\nWelcome to the Sears kilometers per liter calculator.\n\n");
/ Comment about the function of the program. /
printf ("The program will calculate kilometers per liter for 4 tanks\n");
printf ("after you have entered the kilometers driven and liters used.\n\n");
for( i=1; i<=4; ++i ) / Define loop. /
{
/ Enter and display kilometers driven. /
printf ("Enter the number of kilometers driven for tank #%i: \n", i);
scanf ("%f", &km);
/ Enter and display liters of fuel used in tanks. /
printf ("Enter the number of liters used for tank #%i: \n", i);
scanf ("%f", &liter);
kmpl = km /liter;
/ Display kilometers per liter for each tank. /
printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
}
average = kmpl / 4;
/ Display overall average kilometers per liter /
printf ("Your overall average kilometers per liter for 4 tanks is %.1f \n\n", kmpl, average);
/ Comment thanking user for executing program. /
printf ("Thanks for using the Sears KPL calculator program.\n\n");
return 0;
}
A few points.
> fflush(stdin);
Don't do that. The stanadard does not define fflush() for input streams, so it is a non-portable technique.
Use this:
while( getchar() != '\n' ){} ; / flush ot end-of-line /
> average = (float) totalKm / totalLiter;
No cast required here.
> printf ("Your overall average kilometers per liter for 4 tanks is %.1f\n\n",
> totalKm, totalLiter, average);
Three arguments but only one format specifier.
I suggest that you add teh following to your compiler options (habitually):
-Wall -Werror -Wformat
It will allow the compiler to perform stronger error checking on your code, but specifically it would have trapped the formatted I/O error. Let teh compiler help you write better code.
Clifford
Am I'm missing some calculations for total kilometers and total liters? Examples?
This might be a little better but I still don't have the variables defined just right...can someone point me in the right direction? Am I close to the answer?
include <stdio.h>
int main(void)
{
/ Declare variables. /
int i, totalKm, totalLiter;
float km;
float liter;
float kmpl;
float average;
printf ("\nWelcome to the Sears kilometers per liter calculator.\n\n");
/ Comment about the function of the program. /
printf ("The program will calculate kilometers per liter for 4 tanks\n");
printf ("after you have entered the kilometers driven and liters used.\n\n");
for( i=1; i<=4; ++i ); / Define loop. /
{
/ Enter and display kilometers driven. /
printf ("Enter the number of kilometers driven for tank #%i: \n", i);
scanf ("%f", &km);
/ Enter and display liters of fuel used in tanks. /
printf ("Enter the number of liters used for tank #%i: \n", i);
scanf ("%f", &liter);
kmpl = km /liter;
totalKm =
totalLiter =
/ Display kilometers per liter for each tank. /
printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
}
average = totalKm / totalLiter;
/ Display overall average kilometers per liter /
printf ("Your overall average kilometers per liter for 4 tanks is %.1f \n\n", average);
/ Comment thanking user for executing program. /
printf ("Thanks for using the Sears KPL calculator program.\n\n");
return 0;
}
OK, I think I'm getting it down but I still can't come to the overall average...what am I doing wrong?
include <stdio.h>
int main(void)
{
int i;
float km;
float liter;
float kmpl;
float totalKm;
float totalLiter;
float average;
printf ("Welcome to the Sears kilometers per liter calculator.\n\n");
printf ("The program will calculate kilometers per liter for 4 tanks\n");
printf ("after you have entered the kilometers driven and liters used.\n\n");
{
printf ("Enter the number of kilometers driven for tank #%i: \n", i);
scanf ("%f", &km);
printf ("Enter the number of liters used for tank #%i: \n", i);
scanf ("%f", &liter);
kmpl = km /liter;
km <= totalKm;
liter <= totalLiter;
printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
}
average = totalKm / totalLiter;
printf ("Your overall average kilometers per liter for 4 tanks is %.1f\n\n", average);
printf ("Thanks for using the Sears KPL calculator program.\n\n");
return 0;
}
This is nonsense:
km <= totalKm;
liter <= totalLiter;
They are boolean expressions that will evaluate to true or false and then be discarded since they are not assigned to anything.
Perhaps you intended:
totalKm += km ;
totalLiter + liter ;
but I have no idea what you might have thought you were doing!?
You will need to initialise the totals first, local variables have undefined initial values otherwise.
Note that you seemed to get pretty far on your own, to the point that it became difficult to know what to answer with your serial posting. Next time work at it a while until you are really stuck!
Previously you had:
totalKm =
totalLiter =
/ Display kilometers per liter for each tank. /
printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
which is identical to :
totalKm = totalLiter = printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
which would have caused both totals to contain a value equal to the number of characters displayed by the printf()!
Just think, and read your code.
Clifford
Thanks Clifford,
Hopefully this closer to the correct code? The totalKm = and the totalLiter = were place holders where I wasn't sure what to put beside them. You're right, the <= was nonsense, I don't know why I thought that might be plausible.
The loop works but I'm try to get an overall average of the 4 tanks to equal 17.4 and I'm missing the mark. What am I screwing up, suggestions?
include <stdio.h>
int main(void)
{
/ Declare variables. /
int i;
float km;
float liter;
float kmpl;
float totalKm = 0;
float totalLiter = 0;
float average;
printf ("Welcome to the Sears kilometers per liter calculator.\n\n");
printf ("The program will calculate kilometers per liter for 4 tanks\n");
printf ("after you have entered the kilometers driven and liters used.\n\n");
for( i=1; i<=4; i++ )
{
printf ("Enter the number of kilometers driven for tank #%i: \n", i);
scanf ("%f", &km);
fflush(stdin);
printf ("Enter the number of liters used for tank #%i: \n", i);
scanf ("%f", &liter);
fflush(stdin);
kmpl = km / liter;
totalKm = km + totalKm;
totalLiter = liter + totalLiter;
printf ("*** The kilometers per liter for tank %i is %.1f\n\n", i, kmpl);
}
average = (float) totalKm / totalLiter;
printf ("Your overall average kilometers per liter for 4 tanks is %.1f\n\n", totalKm, totalLiter, average);
printf ("Thanks for using the Sears KPL calculator program.\n\n");
return 0;
}
Clifford,
I got it, thanks for your help.