C ++获得最高的测试分数并使用指针

时间:2016-11-01 00:39:39

标签: c++

创建了一个程序来提示用户输入测试分数并找到平均值,最高值并对测试分数进行排序。我遇到了问题我似乎无法从我的功能中获得最高的测试分数,当我创建分类测试分数功能时程序崩溃。有人可以查看我的功能并查看我的问题所在,我似乎可以得到平均测试分数。

double getAverage(double*, int);
double gethighest(double*, int);
double getlowest(double*, int);
void arrayAscending(double*, int);

int main()
{
    // Variable declarations
    double *testScores; // Dynamically allocate an array of test scores
    double average, highest, lowest;     
    int numTests;       // Number of test scores
    int count;          // Counter variable

                        // Get the number of tests
    cout << "How many tests do you wish to process? ";
    cin >> numTests;

    // Verify input is not a negative number
    while (numTests <= 0)
    {
        // Get input again.
        cout << "Please enter a positive number:  ";
        cin >> numTests;
    }

    // Dynamically allocate an array large enough to hold the test scores
    testScores = new double[numTests];

    // Get the specified number of test scores
    cout << "Enter the test scores below.\n";
    for (count = 0; count < numTests; count++)
    {
        cout << "Test " << (count + 1) << ": ";
        cin >> *(testScores + count);

        // Verify input is not a negative number
        while (*(testScores + count) < 0)
        {
            // Get input again.
            cout << "Please enter a valid test score.\n";
            cin >> *(testScores + count);
        }
    }

    // Calculate the average test score
    average = getAverage(testScores, numTests);
    highest = gethighest(testScores, numTests);
    lowest = getlowest(testScores, numTests);
    // Display the results.

    arrayAscending(testScores, numTests);

    cout << endl;
    cout << "The average of those scores is:  " << average << endl;

    cout << "The higest of those scores is: " << highest << endl;
    cout << "The lowest of those scores is: " << lowest << endl;

    // Free dynamically allocated memory
    delete[] testScores;
    testScores = 0;     // Make testScores point to null
    return 0;
}

//function getAverage - calculates the average of the test scores
double getAverage(double* scores, int num)
{
    double avg;
    double total = 0.0;
    for (int count = 0; count < num; count++)
    {
        total += scores[count];
    }
    avg = total / num;
    return avg;
}

double gethighest(double* scores, int num)
{
    double highnum = 0.0;

    for (int i = 0; i < num; i++)
    {
        if (scores[i] > highnum)
            highnum = scores[i];

    }
    return highnum;
}

double getlowest(double* scores, int num)
{
    double lowestnum = 100;

    for (int i = 0; i < num; i++)
    {
        if (scores[i] < lowestnum)
            lowestnum = scores[i];
    }
    return lowestnum;
}

void arrayAscending(double *array, int size)
{
    int startScan, minIndex;
    double minElem;

    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minElem = array[startScan];
        for (int index = startScan + 1; index < size; index++)

        {
            if ((array[index]) < minElem)
            {
                minElem = array[index];
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = minElem;
    }
}

1 个答案:

答案 0 :(得分:3)

void arrayAscending(double *[], int);

应该是

void arrayAscending(double *, int);

你正在排序一个双精度数组,而不是一个双精度指针数组。

另外,

double *minElem;
稍后使用

而不为其分配任何内存。再说一遍,你可能只需要

double minElem;

而不是指针。

如果您不需要使用指针,请使用标准库中的std::vector和算法,例如std::sort