-5

Are there any C language programmers on here that can help me figure this out? I am having trouble getting the calculation for the average miles per gallon to work and my head is spinning. I would really appreciate if anyone have a solution ^_^

int x, number_of_tanks = 3;
double total_num1, total_num2;
double total_miles_per_gallon;
float division, avg;
float num1, num2;

for (x = 1; x <= 3; x++)
{
    printf("Enter the number of gallons used for tank #%i: ",x);
    scanf("%f", &num1);
    fflush(stdin);      /* clear input buffer */

    printf("Enter the number of miles driven: ");
    scanf("%f", &num2);
    fflush(stdin);      /* clear input buffer */

    /*--------------------------------------------------------------*/
    /* calculate and output the miles per gallon from user input.   */
    /* ------------------------------------------------------------ */

    division = num2 / (float) num1;                            
    printf("The miles per gallon for this tank %.1f divided by %.1f is %.1f", \
        num2, num1, division);

    total_num2 = total_num2 + num2;
    printf("The total of miles is %f\n", total_num2);

    total_num1 = total_num1 + num1;
    printf("The total of gallons is %f\n", total_num1);
}

avg = (double) total_num2 / total_num1; 
printf("Overall average miles per gallon for three tanks: %.1f", avg);
icedwater
  • 4,280
  • 3
  • 31
  • 47
  • Please be more specific - what is the problem? Also, thanks for including the code, but a lot of it isn't directly relevant to the problem... I'll propose a few edits... – icedwater Jun 28 '13 at 02:50
  • 2
    You dont post your homework and ask for anwsers here. – 蒋艾伦 Jun 28 '13 at 02:51
  • 1
    You could start by giving your variables meaningful names. E.g. rename `total_num2` to `total_miles`. And `division ` might be `mpg_this_tank`. – Bull Jun 28 '13 at 02:52
  • 2
    What exactly is the problem? The calculation looks fine, unless I can convince you to use the metric system upon which modern society is based. – paddy Jun 28 '13 at 02:53
  • 1
    FYI, `fflush(stdin)` does not have defined behaviour. Don't do it. See here: http://stackoverflow.com/questions/2979209/using-fflushstdin – paddy Jun 28 '13 at 03:06
  • @AaronJiang Please point out something in SO's Help that says that people can't get help with their homework here. The problem with the question is lack of detail about what it's doing wrong and how it differs from the OP's expectations, not that it's homework. – Jim Balter Jun 28 '13 at 03:53

1 Answers1

1

You don't initialise your totals, so they are undefined. When you start adding to them, you get undefined results. I bet that's what you mean by it not working.

Do this:

double total_num1 = 0;
double total_num2 = 0;
paddy
  • 52,396
  • 6
  • 51
  • 93
  • You could be right, but @SanoAlexander should probably specify what isn't working. – icedwater Jun 28 '13 at 03:04
  • my apologizes if i didn't explain the problem I am having. I am having trouble with the calculation to find the Average Miles Per Gallon. the user input is accepted but the average is giving the correct answer – Sano Alexander Jun 28 '13 at 03:18
  • @paddy thanks for showing me my error, I followed what you said and the results are correct. I really appreciate the help from you and everyone else ^_^ – Sano Alexander Jun 28 '13 at 03:32