View the step-by-step solution to:

I have to 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...

I have to 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 <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <stdexcept>

// 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<double> homework; // all homework grades
};

///////////////////////////////////////////////////////////////////////////////
// compute the median of a vector<double>
// note: calling this function copies the whole vector
double median(vector<double> vec)
{
typedef vector<double>::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<double> 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<double>
istream& read_hw(istream& in, vector<double>& 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<student_info> 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<student_info>::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;
}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question