how do you find the median in c++

from smallest to largest this is what I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 if(grades[0] < grades[1] && grades[0] < grades[2] && grades[0] < grades[3] && grades[0] < grades[4])
    {
       cout<<grades[0]<<" "<<grades[1]<<" "<<grades[2]<<" "<<grades[3]<<" "<<grades[4]<<endl;
       med_grade = grades[2];
    }
        else if(grades[1] < grades[0] && grades[1] < grades[2] && grades[1] < grades[3] && grades[1] < grades[4])
        {
            cout<<grades[1]<<" "<<grades[0]<<" "<<grades[2]<<" "<<grades[3]<<" "<<grades[4]<<endl;
            med_grade = grades[2];
        }
        else if(grades[2] < grades[1] && grades[2] < grades[0] && grades[2] < grades[3]&& grades[2] < grades[4])
        {
            cout<<grades[2]<<" "<<grades[1]<<" "<<grades[0]<<" "<<grades[3]<<" "<<grades[4]<<endl;
            med_grade = grades[0];
        }
        else if(grades[3] < grades[2] && grades[3] < grades[0] && grades[3] < grades[1] && grades[3] < grades[4])
        {
            cout<<grades[3]<<" "<<grades[2]<<" "<<grades[0]<<" "<<grades[1]<<" "<<grades[4]<<endl;
            med_grade = grades[0];
        }
        else if(grades[4] < grades[3] && grades[4] < grades[0] && grades[4] < grades[1] && grades[4] < grades[2])
        {
            cout<<grades[4]<<" "<<grades[3]<<" "<<grades[0]<<" "<<grades[1]<<" "<<grades[2]<<endl;
            med_grade = grades[0];
        }

I know that there better ways to find the median. is there a sort function in C++. another idea i had was to use the sort function, and what ever number was in [2] would be the median. thanks in advance for any help.

Last edited on
Yes, there's a general-purpose sort:
https://www.cplusplus.com/reference/algorithm/sort/

Strictly, you only need to partial-sort to get the median:
https://www.cplusplus.com/reference/algorithm/partial_sort/
though I think most people would continue to sort the rest.


If grades has size N then the median is, for a 0-indexed and sorted array:
median = 0.5 * ( grades[N/2] + grades[N/2-1] ); if N is even,
or
median = grades[(N-1)/2]; if N is odd.


Actually, integer division allows a single formula to be used in both cases:
median = 0.5 * ( grades[(N-1)/2] + grades[N/2] );
though, in the case of N being odd, you would end up doubling something then halving it again.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//----------------------------------------------------------------------

template <typename T> ostream & operator << ( ostream &out, const vector<T> &V )
{
   for ( T e : V ) out << e << ' ';
   return out;
}

//----------------------------------------------------------------------

double median( const vector<double> &V )
{
   int N = V.size();
   return 0.5 * ( V[(N-1)/2] + V[N/2] );
}

//----------------------------------------------------------------------

int main()
{
   vector<double> A = { 10, 7, 3, 4, 4, 6 };
   cout << "Original vector: " << A << '\n';

   sort( A.begin(), A.end() );
   cout << "Sorted vector: " << A << '\n';

   cout << "Median: " << median( A ) << '\n';
}


Original vector: 10 7 3 4 4 6 
Sorted vector: 3 4 4 6 7 10 
Median: 5


Better (but more pedantic) code would scream if you sent an empty vector to the median() routine. This one just assumes that you know what you are doing; life's too short.

Last edited on
Topic archived. No new replies allowed.