Linked Questions

Popular Questions

Problems with a median value function C++

Asked by At

I am trying to create a problem in zybooks (C++) that find the median value of a vector. Here is the function I wrote to do so:

double FindMedianScore(vector<int>& scores)
{
   sort(scores.begin(), scores.end());
   int median_score;
   if ((scores.size() % 2) == 0)
   {
      median_score = (scores.at((scores.size() / 2) - 1) + 
scores.at(scores.size() / 2)) / 2;   
   }
   else if ((scores.size() % 2) != 0)
   {
      median_score = scores.at(scores.size() / 2);
   }
   return median_score;    
}

In all the tests it passes, except for the random values test. zybooks gives the function a vector of 250 random values and wants the median. As far as I can tell there is nothing wrong with my code, but I fail the test no matter what I change. Any thoughts?

Related Questions