Skip to content

Commit f0fda0e

Browse files
author
Kevin Languasco
committed
Chapter 4
1 parent 0da9355 commit f0fda0e

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.ycm_extra_conf.py
22
.color_coded
3+
_vimrc_local.vim

3-working_with_batches_of_data/median.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ int main() {
2828
while (cin >> x) {
2929
homework.push_back(x);
3030
}
31-
31+
3232
typedef vector<double>::size_type vec_sz;
3333
vec_sz size = homework.size();
34-
34+
3535
if (size==0) {
3636
cout << endl << "You must enter your grades. "
3737
"Please try again." << endl;

4-organizing_programs_and_data/grades

45.5 KB
Binary file not shown.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <stdexcept>
5+
#include <string>
6+
#include <ios>
7+
#include <iomanip>
8+
9+
using std::vector;
10+
using std::sort;
11+
using std::domain_error;
12+
using std::istream;
13+
using std::string;
14+
using std::cin;
15+
using std::cout;
16+
using std::endl;
17+
using std::setprecision;
18+
using std::streamsize;
19+
20+
double grade(double midterm, double final, double homework) {
21+
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
22+
}
23+
24+
double median(vector<double> vec) {
25+
typedef vector<double>::size_type vec_sz;
26+
vec_sz size = vec.size();
27+
if (size==0)
28+
throw domain_error("median of an empty vector");
29+
30+
sort(vec.begin(), vec.end());
31+
32+
vec_sz mid = size/2;
33+
34+
return size % 2 == 0 ? (vec[mid] + vec[mid-1]) /2 : vec[mid];
35+
}
36+
37+
// overloading
38+
double grade(double midterm, double final, const vector<double>& hw) {
39+
if (hw.size() == 0)
40+
throw domain_error("student has done no homework");
41+
return grade(midterm, final, median(hw));
42+
}
43+
44+
istream& read_hw(istream& in, vector<double>& hw) {
45+
if (in) {
46+
// get rid of previous contents
47+
hw.clear();
48+
// read the homework grades
49+
double x;
50+
while (in>>x)
51+
hw.push_back(x);
52+
// clear the stream so that input will work for the next student
53+
in.clear();
54+
}
55+
return in;
56+
}
57+
58+
int main() {
59+
cout << "Please enter your first name: ";
60+
string name;
61+
cin >> name;
62+
cout << "Hello, " << name << "!" << endl;
63+
cout << "Please enter your midterm and final exam grades: ";
64+
double midterm, final;
65+
cin >> midterm >> final;
66+
cout << "Enter all your homework grades, "
67+
"followed by end-of-file: ";
68+
vector<double> homework;
69+
read_hw(cin, homework);
70+
try {
71+
double final_grade = grade(midterm, final, homework);
72+
streamsize prec = cout.precision();
73+
cout << "Your final grade is " << setprecision(3)
74+
<< final_grade << setprecision(prec) << endl;
75+
} catch (domain_error) {
76+
cout << endl << "You must enter your grades. "
77+
"Please try again." << endl;
78+
return 1;
79+
}
80+
return 0;
81+
}
82+
83+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CXX = clang++
2+
C = clang++
3+
4+
all: grades

0 commit comments

Comments
 (0)