我试图编写一个程序来查找加权平均值(对于等级等)。 通过用户输入,我创建了两个相同大小的数组,其中一个承载每个评分类别的权重,另一个将用户评分保留在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*/
预先感谢您
在你的例子中,这些特定的三个权重和分数是什么,'categories'和'totalWeight'的值是多少? –
在这里显示整个来源。许多变量未知。 –
好像你在计算'totalWeight'错误,因为findAverage工作得很好http://ideone.com/KMDlo1 – Ryzhehvost