This is Part 5 of the 7-part Exercise 6-0. Click here to see the other parts.
Exercise 6-0 (Part 5 / 7)
Relating to section 6.2 of the textbook (page 110-116). Implement and test out the “Comparing Grading Schemes” program. This program reads in a set of student’s grades (from mid-term exam, final exam, and individual homework grades), compute the student’s final grades using the 3 grading schemes (median homework, average homework, and optimistic-median homework), split the students into two groups (did and didn’t do all homework), and for each group compute the group medians associating with the grading schemes. i.e. the end result consists of 6 total grades: medians computed using the 3 schemes for group A (students who did all homework), and medians computed using the 3 schemes for group B (students who didn’t do all homework). The exercise introduces one key idea: parsing a function as an argument to another function. From Chapter 10 we shall see this is actually facilitated by the “hidden” pointer and array mechanism.
The Project
This section summarises the partitioned program in the form of C++ source and header files.
Source File List
Header File List
Source Files
main.cpp
#include <iostream> // cin, cout, endl
#include <vector> // vector
#include "Student_info.h" // Student_info
#include "did_all_hw.h" // did_all_hw
#include "write_analysis.h" // write_analysis
#include "median_analysis.h" // median_analysis
#include "average_analysis.h" // average_analysis
#include "optimistic_median_analysis.h" // optimistic_median_analysis
using std::cin;
using std::cout;
using std::endl;
using std::vector;
// Compare the Grading Scheme
// (S6.2.3/114)
int main()
{
// students who did and didn't do all their homework
vector<Student_info> did, didnt;
// read the student records and partition them
Student_info student;
while (read(cin, student)) {
if (did_all_hw(student))
did.push_back(student);
else
didnt.push_back(student);
}
// verify that the analyses will show us something
if (did.empty()) {
cout << "No student did all the homework!" << endl;
return 1;
}
if (didnt.empty()) {
cout << "No student did all the homework!" << endl;
return 1;
}
// do the analyses
write_analysis(cout, "median", median_analysis, did, didnt);
write_analysis(cout, "average", average_analysis, did, didnt);
write_analysis(cout, "median of homework turned in",
optimistic_median_analysis, did, didnt);
return 0;
}
average.cpp
#include <vector> // vector
#include <numeric> // numeric
using std::vector;
using std::accumulate;
// Compute average of elements
// (S6.2.3/115)
double average(const vector<double>& v)
{
return accumulate(v.begin(), v.end(), 0.0) / v.size();
}
average_analysis.cpp
#include <vector> // vector
#include <algorithm> // transform
#include "Student_info.h" // Student_info
#include "average_grade.h" // average_grade
#include "median.h" // median
using std::vector;
using std::transform;
// Perform average analysis
// (S6.2.3/115)
double average_analysis(const vector<Student_info>& students)
{
vector<double> grades;
transform(students.begin(), students.end(),
back_inserter(grades), average_grade);
return median(grades);
}
average_grade.cpp
#include "Student_info.h" // Student_info
#include "grade.h" // grade
#include "average.h" // average
// Compute the final grade using average of homework
// (S6.2.3/115)
double average_grade(const Student_info& s)
{
return grade(s.midterm, s.final, average(s.homework));
}
did_all_hw.cpp
#include <algorithm> // find
#include "Student_info.h" // Student_info
// Has the student done all the homework?
// (S6.2.1/110)
bool did_all_hw(const Student_info& s)
{
return ((find(s.homework.begin(), s.homework.end(), 0)) == s.homework.end());
}
grade.cpp
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"
using std::domain_error;
using std::vector;
// definitions for the grade functions from S4.1/52, S4.1.2/54, S4.2.2/63
// compute a student's overall grade from midterm and final exam
// grades and homework grade (S4.1/52)
double grade(double midterm, double final, double homework)
{
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
// compute a student's overall grade from midterm and final exam grades
// and vector of homework grades.
// this function does not copy its argument, because median (function) does it for us.
// (S4.1.2/54)
double grade(double midterm, double final, const vector<double>& hw)
{
if (hw.size() == 0)
throw domain_error("student has done no homework");
return grade(midterm, final, median(hw));
}
// this function computes the final grade for a Student_info object
// (S4.2.2/63)
double grade(const Student_info& s)
{
return grade(s.midterm, s.final, s.homework);
}
grade_aux.cpp
#include "Student_info.h" // Student_info
#include "grade.h" // grade
#include <stdexcept> // domain_error
using std::domain_error;
// Auxiliary function to be parsed as argument to a function
// (S6.2.2/113)
double grade_aux(const Student_info& s)
{
try {
return grade(s);
} catch (domain_error) {
return grade(s.midterm, s.final, 0);
}
}
// source file for the median function
#include <algorithm>
#include <stdexcept>
#include <vector>
using std::domain_error;
using std::sort;
using std::vector;
// compute the median of a vector<double>
double median(vector<double> vec)
{
typedef vector<double>::size_type vec_sz;
vec_sz size = vec.size();
if (size == 0)
throw domain_error("median of an empty vector");
sort(vec.begin(),vec.end());
vec_sz mid = size/2;
return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}
#include <vector> // vector
#include <algorithm> // transform
#include "Student_info.h" // Student_info
#include "grade_aux.h" // grade
#include "median.h" // median
using std::vector;
using std::transform;
// S6.2.2/112
double median_analysis(const vector<Student_info>& students)
{
vector<double> grades;
transform(students.begin(), students.end(), back_inserter(grades), grade_aux);
return median(grades);
}
#include <vector> // vector
#include <algorithm> // remove_copy, back_inserter
#include "Student_info.h" // Student_info
#include "grade.h" // grade
#include "median.h" // median
using std::vector;
// median of the nonzero elements of s.homework, or 0 if no such elements exist
double optimistic_median(const Student_info& s)
{
vector<double> nonzero;
remove_copy(s.homework.begin(), s.homework.end(),
back_inserter(nonzero), 0);
if (nonzero.empty())
return grade(s.midterm, s.final, 0);
else
return grade(s.midterm, s.final, median(nonzero));
}
#include <vector> // vector
#include <algorithm> // transform
#include "Student_info.h" // Student_info
#include "optimistic_median.h" // optimistic_median
#include "median.h" // median
using std::vector;
using std::transform;
// Auxiliary function
// (S6.2.3/115) - adjust accordingly
double optimistic_median_analysis(const vector<Student_info>& students)
{
vector<double> grades;
transform(students.begin(), students.end(),
back_inserter(grades), optimistic_median);
return median(grades);
}
Student_info.cpp
#include "Student_info.h"
using std::istream;
using std::vector;
// we are interested in sorting the Student_info object by the student's name
bool compare(const Student_info& x, const Student_info& y)
{
return x.name < y.name;
}
// read student's name, midterm exam grade, final exam grade, and homework grades
// and store into the Student_info object
// (as defined in S4.2.2/62)
istream& read(istream& is, Student_info& s)
{
// read and store the student's name and midterm and final exam grades
is >> s.name >> s.midterm >> s.final;
// read and store all the student's homework grades
read_hw(is, s.homework);
return is;
}
// read homework grades from an input stream into a vector<double>
// (as defined in S4.1.3/57)
istream& read_hw(istream& in, vector<double>& hw)
{
if (in)
{
// get rid of previous contents
hw.clear();
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}
write_analysis.cpp
#include <iostream> // ostream, endl;
#include <vector> // vector
#include <string> // string
#include "Student_info.h" // Student_info
using std::ostream;
using std::endl;
using std::string;
using std::vector;
// Output result to compare the two groups of students who did and
// who didn't do all of their homework.
// (S6.2.3/113)
void write_analysis(ostream& out,
const string& name,
double analysis(const vector<Student_info>&),
const vector<Student_info>& did,
const vector<Student_info>& didnt)
{
out << name << ": median(did) = " << analysis(did) <<
": median(didnt) = " << analysis(didnt) << endl;
return;
}
Header Files
average.h
#ifndef GUARD_AVERAGE_H
#define GUARD_AVERAGE_H
// average.h
#include <vector>
double average(const std::vector<double>&);
#endif // GUARD_AVERAGE_H
average_analysis.h
#ifndef GUARD_AVERAGE_ANALYSIS_H
#define GUARD_AVERAGE_ANALYSIS_H
// average_analysis.h
#include <vector>
double average_analysis(const std::vector<Student_info>&);
#endif // GUARD_AVERAGE_ANALYSIS_H
average_grade.h
#ifndef GUARD_AVERAGE_GRADE_H
#define GUARD_AVERAGE_GRADE_H
// average_grade.h
#include "Student_info.h" // Student_info
double average_grade(const Student_info&);
#endif // GUARD_AVERAGE_GRADE_H
did_all_hw.h
#ifndef GUARD_DID_ALL_HW_H
#define GUARD_DID_ALL_HW_H
// did_all_hw.h
#include "Student_info.h" // Student_info
bool did_all_hw(const Student_info&);
#endif // GUARD_DID_ALL_HW_H
grade.h
#ifndef GUARD_GRADE_H
#define GUARD_GRADE_H
// grade.h
#include <vector>
#include "Student_info.h"
double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const Student_info&);
#endif // GUARD_GRADE_H
grade_aux.h
#ifndef GUARD_GRADE_AUX_H
#define GUARD_GRADE_AUX_H
// grade_aux.h
#include "Student_info.h" // Student_info
double grade_aux(const Student_info&);
#endif // GUARD_GRADE_AUX_H
#ifndef GUARD_MEDIAN_H
#define GUARD_MEDIAN_H
// median.h
#include <vector>
double median(std::vector<double>);
#endif // GUARD_MEDIAN_H
#ifndef GUARD_MEDIAN_ANALYSIS_H
#define GUARD_MEDIAN_ANALYSIS_H
// median_analysis.h
#include <vector>
#include "Student_info.h" // Student_info
double median_analysis(const std::vector<Student_info>&);
#endif // GUARD_MEDIAN_ANALYSIS_H
#ifndef GUARD_OPTIMISTIC_MEDIAN_H
#define GUARD_OPTIMISTIC_MEDIAN_H
// optimistic_median.h
#include "Student_info.h" // Student_info
double optimistic_median(const Student_info&);
#endif // GUARD_OPTIMISTIC_MEDIAN_H
#ifndef GUARD_OPTIMISTIC_MEDIAN_ANALYSIS_H
#define GUARD_OPTIMISTIC_MEDIAN_ANALYSIS_H
// optimistic_median_analysis.h
#include <vector>
double optimistic_median_analysis(const std::vector<Student_info>&);
#endif // GUARD_OPTIMISTIC_MEDIAN_ANALYSIS_H
Student_info.h
#ifndef GUARD_STUDENT_INFO_H
#define GUARD_STUDENT_INFO_H
// Student_info.h
#include <iostream>
#include <string>
#include <vector>
struct Student_info
{
std::string name;
double midterm, final;
std::vector<double> homework;
};
bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);
#endif // GUARD_STUDENT_INFO_H
write_analysis.h
#ifndef GUARD_WRITE_ANALYSIS_H
#define GUARD_WRITE_ANALYSIS_H
// write_analysis.h
#include <iostream> // ostream;
#include <vector> // vector
#include <string> // string
#include "Student_info.h" // Student_info
void write_analysis(std::ostream&,
const std::string&,
double analysis(const std::vector<Student_info>&),
const std::vector<Student_info>&,
const std::vector<Student_info>&);
#endif // GUARD_WRITE_ANALYSIS_H
Test Program
I now submit a set of hypyethetical student grades. As expected, the optimistic scheme (3rd one), gives higher median grades for the students who didn’t do all the homework.
pete 100 100 100 100 100
jon 90 90 0 0 0
mary 50 50 50 50 50
anna 40 40 0 0 0
gary 80 80 0 80 0
bob 100 100 100 0 0
ken 20 88 99 44 66
jay 99 39 40 80 0
bill 20 88 0 39 0
^Z
^Z
median: median(did) = 65.6: median(didnt) = 49.7
average: median(did) = 67.0667: median(didnt) = 52.7
median of homework turned in: median(did) = 65.6: median(didnt) = 57.1
Reference
Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000