2014-03-28 44 views
0

我試圖編寫一個程序來查找加權平均值(對於等級等)。 通過用戶輸入,我創建了兩個相同大小的數組,其中一個承載每個評分類別的權重,另一個將用戶評分保留在corressponding類別中。例如,如果一個學生的分數爲92%,而分數爲總分數的30%,那麼權重[1] = .3,分數[1] = .92。這樣,每個類別的平均值可以通過(權重[i] *分數[i])找到,其中i是在for循環中初始化的實例變量,以完全遍歷每個數組。這裏的問題在於找到累計平均值 - 我的程序寫入的是綜合它處理的每個平均值的值,例如我嘗試了代碼如average = average +(weights [i] * scores [i])來獲得最終累積值平均。然而,通過打印語句,我發現平均值被設置爲最後一次(權重[i] *分數[i])操作,而忽略以前的平均值(或將其取爲0)。例如,如果權重[] = {.3,.3,.4}和分數[] = {.4,.4,.4}。累計平均值應爲0.4,但我的程序告訴我.32,這是權重的最後一個值乘以分數的最後一個值。For/while循環沒有正確更新數值

methods.c:

#include <stdio.h> 
#include "methods.h" 

int howManyCategories() 
{ 
int categories = 0; 
int firstTime = 0; 

while(1) 
{ 
    if(firstTime == 0) 
    { 
    fprintf(stdout, "\nHow many categories are you currently graded on? "); 
    scanf("%d", &categories); 
    } 
    else 
    { 
    fprintf(stdout, "\nThat's not a valid value, try a positive integer. "); 
    scanf("%d", &categories); 
    } 

    firstTime++; 

    if(categories > 0) 
    { 
    return categories; 
    } 
} 
} 

double howMuchWeight(int categories) 
{ 
double weight = 0; 

while(1) 
{ 
    if(categories == 1) 
    { 
    fprintf(stdout, 
    "\nThis %d category accounts for how much of your grade? (.1, .85, 1 etc.) ", 
    categories); 
    scanf("%lf", &weight); 
    } 
    else 
    { 
    fprintf(stdout, 
    "\nThese %d categories account for how much of your grade? (.1, .85, 1 etc.) ", 
    categories); 
    scanf("%lf", &weight); 
    } 

    if(weight > 1 || weight <= 0) 
    { 
    fprintf(stdout, 
    "\nRemember, total weighting must be positive and no larger than 1" 
    " (100 percent). \nTry Again.\n"); 
    } 
    else 
    { 
    return weight; 
    } 
} 
} 

double findWeightsAndScores(int i, double categories, 
          double checkWeights, double totalWeight, 
          double scores[]) 
{ 
while(1) 
{ 
    double piece = 0; 
    int count = i + 1; 

    fprintf(stdout, "\nWeight for category %d: ", count); 
    scanf("%lf",&piece); 

    if(piece > 0 && piece < 1) 
    { 
    findScores(i, &scores[i]); 
    return piece; 
    } 
    else 
    { 
    fprintf(stdout, "\nRemember, weights must be positive and less than 1!\n"); 
    } 
} 
} 

void findScores(int i, double scores[]) 
{ 
int validScore = 0; 

while(validScore == 0) 
{ 
    int count = i + 1; 
    double score = 0; 

    fprintf(stdout, "Please enter your percentage score in category %d: ", count); 
    scanf("%lf", &score); 

    if(score >= 0 && score <= 1) 
    { 
    scores[i] = score; 
    validScore = 1; 
    } 
    else 
    { 
    fprintf(stdout, "\nRemember, scores in each category must be positive, no greater " 
    "than 1 (100 percent) and no less than 0, try again.\n\n"); 
    } 
} 
} 

double findAverage(int categories, double weights[], double scores[]) 
{ 
double average = 0; 

for(int i = 0; i <= categories - 1; i++) 
{ 
    average += (weights[i] * scores[i]); 
    fprintf(stdout, "%lf", average); 
} 

return average; 
} 

methods.h:

#ifndef METHODS_H_ 
#define METHODS_H_ 

int howManyCategories(); 
double howMuchWeight(int categories); 
double findWeightsAndScores(int i, double categories, 
     double checkWeights, double totalWeight, double scores[]); 
void findScores(int i, double scores[]); 
double findAverage(int categories, double weights[], double scores[]); 

#endif 

whatsmyGrade.c:

#include <stdio.h> 
#include "methods.h" 

int main() 
{ 
while(1) 
{ 
    /*Prompts for categories*/ 
    int categories = howManyCategories(); 
    int dataProvided = 1; 

    double checkWeights = 0; 
    double grade = 0; 
    double average = 0; 

    /*Prompts for total weight*/ 
    double totalWeight = howMuchWeight(categories); 

    double category[categories]; 
    double weights[categories]; 
    double scores[categories]; 

    /*Assign weights to each category and score in each category*/ 
    for(int i = 0; i <= categories - 1; i++) 
    {   
    /*Fill array for weights*/ 
    weights[i] = findWeightsAndScores(i, categories, checkWeights, totalWeight, 
    scores); 

    checkWeights = checkWeights + weights[i]; 

    if(checkWeights != totalWeight && i == categories - 1) 
    { 
    fprintf(stdout, "\nYour weight distribution is invalid! Try again.\n"); 
    dataProvided = 0; 
    break; 
    } 
    } 

    /*Calculate total grade*/ 
    if(dataProvided == 1) 
    { 
    average = findAverage(categories, weights, scores); 

    grade = (average/totalWeight) * 100; 
    fprintf(stdout, "Your cumulative average is %.2lf percent\n\n", grade); 

    return 1; 
    } 


}/*End of parent while loop*/ 
}/*End of main*/ 

預先感謝您

+1

在你的例子中,這些特定的三個權重和分數是什麼,'categories'和'totalWeight'的值是多少? –

+0

在這裏顯示整個來源。許多變量未知。 –

+1

好像你在計算'totalWeight'錯誤,因爲findAverage工作得很好http://ideone.com/KMDlo1 – Ryzhehvost

回答

0

明白了,當我打電話的findWeightsAndScores功能findScores功能我傳遞&得分[I]時,我應該已經通過它的分數。謝謝你們

0

你的問題是在別處因爲它像魅力一樣工作對我來說:

#include <stdio.h> 

double findAverage(int categories, double weights[], double scores[]) 
{ 
    double average = 0; 
    int i; 
    for(i = 0; i <= categories - 1; i++) { 
     average += (weights[i] * scores[i]); 
     fprintf(stdout, "%lf ", average); 
    } 

    return average; 
} 

int main(void) { 
    // your code goes here 
    double average; 
    double grade; 
    int categories = 3; 
    double totalWeight = .3 + .3 + .4; 
    double weights[] = {.3, .3, .4}; 
    double scores[] = {.4, .4, .4}; 

    average = findAverage(categories, weights, scores); 

    grade = (average/totalWeight) * 100; 
    fprintf(stdout, "\nYour cumulative average is %.2lf percent\n\n", grade); 

    return 0; 
}