2012-10-31 45 views
1

代码运行良好,除非它不降低最低分并计算它。程序不会降低最低分,并计算没有它的平均分

输出例:

多少测试成绩你想进入?
进入测试得分期望:
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; 
} 
+1

'新的双[numTest]' - 好恶。试试'std :: vector '。 –

+0

逐步查找找到最低分数的代码。 –

+0

'std :: min_element' – chris

回答

4

你在这个码连续3个显著错误:

首先,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 
+0

OMG YOU ROCK !!!!有用!!! Wooo hooo !!!我很激动!!谢谢sooooo多! – user1787078

+0

@ user1787078很高兴提供帮助。你昨晚差点吃过东西。您只需将最低的减法放在错误的地方,并将循环中的最低值放入初始值。很高兴现在可以工作。 – WhozCraig

+0

在这个新的和用尽在屏幕上主演令人沮丧。我无法感谢你的巨大帮助。我真的学到了很多。我的老师真的不存在,所以很高兴知道我能来某个地方并真正学到一些东西。万圣节快乐! – user1787078

0

的代码从total减去lowest每次通过循环。将该线移到循环外部,效果会更好。

编辑:和,作为@WhozCraig指出,lowest没有得到正确的初始值。

0

在这一行:

int lowest = score[count]; 

你怎么想count是什么?

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 
} 
+0

我错过了第一个分数[count],但我只是改变了这一点,现在我得到了29.00。 – user1787078