c++获得最高分和使用指针

C++ getting highest test score and using pointers

本文关键字:指针 最高分 c++      更新时间:2023-10-16

创建了一个程序来提示用户输入考试成绩,并找到平均分、最高分,并对考试成绩进行排序。我遇到了问题,我似乎无法从我所做的函数中获得最高的测试分数,当我创建排序测试分数函数时,程序崩溃了。谁能看看我的函数,看看我的问题在哪里,我似乎得到平均考试成绩显示。

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;
    }
}
void arrayAscending(double *[], int);
应该

void arrayAscending(double *, int);

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

,

double *minElem;

稍后使用,不会为其分配任何内存。同样,你可能只需要

double minElem;

而不是指针

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