I'm having trouble pointing into an array

Hi all,
Im trying to write this program that will give me the Average and the Median of a set of grades within an array. I have the Average part of the program down, but i'm having trouble trying to get the median of the grades. What i am on doing is if the array is odd, it will chose the middlest(?) grade as the median, and if the array is even, it will choose the two middlest(?) and average them out.

Here's the 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

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    const int STUDENTS = 10;
    int grades[STUDENTS];
    cout << "Enter " << STUDENTS << " grades. \n";
    for (int i = 0; i < STUDENTS; i++){
        cout << i << ": ";
        
        while ( !(cin >> grades[i]) ){
              cin.clear();
              cin.ignore(1000, '\n');
        }
    }
    
    sort (grades , grades + STUDENTS);
    
    int sum = 0;
    for (int i = 0; i < STUDENTS; ++i){
        sum += grades[i];
    }
    cout << "Average: " << double(sum)/STUDENTS << endl;
    cout << "Median: ";
    if (STUDENTS % 2 !== 0){ // this is as far as i've gotten with the median
                 
    system (("pause"));
}


I know that I have to actually point into the array, but have no idea how to do it!
Any suggestions?

Thanks in advance
Last edited on
STUDENTS is always 10 so the array always has an even number of elements. The two middle elements are grades[STUDENTS / 2] and grades[STUDENTS / 2 - 1]. Now it shouldn't be hard calculating the median.
Last edited on
Yep, that ended up solving it.

Thanks a bunch Peter87!
Topic archived. No new replies allowed.