1

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.

3
  • 1
    I'm not going to debug your code ... but if you're successfully able to sort ints, then you SHOULD be able to sort floats exactly the same way. SUGGESTION: consider using a std::vector (instead of arrays), and using std::sort (instead of what looks like a home-rolled bubble sort). Here's an example: cplusplus.com/reference/algorithm/sort Commented May 28, 2021 at 4:53
  • 6
    When you swap grades while sorting, what is the type of value being swapped, and what is the type of the temporary you use during the swap? Commented May 28, 2021 at 4:54
  • 3
    And the random number in the output is due to there being no space between cout << median() and the first number in the sorted output. Commented May 28, 2021 at 5:00

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.