Skip to content

Commit 58cf391

Browse files
committed
Chapter03
1 parent 1e83b07 commit 58cf391

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Chapter03/ComputeStudentGrades.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ int main(){
5252
conditions, they are automatically converted to bool. */
5353
++count;
5454
sum += x;
55+
5556
}
5657

5758
/* write the result.

Chapter03/ComputeStudentGrades_v2.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

0 commit comments

Comments
 (0)