简体   繁体   中英

Problem with Output in My Median Calculator

bool isEven(int num){
    if(num % 2 == 0){
        return true;
    }
    else {
        return false;
    }
}


int main() {
    int footballPlayer[] = {1,2,3,4};
    int lengthOfArray = sizeof(footballPlayer) / sizeof(footballPlayer[0]);
    int oddMedianFormula = lengthOfArray/2;
    int evenMedianFormula = lengthOfArray/2;
    if(isEven(lengthOfArray) == false) {
         cout << "The median is " << footballPlayer[oddMedianFormula] << endl;
    } else {
        if(isEven(footballPlayer[evenMedianFormula]) == true && isEven(footballPlayer[evenMedianFormula - 1] == true)) {
            cout << "The median is " << (footballPlayer[evenMedianFormula - 1] + footballPlayer[evenMedianFormula]) / 2 << endl;
        }
        else if(isEven(footballPlayer[evenMedianFormula]) == false && isEven(footballPlayer[evenMedianFormula - 1] == false)) {
            cout << "The median is " << (footballPlayer[evenMedianFormula - 1] + footballPlayer[evenMedianFormula]) / 2 << endl;
        }
        else {
            cout << "The median is " << ((footballPlayer[evenMedianFormula - 1] + footballPlayer[evenMedianFormula]) / 2) + 0.5 << endl;
        }
    }

    return 0;
}

output = 2;

Why is my output an integer when one number is odd when I have clearly have added a half (0.5) to the output?

When you divide an integer by another integer, you simply get the floor of the mathematical value (as an integer). Say for example: 1 / 2 = (int) floor(0.5) = 0 . The answers (they are correct) here have suggested (explicit) type casting your integer values to floating point data types. But I would like to add up something.

You simply can put a point after 2 to make the compiler know you are performing division of floating point numbers. The numerator will be implicitly type converted into double in this case. Don't get confused --> 2. is same as 2.0 . Here is your modified code:

#include <iostream>
using namespace std;

bool isEven(int num) {
  if (num % 2 == 0) {
    return true;
  } else {
    return false;
  }
}

int main() {
  int footballPlayer[] = {1, 2, 3, 4};
  int lengthOfArray = sizeof(footballPlayer) / sizeof(footballPlayer[0]);
  int oddMedianFormula = lengthOfArray / 2;
  int evenMedianFormula = lengthOfArray / 2;
  if (isEven(lengthOfArray) == false) {
    cout << "The median is " << footballPlayer[oddMedianFormula] << endl;
  } else {
    if (isEven(footballPlayer[evenMedianFormula]) == true &&
        isEven(footballPlayer[evenMedianFormula - 1] == true)) {
      cout << "The median is "
           << (footballPlayer[evenMedianFormula - 1] +
               footballPlayer[evenMedianFormula]) /
                  2.
           << endl;
    } else if (isEven(footballPlayer[evenMedianFormula]) == false &&
               isEven(footballPlayer[evenMedianFormula - 1] == false)) {
      cout << "The median is "
           << (footballPlayer[evenMedianFormula - 1] +
               footballPlayer[evenMedianFormula]) /
                  2.
           << endl;
    } else {
      cout << "The median is "
           << ((footballPlayer[evenMedianFormula - 1] +
                footballPlayer[evenMedianFormula]) /
               2.) +
                  0.5
           << endl;
    }
  }

  return 0;
}

I don't know why, but your code clearly looks overly complicated to me. I actually doubt if even your logic is correct. If you just want to find the median, then I think this much code will be sufficient:

#include <iostream>
#include <vector>

double findMedianSorted(const std::vector<int> &v) {
  auto n = v.size();
  return n % 2 ? v[n / 2] : (v[(n - 1) / 2] + v[n / 2]) / 2.;
}

int main() {
  std::vector<int> footballPlayer = {1, 2, 3, 4};
  std::cout << "Median is: " << findMedianSorted(footballPlayer);
}

Because you work with Integers. You have to cast it as Double or Float.

cout << "The median is " << (float)(footballPlayer[evenMedianFormula - 1] + footballPlayer[evenMedianFormula]) / 2 << endl;

Something like this should fix the problem

That's because you're doing integer division. division of integer types always returns an integer even if the result is in the float. cast any of your variable into float and it should work. What is the behavior of integer division?

Did some changes to your code -

bool isEven(int num)
{
    return !(num % 2);
}

int main()
{
    int footballPlayer[] = {1, 2, 3, 4};
    int lengthOfArray = sizeof(footballPlayer) / sizeof(footballPlayer[0]);

    int oddMedianFormula = lengthOfArray / 2;
    int evenMedianFormula = lengthOfArray / 2;

    if (isEven(lengthOfArray))
    {
        if (isEven(footballPlayer[evenMedianFormula]) && isEven(footballPlayer[evenMedianFormula - 1]))
        {
            cout << "The median is " << (float)(footballPlayer[evenMedianFormula - 1] + footballPlayer[evenMedianFormula]) / 2 << endl;
        }
        else if (!isEven(footballPlayer[evenMedianFormula]) && !isEven(footballPlayer[evenMedianFormula - 1]))
        {
            cout << "The median is " << (float)(footballPlayer[evenMedianFormula - 1] + footballPlayer[evenMedianFormula]) / 2 << endl;
        }
        else
        {
            cout << "The median is " << (float)((footballPlayer[evenMedianFormula - 1] + footballPlayer[evenMedianFormula]) / 2) + 0.5 << endl;
        }
    }
    else
    {
        cout << "The median is " << footballPlayer[oddMedianFormula] << endl;
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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