简体   繁体   English

bubbleort无法传递数据或不起作用

[英]bubblesort not passing data or doesnt work

I'm writing a program for an assignment in which the program stores grades in array, has a function that inputs the grades and stores them in an array and returns the number of grades, handles up to 20 grades, has a function that sorts the array of grades, and has a separate function that takes the sorted array and returns the median. 我正在编写一个用于分配的程序,其中程序将成绩存储在数组中,具有输入成绩并将其存储在数组中并返回成绩数的功能,最多可处理20个成绩,并具有对成绩进行排序的功能分数数组,并具有一个单独的函数,该函数采用排序后的数组并返回中位数。 I have the code written but it is not sorting the array. 我已经编写了代码,但是没有对数组进行排序。 Not sure what I am doing wrong. 不知道我在做什么错。 Any help would be greatly appreciated. 任何帮助将不胜感激。

#include <iostream>

using namespace std;

int main();

void bubbleSort(double[], int); //Function prototypes
void swap(double &, double &);
void findMedian(double[], int, int, int, int, int);



int main()
{
    int numgrades;          // the number of grades in the array
    double grades[20];      // array of grades
    int first = 0,
        last,
        middle;
    double medianeven;      // median if # of elements are even
    double medianodd;       // median if # of elements are odd
    bool isEven(int);       // determines if the #of elements is even

    cout << "Please enter the number of grades. ";
    cin >> numgrades;

    for (int index = 0; index <= numgrades - 1; index++)
    {
        cout << "Enter test score "
            << (index + 1) << ":";
        cin >> grades[index];
    }

    void bubbleSort(double grades[], int numgrades);


    for (int index = 0; index <= numgrades - 1; index++)
    {
        cout << grades[index];
    }

    (((last) = (numgrades - 1)));
    (((middle) = (first + last) / 2));

    if (isEven(numgrades))
    {
        (medianeven = ((grades[middle] + grades[middle + 1]) / 2));
        cout << "The median grade is +" << medianeven << "." << endl;
    }

    else
    {
        ((medianodd = grades[middle]));
        cout << "The median grade is -" << (medianodd) << "." << endl;
    }

    return 0;
}


void bubbleSort(double array[], int numgrades)
{
    int minIndex;
    double minValue;

    for (int start = 0; start < (numgrades - 1); start++)
    {
        minIndex = start;
        minValue = array[start];
        for (int index = start + 1; index < numgrades; index++)
        {
            if (array[index] < minValue)
            {
                minValue = array[index],
                    minIndex = index;
            }
        }
        swap(array[minIndex], array[start]);
    }

}

void swap(double &a, double &b)
{
    double temp = a;
    a = b;
    b = temp;
}

bool isEven(int number)
{
    bool status;

    if (number % 2 == 0)
        status = true;
    else
        status = false;

    return status;
}

In main main

void bubbleSort(double grades[], int numgrades);

is a forward declaration of the bubbleSort function, not a call to it. bubbleSort函数的前向声明,而不是对其的调用。

bubbleSort(grades, numgrades);

will call the function. 将调用该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM