如何让我的程序计算平均值下降最低分?

时间:2012-10-31 02:53:45

标签: c++ average

以下是我的代码:它运行良好但是如何使用户输入的所有分数平均但在计算之前降低最低分数?我已经插入了一些代码,但我认为它错了,因为我无法正常工作。是否有更简单,更简单的方法来编写代码?还是最好的写作方式?谢谢。

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

4 个答案:

答案 0 :(得分: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.)

答案 1 :(得分:1)

获得所有分数后,迭代它们以找到最低分数。一个简单的迭代将正常工作:

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

现在lowest将保持最低分。你可以从你的总数中减去它除以numTest-1,你应该全部设定。

答案 2 :(得分: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);

答案 3 :(得分: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;