calculating final grade using median

This code is correct it runs correctly I just have a question about something pertaining to the median. It says near the bottom of my code that mid is equal to size / 2. but from my understanding since size is representing homework.size()
wouldn't half of it be half of the amount of elements in the vector not half of the sum of the elements within the vectors. I hope I made that question as clear as possible, by the way the goal of this code is to compute a final grade.

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
#include <iostream>
#include <ios>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std;
int main()
{
	//ask for and read the student's name
	cout << "Please enter your name: ";
	string name;
	cin >> name;
	cout << "Hello, " + name + "!";
	
	cout << endl;
	//ask for and read the midterm and final grades
	cout << "Please enter your midterm and final grades: ";
	double midterm, final;
	cin >> midterm >> final;
	
	//ask for and read the homework grades
	cout << "Enter all of your homework grades, "
			"followed by end_of_file: ";
	
	//vector is a container that holds values
	//so this vector will hold numbers with decimals because it's type is "double"
	vector<double> homework;
	double x;
	
	//invariant: homework contains all the homework grades read so far
	while (cin >> x) 
		//push_back can only be used with vectors, it is pushing 1 data into the 
		//vector or container until it hits EOF or the data does not fit the type. because it 
		//is pushing this data into the vector, as a side effect it will increase the vectors size by 1.
		homework.push_back(x); 
		
	
	//check that the student entered some homework grades
	//typedef allows you to use the name that we define to be a synonym for the given type
	//so for example vec_sz = vector<double>::size_type
	typedef vector<double>::size_type vec_sz;
	vec_sz size = homework.size();
	if (size == 0) {
		cout << endl << "You must enter your grades. "
						"Please try again. " << endl;
			return 1000000;
	}
	//sort the grades
	sort(homework.begin(), homework.end());
	
	//compute the median homework grade
	vec_sz mid = size/2;
	double median;
	median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2
							: homework[mid];
	
	//compute and write the final grade
	streamsize prec = cout.precision();
	cout << "Your final grade is " << setprecision(3) 
		 << 0.2 * midterm + 0.4 * final + 0.4 * median 
		 << setprecision(prec) << endl;
		 
		return 0; 
	
}
	
	


Thanks so much!
http://en.wikipedia.org/wiki/Median

Not more than 10 lines of reading you'll understand it ^^


And, mid is POSITION , median is VALUE.
Last edited on
the median is after sorting the values the 2 middle values will be added and divided by 2

algorithm for this median is

sort the values,find the 2 middle values ,get its mean
@terapaht not more than 2 lines ^^
Last edited on
Topic archived. No new replies allowed.