So, my goal is to sort the floating values in acsending order. It sorts ints just fine, but not floating values. How do you fix this? Also, there's a random number at the start of the output.
#include <iostream>
using namespace std;
float median(float grades[], int dataPoints);
int main() {
float grades[11] = {5.00, 4.00, 3.00, 2.75, 2.50, 2.25,
2.00, 1.75, 1.50, 1.25, 1.00};
int size = 11;
for (int i = 0; i < size; i++) {
cout << grades[i] << " ";
}
cout << endl << endl;
cout << median(grades, size);
for (int i = 0; i < size; i++) {
cout << grades[i] << " ";
}
cout << endl;
}
float median(float grades[], int dataPoints) {
int temp = 0;
int i, j = 0;
for (i = 0; i < dataPoints; i++) {
for (j = 0; j < dataPoints - i - 1; j++) {
if (grades[j] > grades[j + 1]) {
temp = grades[j];
grades[j] = grades[j + 1];
grades[j + 1] = temp;
}
}
}
if (dataPoints % 2 != 0)
return (float)grades[dataPoints / 2];
else
return (float)(grades[(dataPoints / 2) - 1] + grades[dataPoints / 2]) / 2.0;
}
Output:
5 4 3 2.75 2.50 2.25 2 1.75 1.50 1.25 1
21 1 1 1 2 2 2 2 3 4 5
This is for my project and I have no idea how to fix this. Any fix would be greatly appreciated.