How to find the median?

Hi, i have the mean and i also sorted the program but i need to know the function for median in c++? just point me in the right direction

here is my code

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;

const int N=10;

void input(double &s, int arynum[]){
    s=0;
    for (int i = 0; i<N; i++){
    cout<<"grade"<<i+1<<" ";
    cin>>arynum[i];
    s= s+arynum[i];
    }


}


void swap(int &a, int &b){
    int temp;
    temp=a;
    a=b;
    b=temp;
}


void sort(int arynum[]){

    for (int a=0; a>N; a++){

        for (int b=0; b<N; b++){

        if (arynum [a]>arynum[b]){
            swap (arynum[a], arynum[b]);

}

}

}


}

double calc (double sum, int arynum[]){
    double mean=sum/N;

}




void output(double sum, int arynum[]){

cout<<"your mean is: "<< sum/N <<endl;

for (int i=0; i < N; i++)
   {
       cout << arynum[i] << " " ;
   }


}



int main(){

    int numbers[N];
    double mean;
    double median;
    double n;
    double s;
    input(s, numbers);
    sort(numbers);
    output(s, numbers);




}

Last edited on
If you've sorted your values, the median is either the middle one (if there is an odd number of values) or the mean of the middle two (if you have an even number of values)

For more info, see Median
http://en.wikipedia.org/wiki/Median

Etc.

Andy
Last edited on
Topic archived. No new replies allowed.