我正在尝试编写一个程序来查找加权平均值(等级等)。 通过用户输入,我创建了两个相同大小的数组,一个为每个评分类别提供权重,另一个在相应类别中保存用户评分。例如,如果学生在类别1中得分为92%,其总值为总成绩的30%,则权重[1] = .3和得分[1] = .92。这样,每个类别的平均值可以通过(weights [i] * scores [i])找到,其中i是在for循环中初始化的实例变量,以完全遍历每个数组。这里的问题在于找到累积平均值 - 我的程序被编写为复合它处理的每个平均值的值,例如我尝试过像average = average +(weights [i] * scores [i])这样的代码来获得最终累积平均。但是通过打印语句,我发现平均值设置为最后的(权重[i] *得分[i])操作,忽略先前的平均值(或将其取为0)。例如,如果权重[] = {。3,.3,.4}和得分[] = {。4,.4,.4}。累积平均值应为.4,但我的程序告诉我.32,这是权重的最后一个值乘以最后一个得分值。
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*/
提前谢谢
答案 0 :(得分: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;
}
答案 1 :(得分:0)
知道了,当我在findWeightsAndScores函数中调用findScores函数时,我正在传递它&amp;得分[i]当我应该通过它得分时。谢谢你们