Rgoat
Rgoat

Reputation: 85

get average from while loop in C using the same variable

I have a variable total that stores the total of miles and gallons every time user enters it.

When user hits -1, the program is suppose to add all the totals together and take the average. I can't get the variable to add the previous total value to itself for the next iteration of the loop.

I tried totals += totals; but that returns the last total value of the loop?

what am i doing wrong?

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <conio.h>

const float EXIT_VALUE = -1.0f;

int main(void)
{
    float gallons; // number of gallons used
    float miles; // number of miles driven
    float totals; // total = miles * gallons
    int numOfEntries = 0; // number of entries 
    float avgConsumption = 0; // avg of all entries made by user

    // get number of gallons from user
    printf("%s%.1f%s", "Enter the gallons used (", EXIT_VALUE, " to end): ");
    scanf("%f", &gallons);

    // loops until user enter -1 
    while (gallons != EXIT_VALUE) {
        //miles driven by user
        printf("%s", "Enter the miles driven: "); 
        scanf("%f", &miles);

        // calculate total miles per gallon
        totals = miles/gallons;
        printf("The miles/gallons for this tank was %.6f\n\n", totals);

        // get number of gallons from user
        printf("%s%.2f%s", "Enter the gallons used (", EXIT_VALUE, " to end): ");
        scanf("%f", &gallons);

        totals += totals;
        numOfEntries++;
        avgConsumption = totals / numOfEntries;

    } // end while
    printf("\nThe overall average miles/gallon was %.6f: ", avgConsumption);

    _getch();
    return 0;

} // end main

Upvotes: 0

Views: 941

Answers (3)

chux
chux

Reputation: 153367

what am i doing wrong?

Code is mis-calculating totals.

To average multiple quotients like many miles_per_gallon (MPG), usually the approach is to divide the total_miles/total_gallons. This is not the only definition of the average MPG yet I am certain the best to use here. @Yunnosch

// Pseudo code
total_miles = 0.0
total_gallons = 0.0
while (still_getting_input)
  miles = get_input();
  gallons = get_input();
  print miles, gallons, miles/gallon

  total_miles += miles
  total_gallons += gallons

print total_miles, total_gallons, total_miles/total_gallon

Upvotes: 0

Jayson Boubin
Jayson Boubin

Reputation: 1504

What you need to do is have a variable that adds up all of the totals, and then when the loop is complete, divides the number by your avgConsumption variable.

Here is a slight modification of your program:

#include <stdio.h>

const float EXIT_VALUE = -1.0f;

int main(void)
{
    float gallons; // number of gallons used
    float miles; // number of miles driven
    float totals = 0; // total = miles * gallons
    int numOfEntries = 0; // number of entries
    float avgConsumption = 0; // avg of all entries made by user

    // get number of gallons from user
    printf("%s%.1f%s", "Enter the gallons used (", EXIT_VALUE, " to end): ");
    scanf("%f", &gallons);

    // loops until user enter -1
    while (gallons != EXIT_VALUE) {
        //miles driven by user
        printf("%s", "Enter the miles driven: ");
        scanf("%f", &miles);

        // calculate total miles per gallon
        float curTotal = miles/gallons;
        printf("The miles/gallons for this tank was %.6f\n\n", curTotal);

        // get number of gallons from user
        printf("%s%.2f%s", "Enter the gallons used (", EXIT_VALUE, " to end): ");
        scanf("%f", &gallons);

        totals += curTotal;
        numOfEntries++;

    } // end while
    avgConsumption = totals / numOfEntries;
    printf("\nThe overall average miles/gallon was %.6f: ", avgConsumption);


    return 0;

} // end main

What I did was use totals to add up all of the miles/gallons you received in your loop. I used a local variable curTotal to store the actual miles/gallons calculation. This variable is reset for every iteration of the loop. After th loop concludes, I calculated avgConsumption, instead of calculating it in the loop which produces different results. This gives you the average number of miles per gallon reported by users.

Upvotes: 2

mhn_ngn
mhn_ngn

Reputation: 1

I dont think you are getting the right "total" at all

It is getting over-written every time you reiterate through the while-loop.

Make another variable to hold the "total" and one variable that you can store "miles/gallons" by -- like 'MPG'

Then at the end you can do Total = Total + MPG.

Upvotes: 0

Related Questions