程序不会降低最低分并计算没有它的平均值

时间:2012-10-31 17:36:02

标签: c++

代码运行良好,但不会降低最低分,然后计算它。

输出示例:

您想要输入多少个考试成绩? 3
输入所需的测试分数:
分数1:58
分数2:96
分数3:78
测试得分平均值最低,为:116.00

问题: 正如您在输出示例中看到的那样,这是不正确的。它必须显示不包括最低值的平均值。你能查一下我的代码,让我知道我哪里出错了吗?我有几个人也看过它,他们在我的代码中看不到任何错误。以下是我的代码:

代码:

#include <iostream>
#include <iomanip>

using namespace std;


int main()
{
    //To dynamically allocate an array, Accumulator, to hold the average scores.
    double *score;      
    double total = 0;  
    double average; 


    //int for counter, to hold the number of test scores.
    int count; 
    int numTest; 


    // To obtain the number of test scores the user would like to enter.
    cout << "How many test scores would you like to enter? " << endl;
    cin >> numTest; 


    //Dynamically allocates an array large enough to hold the amount of test scores to enter.
    score = new double[numTest];


    //Get the test scores.
    cout << "Enter the test score desired. " << endl;
    for (count = 0; count < numTest; count++)
    {
        cout << "Score " << (count + 1) << ": ";
        cin >> score[count];
    }

    //Find lowest score.
    int lowest = score[count];
    for (count = 1; count < numTest; count++)
    {
        if (score[count] < lowest) 
            lowest = score[0];
    }


    //Calculate the total test scores.
    for (count = 0; count < numTest; count++)
    {
        total += score[count];
        total -= lowest;
    }

    //Calculate the test scores average minus the lowest score. 
    average = total / (numTest - 1);

    //Display the results
    cout << fixed << showpoint << setprecision(2);
    cout << "Test Scores Average with the lowest dropped is: " << average << endl;

    //Free dynamically allocated memory
    delete [] score;
    score = 0; // Makes score point to null.

    system("pause");
    return 0;
}

4 个答案:

答案 0 :(得分:4)

此代码中有三个重大错误:

首先,lowest的初始值:

//Find lowest score.
int lowest = score[count];  // ERROR
for (count = 1; count < numTest; count++)
{
    if (score[count] < lowest) 
        lowest = score[0];
}

错误在于:

int lowest = score[count];

需要这样:

int lowest = score[0];

接下来,在循环中

lowest = score[0]; // ERROR

应该是:

lowest = score[count];

所以你的循环应该是这样的:

//Find lowest score.
int lowest = score[0];
for (count = 1; count < numTest; count++)
{
    if (score[count] < lowest) 
        lowest = score[count];
}

最后,计算总数也是错误的。在计算所有分数的总和之后减去的最低分然后除以得分数减去一:

//Calculate the total test scores.
for (count = 0; count < numTest; count++)
{
    total += score[count];
    total -= lowest; // ERROR: should not be here. should be AFTER the loop
}

应该是:

for (count = 0; count < numTest; count++)
    total += score[count];
total -= lowest; // note: NOT in the loop

答案 1 :(得分:1)

执行此操作时:

//Find lowest score.
int lowest = score[count];

您的count变量等于numTest,因为您刚离开前一个循环。

你应该写

//Find lowest score.
int lowest = score[0]; // Error here
for (count = 1; count < numTest; count++)
{
    if (score[count] < lowest) 
        lowest = score[count]; // Error here too
}

答案 2 :(得分:0)

每次循环时,代码都会从lowest中减去total。将该行移到循环之外,它将更好地工作。

编辑:并且,正如@WhozCraig指出的那样,lowest没有获得正确的初始值。

答案 3 :(得分:0)

在这一行:

int lowest = score[count];

您认为count是什么?