2012-10-31 78 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