Sorting Float Arrays in C++

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.

Upvotes: 1

Views: 1008

Answers (2)

Shadow2531
Shadow2531

Reputation: 12170

For sorting, assuming you don't have to implement the sorting yourself, you can use sort() like this:

#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<float> grades{5.00, 4.00, 3.00, 2.75, 2.50, 2.25, 2.00, 1.75, 1.50, 1.25, 1.00};
    sort(grades.begin(), grades.end());
    copy(grades.begin(), grades.end(), ostream_iterator<float>(cout, " "));
    cout << "\n";
}

However, in https://stackoverflow.com/a/55779158/1697, it shows you how you don't have to sort the whole array (with sort() for example) to find the median. You can use nth_element() to put the median in the middle. And, for cases where there are an even amount of grades, you can handle that with the addition of max_element(). Here is an example based off that linked answer:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cassert>
using namespace std;

int main() {
    vector<float> grades{5.00, 4.00, 3.00, 2.75, 2.50, 2.25, 2.00, 1.75, 1.50, 1.25, 1.00};
    assert(!grades.empty());
    const auto mid_iter = grades.begin() + grades.size() / 2;
    nth_element(grades.begin(), mid_iter, grades.end());
    cout << "Median: ";
    if (grades.size() % 2 == 0) {
        cout << (*max_element(grades.begin(), mid_iter) + *mid_iter) / 2;
    } else {
        cout << *mid_iter;
    }
    cout << "\n";
}

Upvotes: 1

Miraz
Miraz

Reputation: 343

In function median(), you are using temp (which is datatype int) to temporarily store a value (which is datatype float). when temp is storing a float value, it is automatically converting the float to int. then it is storing that int value to grade[] (which is float), thus converting all the float values of grade[] into int.

int temp --> float temp.

#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: "<<median(grades, size)<<endl<<"Sorted numbers: ";
  for (int i = 0; i < size; i++) {
    cout << grades[i] << " ";
  }
  cout << endl;
}

float median(float grades[], int dataPoints) {
  float 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;
}

Upvotes: 1

Related Questions