简体   繁体   English

平均值未正确计算

[英]Average not Calculating Properly

I have designed a small program to calculate the average test scores for an array of students, however, for the life of me, cannot figure out why the average is not calculating properly. 我设计了一个小程序来计算一系列学生的平均考试成绩,但是,对于我一生来说,无法弄清楚为什么平均成绩计算不正确。 What is going on? 到底是怎么回事?

Output: 输出:

Enter number of students in class (max 50): 3 Enter total number of tests (max 10): 3 Enter student 1's test scores: 100 100 100 Enter student 2's test scores: 100 97 75 Enter student 3's test scores: 56 45 67 (lldb) (lldb) (lldb) 输入班级的学生人数(最多50个):3输入测试总数(最多10个):3输入学生1的测试成绩:100 100 100输入学生2的测试成绩:100 97 75输入学生3的测试成绩:56 45 67 (lldb)(lldb)(lldb)

The average score for student 1 is 33.3333. 学生1的平均分数是33.3333。 // As you see here it should be 100 since all 3 scores were 100. (lldb) //如您所见,由于所有3个分数均为100,因此应该为100。(lldb)

Code: 码:

const int MAX_STUDENTS = 50;
const int MAX_TESTS = 10;

int main()
{
    char studentName[50];
    int totalStudents = 0;
    int totalTests = 0;
    double totalScore = 0;
    double score[MAX_STUDENTS][MAX_TESTS];
    double averages[MAX_STUDENTS];

    std::cout << "Enter number of students in class (max " << MAX_STUDENTS << "): ";
    std::cin >> totalStudents;
    std::cout << "Enter total number of tests (max " << MAX_TESTS << "): ";
    std::cin >> totalTests;
    for (int student = 0; student < totalStudents; student++) {
        std::cout << "Enter student " << (student + 1) << "'s test scores: " << endl;
        for (int test = 0; test < totalTests; test++) {
            std::cin >> score[student][test];
        }
    }

    for (int student = 0; student < totalStudents; student++) {
        for (int test = 0; test < totalTests; test++) {
            totalScore = NULL;
            totalScore += score[student][test];
        }
        averages[student] = totalScore / totalTests;
        std::cout << endl;
        std::cout << "The average score for student " << student + 1 << " is " << averages[student] << "." << endl;
    }

    return 0;
}

Change 更改

    for (int test = 0; test < totalTests; test++) {
        totalScore = NULL;
        totalScore += score[student][test];
    }

to

    totalScore = 0;
    for (int test = 0; test < totalTests; test++) {
        totalScore += score[student][test];
    }

Otherwise you are resetting the totalScore prior to each addition. 否则,您将在每次添加之前重置totalScore。

You always set totalScore to 0 inside the inner loop. 您总是在内部循环中将totalScore设置为0。

   for (int student = 0; student < totalStudents; student++) {
        for (int test = 0; test < totalTests; test++) {
            totalScore = NULL;
            totalScore += score[student][test];
        }

Statement 声明

            totalScore = 0.0; // why NULL?

must be placed before the inner loop. 必须放在内部循环之前。

Also I would separate the calculation of average scores and printing the result. 我也将平均分数的计算和打印结果分开。

So I would write the following way 所以我会这样写

for ( int student = 0; student < totalStudents; student++ ) 
{
    averages[student] = 0.0;
    for ( int test = 0; test < totalTests; test++ ) 
    {
        averages[student] += score[student][test];
    }
    averages[student] /= totalTests;
}

for ( int student = 0; student < totalStudents; student++ ) 
{
    std::cout << "The average score for student " << student + 1 << " is " << averages[student] << "." << endl;
}

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

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