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.
#include <iostream>
#include <algorithm>
usingnamespace std;
int main(){
constint 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?
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.