Reputation: 27
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?
Upvotes: 0
Views: 67
Reputation: 13004
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);
}
Upvotes: 1
Reputation: 1970
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;
}
Upvotes: 0
Reputation: 11
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
Upvotes: 0