Finding the Mean, Median, and Range of an Array

I am having trouble determining how to calculate the median for my array. Additionally, I am not too confident in my solution to find the range of the array. The prompt for my problem is as follows, with my current code below:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A teacher has a small class of 10 students taking an exam. The professor would like to enter the scores from the exam and then calculate the mean, median, and range of scores to display them.

Your job is to write a program that collects the 10 scores using a for loop. Then write a second for loop that calculates the mean, median, and range of the scores.

Finally display the mean, median, and range.

Hint: Use #include <algorithm> to sort() your vector before finding the median.

Example input/output:
Enter the students' 10 scores: 67.0 2.2 73.1 32.2 53.6 41.1 37.9 59.2 69.3 81.6
Mean: 51.72
Median: 56.4
Range: 79.4
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	const double NUM_SCORES = 10;
	vector<double> studentScores(NUM_SCORES);
	int i;
	int sum = 0;
	int range;

	cout << "Enter the students' 10 scores: ";

	// determining the sum of the input numbers in the array
	for (i = 0; i < studentScores.size(); i++) {
		cin >> studentScores.at(i);
		sum += studentScores.at(i);
	}

	// determining the median
	double median;
	sort(studentScores.begin(), studentScores.end());
	if ((studentScores.size() % 2) == 0) {
		median = studentScores[studentScores.size() / 2] + studentScores[(studentScores.size() / 2) - 1] / 2;
	}
	else {
		median = studentScores[studentScores.size() / 2];
	}


	cout << "Mean: " << sum / 10 << endl << "Median: " << median << endl << "Range: " << (studentScores.at(0) - studentScores.at(9)) * -1 << endl;

	system("pause");
	return 0;
}
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

int main() {

    // http://en.cppreference.com/w/cpp/types/size_t
    const std::size_t NUM_SCORES = 10;
    std::vector<double> studentScores(NUM_SCORES);

    std::cout << "Enter the students' " << studentScores.size() << " scores: ";
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( double& score : studentScores ) std::cin >> score ;
    // note: you may want to add input validation (eg. check if the score is negative)

    // mean, median and range are defined only if the sequence is not empty
    // (in this particular program, this check is redundant; NUM_SCORES is greater than zero)
    if( !studentScores.empty() ) {
     
        // sort the scores
        // (we need to sort it anyway, and doing it first may minimise rounding errors in the sum.
        //  we assume that there are no NaN values among the scores.
        //  these two niceties may be safely ignored for now.)
        std::sort( studentScores.begin(), studentScores.end() );

        // determining the sum of the input numbers in the array
        // http://en.cppreference.com/w/cpp/algorithm/accumulate (#include <numeric>)
        const double sum = std::accumulate( studentScores.begin(), studentScores.end(), 0.0 );

        // determine and print out the mean
        std::cout << "mean: " << sum / studentScores.size() << '\n' ;

        // determine the median and print it out
        const auto n = studentScores.size() ;
        // If there is an even number of observations, the median is the mean of the two middle values
        // if n is odd, the middle value is at n/2 (which is also equal to (n-1)/2)
        // if n is even, the two middle values are at (n-1)/2 and n/2
        const double median = ( studentScores[n/2] + studentScores[ (n-1)/2 ] ) / 2.0 ;
        std::cout << "median: " << median << '\n' ;

        // determine the range and print it out
        // range is the difference between the largest and smallest value
        // sorted in ascending order, so largest value is at the back, smallest at the front
        const double range = studentScores.back() - studentScores.front() ;
        std::cout << "range: " << range << '\n' ;
    }
}
Last edited on
Topic archived. No new replies allowed.