2012-10-31 28 views
0

以下是我的代碼:它運行良好,但是如何使用戶輸入的平均分數達到平均值,然後在計算之前降低最低分數?我插入了一些代碼,但我相信它是錯誤的,因爲我無法讓它正常工作。有沒有更簡單的方法來編寫代碼?或者寫出最好的方法?謝謝。如何讓我的程序計算平均下降的最低分數?

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

//void sortArray(double arr[], int numTest, int scoreNum); 
//void displayArray(double arr[], int numTest, int scoreNum); 

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]; 
    } 

    //sortArray(score, numTest, scoreNum); (Need to get this part to work) 
    //displayArray(score, numTest, scoreNum); (Need to get this part to work) 

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

    //Calculate the test scores average minus the lowest score. (I need help here - is this how I drop the lowest score?) 
    average = total/numTest; 

    //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; 
} 

/* void sortArray(double arr[], int numTest, int scoreNum) 
{ 
    double num = 0; 
    int posNum = 0; 
    for (int i = 0; i < numTest; i++) 
    { 
     for (int x = (i + 1); x < numTest; x++) 
     { 
      if (arr[i] > arr[x]) 
      { 
       num = score[i]; 
       posNum = scoreNum[i]; 
       arr[i] = arr[x]; 
       scoreNum[i] = scoreNum[x]; 
       arr[x] = num; 
       scoreNum[x] = posNum; 
      } 
     } 
    } 
} 

void displayArray(double arr[], int numTest, int scoreNum) 
{ 
    double average = 0.0; 
    double sum = 0.0; 
    int x; 
    for (int i = 0; i < numTest; i++) 
    { 
     for (x = 1; x < numTest; x++) 
     { 
      sum += arr[x]; 
     } 
    } 

    average = sum(numTest - 1); 

    cout << fixed << showpoint << setprecision(2) << endl; 
    cout << "The average of all test scores dropping the lowest is: " << average << endl; 
} 
*/ 
+0

什麼問題,你用你的代碼有? – Xymostech

+0

排序對於找到最低值似乎有點矯枉過正。將分數更改爲矢量然後執行此操作: double minScore = 1e6; int minScoreIndex; (count = 0; count score [count]){minScore = score [count]; minScoreIndex =計數; score.erase(score.begin()+ minScoreIndex);}} score.erase(score.begin()+ minScoreIndex); – DigitalGhost

+0

我需要讓它降低最低分,然後計算它。現在我只能用它來計算總分數而不降低最低分數。我有點不清楚如何適當地編寫函數來降低最低分數。我寫下了一些東西(我在底部註釋過),但我根本沒有那麼做。我可以使用像voidFindLowest ...然後average = total /(numTest - 最低)?或者我必須有一個排序數組,然後找到最低,然後放下它並顯示它? – user1787078

回答

1

一旦你有所有的分數,遍歷他們找到最低的一個。一個簡單的迭代將正常工作:

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

現在lowest將持有的最低得分。你可以從你的總數中減去它,除以numTest-1,你應該全部設置。

+0

這是一個退化的案例。我想這不是真正的泡沫排序然後... – GraphicsMuncher

+0

我仍然很難使用此代碼。讓我看看它是否會讓我提交一些。 – user1787078

+0

//查找最低分。 \t int lowest = score [count]; \t爲(計數= 1;計數 user1787078

3

在開始之前,您應該考慮使用std::vector而不是數組的原始指針。

// instead of: 
double *score = new double[numTest]; 

// use: 
std::vector<double> scores; 
scores.resize(numTest); 

關於你的問題:

首先,你需要的分數排序,找到最低:

// partial_sort will find the first n (here, 1) items, and leave the remainder 
std::partial_sort(&score[0], &score[1], &score[numTest]); 

接下來,獲得平均所有,但最低:

auto avg = std::accumulate(&score[1], &score[numTest], 0.0)/(numTest - 1); 

(這是跳過所有錯誤處理;例如,你應該檢查numTest大於1.)

+0

排序分數是矯枉過正從排除得分最低。平均 – kevintodisco

+0

@ktodisco:你說的沒錯,我現在在編輯它的中間 – moswald

+1

@moswald:可能我也建議'scores.begin()1'的'&得分[1]'外貌代替+。更好,而且感覺更有型聲 –

0

要有效地做到這一點(避免儘可能多的說明),您可以在總結數值的同時跟蹤最低分數,因此只需循環一次分數。

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

太棒了。這是我的想法,但不能把它放在一起。非常感謝。 – user1787078

+0

這種方法的問題是它有可能比排序數組的總和引入更多的舍入錯誤。 – Mankarse

+0

@Mankarse請詳細說明一下嗎? – kevintodisco

0
std::vector<double> scores; 
// populate scores here 
double lowest = scores[0]; 
double sum = scores[0]; 
for (int i = 1; i < scores.size(); ++i) { 
    lowest = std::min(lowest, scores[i]); 
    sum += scores[i]; 
} 
sum -= lowest; 
sum /= scores.size() - 1;