File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ int main(){
52
52
conditions, they are automatically converted to bool. */
53
53
++count;
54
54
sum += x;
55
+
55
56
}
56
57
57
58
/* write the result.
Original file line number Diff line number Diff line change
1
+ #include < algorithm>
2
+ #include < iomanip>
3
+ #include < ios>
4
+ #include < iostream>
5
+ #include < string>
6
+ #include < vector>
7
+
8
+ using std::cin; using std::sort;
9
+ using std::cout; using std::streamsize;
10
+ using std::endl; using std::string;
11
+ using std::setprecision; using std::vector;
12
+
13
+ int main (){
14
+ cout << " Please enter your first name: " ;
15
+ string name;
16
+ cin >> name;
17
+ cout << " Hello, " << name << " !" << endl;
18
+
19
+ cout << " Please enter your midterm and final grades: " ;
20
+ double midterm, final ;
21
+ cin >> midterm >> final ;
22
+
23
+ cout << " Enter all your homework grades, "
24
+ " followed by end-of-file: " ;
25
+
26
+ vector<double > homework;
27
+ double x;
28
+ // invariant: homework contains all the homework grades read so far
29
+ while (cin >> x)
30
+ homework.push_back (x);
31
+
32
+ typedef vector<double >::size_type vec_sz;
33
+ vec_sz size = homework.size ();
34
+ if (size == 0 ){
35
+ cout << endl << " You must enter your grades. "
36
+ " Please try again." << endl;
37
+
38
+ return 1 ;
39
+ }
40
+
41
+ sort (homework.begin (), homework.end ());
42
+
43
+ vec_sz mid = size/2 ;
44
+ double median;
45
+ median = size % 2 == 0 ? (homework[mid] + homework[mid - 1 ]) / 2 : homework[mid];
46
+
47
+ streamsize prec = cout.precision ();
48
+ cout << " Your final grade is " << setprecision (3 )
49
+ << 0.2 * midterm + 0.4 * final + 0.4 * median
50
+ << setprecision (prec) << endl;
51
+
52
+ return 0 ;
53
+ }
You can’t perform that action at this time.
0 commit comments