2012-03-21 23 views
0

我必須找到給定的最低輸入,然後平均減去最低分。我遇到了一些麻煩,我的averageScore函數發現陣列中得分最低。我得到非常奇怪的數字作爲我的輸出。任何建議如何調整這將不勝感激。提前致謝。從陣列中刪除最低的輸入?

#include <iostream> 
#include <cstdlib> 
using namespace std; 

//function prototypes 
double* allocate(int&); 
double averageScore(int&); 

int main() 
{ 
    double* testArray; 
     int numOfScores; 
    double average; 

    testArray = allocate(numOfScores); 
    average = averageScore(numOfScores); 

    //delete memory created 
    delete[] testArray; 

    return 0; 
} 

//function to collect user info, dynamically allocate 
double* allocate(int &numOfScores) 
{ 
    double* testArray; 

    //prompt user for scores 
    cout << "How many test scores would\n"; 
    cout << "you like to process: "; 

    //user input validation 
    if(!(cin >> numOfScores)) 
    { 
     cout << "Invalid input!\n"; 
     cout << "Program termination, please\n"; 
     cout << "restart the program." << endl; 
     exit(0); 
    } 
    else if(numOfScores < 0) 
    { 
     cout << "Invalid input!\n"; 
     cout << "Program termination, please\n"; 
     cout << "restart the program." << endl; 
     exit(0); 
    } 

    //dynammically allocate an arrray to hold the scores 
    testArray = new double[numOfScores]; 

    //get the scores from user 
    for (int count = 0; count < numOfScores; count++) 
    { 
     cout << "Enter Score: "; 

     //user input validation 
     if(!(cin >> testArray[count])) 
     { 
      cout << "Invalid input!\n"; 
      cout << "Program termination, please\n"; 
      cout << "restart the program." << endl; 
      exit(0); 
     } 
     else if(testArray[count] < 0.0) 
     { 
      cout << "Invalid input!\n"; 
      cout << "Program termination, please\n"; 
      cout << "restart the program." << endl; 
      exit(0); 
     } 


    } 

    return testArray; 
} 

//function to calculate the average score 
double averageScore(int &numOfScores) 
{ 
    double* testArray; 

    double total, 
      average, 
      scores[0], 
      lowest; 

    lowest = scores[0]; 

    //calculate total scores entered 
    for(int count = 0; count < numOfScores; count++) 
    { 
     total += testArray[count]; 

     //find lowest score entered 
     for(int count = 1; count < numOfScores; count++) 
     { 
      if (testArray[numOfScores] < lowest) 
       lowest = scores[numOfScores]; 
     } 
    } 

    //average the total amount of scores drop the lowest 
    average = (total - lowest)/numOfScores; 

    cout << "The average test score is: " << average << endl; 
    cout << "Lowest is: " << lowest << endl; 

    return average; 
} 
+0

兩條評論:如果可以的話,使用向量而不是動態分配的數組,如果不打算修改它,只需將值傳遞給'int&numOfScores'即可。 – Bwmat 2012-03-21 18:38:15

+0

你爲什麼要'double averageScore(int&numOfScores)'而不是'double averageScore(int numOfScores)'? – twain249 2012-03-21 18:38:21

+0

您不需要內部for循環來查找最低值。只需在外循環中將每個'testArray [count]'值與'lowest'進行比較。 – 2012-03-21 18:39:18

回答

1

有很多概率的與您的averageScore函數一起使用,但現在我將介紹最基本的函數。

首先,你應該傳遞一些數據。現在你正在使用testArray我什至不知道它在哪裏分配。我很驚訝你在運行時沒有出現分段錯誤。

但它也沒有初始化。在C++中,當你聲明一個指針時,它指向的變量就有一個值。它有一個垃圾值,如果你用這個垃圾值執行算術運算,那麼你的輸出也是垃圾。

你必須讓你的averageScore函數可用的分數列表,最好是將它們作爲參數傳入。

