使用相同的变量从C中的while循环获得平均值

时间:2017-06-05 19:19:46

标签: c while-loop average

我有一个变量total,每次用户输入时都会存储里程和加仑的总数。

当用户点击-1时,程序假设将所有总计加在一起并取平均值。 我不能让变量在循环的下一次迭代中将先前的总值添加到自身。

我试过了totals += totals;但是它返回了循环的最后一个总值?

我做错了什么?

#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

3 个答案:

答案 0 :(得分:2)

你需要做的是有一个变量来累计所有总数,然后当循环完成时,将数字除以你的avgConsumption变量。

以下是对您的计划的略微修改:

#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

我所做的是使用总计来累计你在循环中收到的所有英里/加仑。我使用局部变量curTotal来存储实际的英里/加仑计算。对于循环的每次迭代,都会重置此变量。循环结束后,我计算了avgConsumption,而不是在循环中计算它产生不同的结果。这为您提供了用户报告的每加仑平均英里数。

答案 1 :(得分:0)

我认为你没有得到正确的&#34;总计&#34;在所有

每次重复while循环时,它都会被覆盖。

制作另一个变量以保持&#34;总数&#34;和一个你可以存储的变量&#34;英里/加仑&#34; by - like&#39; MPG&#39;

然后在最后你可以做Total = Total + MPG。

答案 2 :(得分:0)

  

我做错了什么?

代码错误计算总数。

要平均多个商数,例如许多miles_per_gallon(MPG),通常的方法是除以total_miles / total_gallons。这不是平均MPG 唯一定义,但我确定最好在这里使用。 @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