我正在编写一个程序,用于程序将成绩存储在数组中,具有输入成绩并将其存储在数组中的函数,并返回成绩数,处理最多20个等级,具有一个功能对等级数组进行排序,并有一个单独的函数,它接受排序的数组并返回中位数。我已经编写了代码,但它没有对数组进行排序。不确定我做错了什么。任何帮助将不胜感激。
#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;
}
答案 0 :(得分:2)
在main
void bubbleSort(double grades[], int numgrades);
是bubbleSort
函数的前向声明,而不是对它的调用。
bubbleSort(grades, numgrades);
将调用该函数。