简体   繁体   English

为什么我的weighted_grade变量不起作用?

[英]Why is my weighted_grade variable not working?

I need to write a C++ program what can be used to determine grades at the end of the semester. 我需要编写一个C ++程序,可以在学期末确定成绩。 For each student, who is identified by an integer number between 1 and 60, four examination grades must be kept. 对于每个由1到60之间的整数标识的学生,必须保留四个考试成绩。 Additionally, two final grade averages must be computed. 此外,必须计算两个最终的平均成绩。 The first grade average is simply the average of all four grades. 一年级的平均值仅仅是所有四个年级的平均值。 The second grade average is computed by weighting the four grades as follows: the first grade gets a weight of 0.2, the second grade gets a weight of 0.3, the third grade a weight of 0.3, and the fourth grade a weight of 0.2; 通过按以下方式对四个等级进行加权来计算第二等级的平均值:第一等级的权重为0.2,第二等级的权重为0.3,第三等级的权重为0.3,第四等级的权重为0.2; that is computed as: 计算公式为:

0.2 * grade1 + 0.3 * grade2+ 0.3* grade3 + 0.2 * grade4 0.2 * 1级+ 0.3 * 2级+ 0.3 * 3级+ 0.2 * 4级

Using this information, you are to construct a 60 X 7 two dimensional array, in which the first column is used for the student number, the next four columns for the grades, and the last two columns for the computed final grades. 使用此信息,您将构建一个60 X 7的二维数组,其中第一列用于学生编号,接下来的四列用于成绩,最后的两列用于计算的最终成绩。 The output of the program should be a display of the data in the completed array. 程序的输出应该是完整数组中数据的显示。

Here's what I have, but my second average is off. 这就是我所拥有的,但是我的第二个平均值已经关闭。 For example, my 1st student should have both averages of 100. However Avg2 is coming out to equal 1000 and I have no idea why. 例如,我的第一个学生的平均成绩均应为100。但是Avg2的平均值为1000,我不知道为什么。 Help? 救命?

#include <iostream>
#include <math.h>
using namespace std;

int grade_Calc(int sg[][5]);

int main()
{
     int student_grades[5][5] =
{
    {1, 100, 100, 100, 100}, //inputs an array of students 1-5's grades
    {2, 100, 0, 100, 0},
    {3, 82, 94, 73, 86},
    {4, 64, 74, 84, 94},
    {5, 94, 84, 74, 64},
};

grade_Calc(student_grades);
return 0;
}


int grade_Calc(int sg[][5])
{
int sum_for_avg = 0;
double weighted_grade = 0.0;
double simple_avg = 0.0;
double sum2 = 0.0;

cout << "Stdnt" << "\t" << "Grd1" << "\t" << "Grd2" << "\t" << "Grd3" << "\t" << "Grd4" << "\t" << "Avg1" << "\t" << "Avg2" << endl;
for (int r = 0; r < 5; r++)
{
    for (int c = 0; c < 5; c++)
    {
        cout << sg[r][c] << "\t\t";

        if (c != 0)
        {
            sum_for_avg += sg[r][c];
            if ((c == 1) || (c == 4))
            {
                weighted_grade += (0.3 * sg[r][c]);
            }
            else
            {
                weighted_grade += (0.2 * sg[r][c]);
            }
        }
    }

    simple_avg = (sum_for_avg / 4.0);
    cout << "\t" << simple_avg << "\t" << weighted_grade;
    for (int k = 0; k < 5; k++)
    {
        if (k != 0)
        {
            sum2 += pow((sg[r][k] - simple_avg), 2);
        }
    }

    cout << sqrt(sum2 / 4.0);
    sum_for_avg = 0;
    weighted_grade = 0.0;
    cout << endl;
}
return 0;
}

You have to reset the value of sum_for_avg for every student, at every line of your table, that is, at the beginning of your for loop over r . 您必须在表的每一行,即在rfor循环开始时,为每个学生重置sum_for_avg的值。 As it is, it starts correctly at 0 for the first student, but then for each following student it starts from the value you already have. 实际上,对于第一个学生,它正确地从0开始,但是对于随后的每个学生,它都从您已有的值开始。

EDIT: turns out I was too hasty. 编辑:原来我太草率了。 sum_for_avg is reset after all - just not where I would do it, that is, it's reset at the end of the loop. sum_for_avg 重置-而不是我sum_for_avg地方,也就是说,它会在循环结束时重置。 So the average is right. 因此,平均值是正确的。 What is happening is that you are also printing another value, which is the standard deviation, and since there are no spaces it looks like it's 1000, whereas it is the concatenation of 100 and 0. Then again, the standard deviation is also wrong, for the reason that I gave at the beginning: you are not resetting it. 发生的情况是您还在打印另一个值,即标准偏差,由于没有空格,它看起来像是1000,而它是100和0的串联。那么,标准偏差也是错误的,出于我一开始给出的原因:您没有重置它。 So add this line: 因此,添加以下行:

sum2 = 0;

just before your loop that calculates the standard deviation. 就在计算标准偏差的循环之前。 And print at least a space between your weighted average and your standard deviation. 并在您的加权平均值和标准偏差之间至少打印一个空格。

Actually Avg2 is coming out 100. But then you run this bit of code for reasons not outlined in your question: 实际上,Avg2排在第100位。但是由于您的问题中未列出的原因,您随后运行了这段代码:

for (int k = 0; k < 5; k++)
{
    if (k != 0)
    {
        sum2 += pow((sg[r][k] - simple_avg), 2);
    }
}

cout << sqrt(sum2 / 4.0);

The cout at the end is writing a 0 right after Avg2's 100 so it looks like it's printing 1000. 末尾的cout在Avg2的100之后紧跟着写0,因此它看起来像在打印1000。

So, you print out: 因此,您可以打印出:

Stdnt   Grd1    Grd2    Grd3    Grd4    Avg1    Avg2
1       100     100     100     100     100     100

and then along comes cout << sqrt(sum2 / 4.0); 然后出现cout << sqrt(sum2 / 4.0); to print out another 0 and you get 打印出另一个0,你得到

Stdnt   Grd1    Grd2    Grd3    Grd4    Avg1    Avg2
1       100     100     100     100     100     1000

Clean that bit of code up and you'll find the bug spotted by @Fabio Turati. 清理一下代码,您将发现@Fabio Turati发现的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM