2017-06-05 101 views
0

我有一個變量total,每次用戶輸入時都會存儲總里程和加侖數。使用相同變量從C語言while循環中獲得平均值

當用戶點擊-1時,程序會假設將所有總數加在一起並取平均值。 我無法獲得該變量,以便爲循環的下一次迭代添加前一個總值。我試過,但是它返回循環的最後總值?

我在做什麼錯?

#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 
+5

'總額+ = totals'是一樣的由2乘以'totals'我敢肯定平均值不同 –

+0

什麼,我的意思是說了,這裏的平均是在「avgConsumption =總計計算計算/ numOfEntries「,其中totals是循環的所有迭代中總計的總數,numOfEntries是用戶被要求輸入總數的次數 – Rgoat

+0

而且正如我所說的,」總計「計算不正確 –

回答

0

我不認爲你得到正確的「總」在所有

它讓你每次經過while循環重申時間覆蓋。

讓另一個變量舉辦「總」,而您可以通過存儲「英里/加侖」一個變量 - 像「省油」

那麼,在年底,你可以做總=總+ MPG。

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來存儲實際的英里/加侖計算。該變量在循環的每次迭代中都被重置。 th循環結束後,我計算了avgConsumption,而不是在產生不同結果的循環中計算它。這給你用戶報告的每加侖平均英里數。

+0

感謝您的解釋和幫助! – Rgoat

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