Complete this program so that it assigns and prints letter


Complete this program so that it assigns and prints letter grades by ranges (in addition to the percentage): 
A 90-100
B 80-89.99
C 70-79.99
D 60-69.99
F < 60
Also how many students fall into each category.

---------------------------------------------------------------

// Median Grades Calculation
// 

#include
#include
#include
#include
#include
#include

// using directives
using std::string; using std::vector;

using std::cin; using std::cout;
using std::istream; using std::streamsize;
using std::setprecision; using std::endl;

using std::sort; using std::max;
using std::domain_error;

///////////////////////////////////////////////////////////////////////////////
// hold all information related to a single student
struct student_info
{
string name; // students name
double midterm, final; // midterm and final exam grades
vector homework; // all homework grades
};

///////////////////////////////////////////////////////////////////////////////
// compute the median of a vector
// note: calling this function copies the whole vector
double median(vector vec)
{
typedef vector::size_type vec_sz;

vec_sz size = vec.size();
if (size == 0)
throw domain_error("vector is empty, median undefined");

sort(vec.begin(), vec.end());
vec_sz mid = size / 2;
return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}

///////////////////////////////////////////////////////////////////////////////
// compute a student's overall grade from midterm and final exam 
// grades and homework grade
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 all homework grades.
// This function does not copy the vector argument 
// (as median does so for us).
double grade(double midterm, double final, vector const& hw)
{
if (hw.size() == 0)
throw domain_error("student has done no homework");
return grade(midterm, final, median(hw));
}

// Calculate the final grade for one student
double grade(student_info const& s)
{
return grade(s.midterm, s.final, s.homework);
}

///////////////////////////////////////////////////////////////////////////////
// read homework grades from an input stream
// into a vector
istream& read_hw(istream& in, vector& hw)
{
if (in) {
hw.clear(); // get rid of previous contents

// read homework grades
double x;
while (cin >> x) 
hw.push_back(x);

// clear the stream so that input will work for
// the next student
in.clear();
}
return in;
}

// read all information related to one student
istream& read(istream& in, student_info& s)
{
// read the students name, midterm and final exam grades
in >> s.name >> s.midterm >> s.final;
// read all homework grades for this student
return read_hw(in, s.homework);
}

///////////////////////////////////////////////////////////////////////////////
// compare two student_info instances, return whether 'x'
// is smaller than 'y' based on comparing the stored names
// of the students
bool compare(student_info const& x, student_info const& y)
{
return x.name < y.name;
}

///////////////////////////////////////////////////////////////////////////////
int main()
{
vector students; // all student records
string::size_type maxlen = 0; // length of longest name 

// read and store all the records, find the length of 
// the longest name
student_info record;
while (read(cin, record)) {
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}

// alphabetize the records
sort(students.begin(), students.end(), compare);

for (vector::size_type i = 0; i != students.size(); ++i) {
// write the name, padded on the right side to maxlen + 1 characters
cout << students[i].name 
<< string(maxlen + 1 - students[i].name.size(), ' '); 

// compute and write the grade
try {
double final_grade = grade(students[i]);
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade << setprecision(prec);

catch (domain_error e) {
cout << e.what();
}
cout << endl;
}

return 0;
}

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Complete this program so that it assigns and prints letter
Reference No:- TGS095703

Expected delivery within 24 Hours