1

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

输出示例:

您想输入多少个考试成绩?
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

4 回答 4

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
于 2012-10-31T17:38:30.550 回答
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
}
于 2012-10-31T17:39:39.183 回答
0

lowest代码从total每次循环中减去。将那条线移到循环之外,它会更好地工作。

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

于 2012-10-31T17:38:42.157 回答
0

在这一行:

int lowest = score[count];

你觉得count是什么?

于 2012-10-31T17:39:31.100 回答