的平均函數的開頭如下所示:

double averageScore(int &numOfScores) 
{ 
    double* testArray; 
    ... 

相反,它應該是這樣的

double averageScore(double*testArray, int numOfScores) 
{ 
    ... 

當您使用&numOfScores而不是numOfScores,這意味着,如果你改變numOfScores在你的averageScore函數中,它的功能也會改變,你不應該那樣做。

現在,在double* testArray;行中,您聲明瞭一個全新的指針,名爲「testArray」,並且其中沒有任何有意義的數據,儘管它可能充滿垃圾。可能還有其他雙指針變量,在您的代碼中名爲「testArray」,但它們都不在您的averageScore函數的範圍內。如果您在方法調用中通過testArray,則可以使用它。例如:double someNumber = testArray[i]

請記住,您的數組也被引用傳遞。如果你寧願按值傳遞它,你可以嘗試

`double averageScore(double testArray[], int numOfScores)` 

但不要引用我一個

一旦你這樣做,你的代碼將仍然有一些問題,但輸出應該足夠有意義,以至於你希望能夠自己解決這些問題。

+0

謝謝。我以爲我將'scoreScore'作爲參數傳遞給'''numOfScores'參數 – Gmenfan83 2012-03-21 18:57:36

+0

@ Gmenfan83'&numOfScores'只是一個單一整數的參考,它似乎是分數的計數。只讓你的功能可用就像問一個人「我有5個號碼,你能找到他們的平均值嗎?」 我會編輯我的答案,以幫助你沿 – 2012-03-21 19:02:24

+0

比你@Sam我非常花時間向我解釋這一點。非常感謝。爲了修改需要的東西,我會將其浸潤。 – Gmenfan83 2012-03-21 19:28:13

2

我想你想改變這一行:

if(testArray[numOfScores] < lowest) 

這樣:

if(testArray[count] < lowest) 

此外,作爲@jzworkman指出的,平均的分母應該是(numScores - 1)因爲你正在消除分子的最低分數。 (如果適用,您可能希望測試只有一個分數的邊緣情況,一旦您消除最低分數,則不會得到平均值。)

2

幾個問題。你不應該嵌套這兩個for循環(而只是使用if語句檢查值是否低於最低值)。

由於這是家庭作業,我會給你的步驟,然後你可以通過修復你的代碼

  1. 環路和計算總,同時
  2. 找到最低分數計算平均爲(總最低)/(numScores -1)
  3. 返回平均
+0

可以解釋爲什麼我會這樣做(總數最低)/(numScores -1)而不是(總數 - 最低)/ numOfScores?謝謝。我只是困惑,想學習。我知道這是家庭作業,但我也想成爲一名程序員,所以這很重要。 – Gmenfan83 2012-03-21 19:58:40

+1

因爲你正在從你的總數中取出一個值。想象一下,我有以下輸入:'1,3,5,'如果我取出最低的(1),那麼我的平均值就是'(3 + 5)/ 2',因爲我有兩個我正在平均的值。你不希望它是'(3 + 5)/ 3',因爲在刪除最低值後,你只剩下2個值,而不是3 – jzworkman 2012-03-21 20:02:41

+0

我看到了,謝謝你的解釋! – Gmenfan83 2012-03-21 20:04:36

3
std::vector<double> scores = {1.2,6.5,3.0,8.3,4.8,6,7.7}; 

// drop lowest score 
scores.erase(min_element(begin(scores),end(scores))); 

double average = accumulate(begin(scores),end(scores),0.0)/scores.size(); 
+1

雖然這可能是正確的,但我的猜測是,這超出了他正在採用的當前課程的範圍(注意作業標籤),並不是採取這種方法。 – jzworkman 2012-03-21 18:50:27

+0

您應該添加命名空間說明符,但是像這樣的答案,更少更多!並贊成std lib – 111111 2012-03-21 19:14:09