### Is there a shorter way

Is there a better/shorter way to write this program?
This is the problem:

Write a program which calculates a student’s end of semester grade based on the following grade information :

Lab Grade (50%) – Assume 4 Lab Assignments
Quiz Grade (10%) – Assume 3 quizzes and drop the lowest quiz grade.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````// Ch. 2 Exercise // Created by: Tucker Peebles #include using namespace std; int main() { double mid = 0.0; double final = 0.0; double quiz1 = 0.0; double quiz2 = 0.0; double quiz3 = 0.0; double lab1 = 0.0; double lab2 = 0.0; double lab3 = 0.0; double lab4 = 0.0; double grade = 0.0; cout << "Enter Midterm Exam Grade: "; cin >> mid; cout << "Enter Final Exam Grade: "; cin >> final; cout << "Enter Quiz 1 Grade: "; cin >> quiz1; cout << "Enter Quiz 2 Grade: "; cin >> quiz2; cout << "Enter Quiz 3 Grade: "; cin >> quiz3; cout << "Enter Lab 1 Grade: "; cin >> lab1; cout << "Enter Lab 2 Grade: "; cin >> lab2; cout << "Enter Lab 3 Grade: "; cin >> lab3; cout << "Enter Lab 4 Grade: "; cin >> lab4; if (quiz1>=quiz3 && quiz2>=quiz3) { grade = mid*.2 + final * .2 + ((lab1+lab2+lab3+lab4)/4)*.5 + ((quiz1+quiz2)/2)*.1; } else if (quiz1>=quiz2 && quiz3 >= quiz2) { grade = mid*.2 + final * .2 + ((lab1+lab2+lab3+lab4)/4)*.5 + ((quiz1+quiz3)/2)*.1; } else if (quiz2>=quiz1 && quiz3 >=quiz1) { grade = mid*.2 + final * .2 + ((lab1+lab2+lab3+lab4)/4)*.5 + ((quiz3+quiz2)/2)*.1; } cout << "Your end of semester grade is: "<
Only thing I would change is to compute the quiz total inside the if statement and compute the grade once outside the if statement. Adds a couple of source lines, but makes it easier to understand and less likely for errors to creep into the calculation of grade if you only do it once.

 ``123456789101112`` `````` double quiz_tot; ... if (quiz1>=quiz3 && quiz2>=quiz3) { quiz_tot = quiz1+quiz2; } else if (quiz1>=quiz2 && quiz3 >= quiz2) { quiz_tot = quiz1+quiz3; } else if (quiz2>=quiz1 && quiz3 >=quiz1) { quiz_tot = quiz3+quiz2; } grade = mid*.2 + final * .2 + ((lab1+lab2+lab3+lab4)/4)*.5 + (quiz_tot/2)*.1;``````

Last edited on
It's only a little shorter, but if you wanted to add 100 more assignments, it's less complicated to expand.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````class Assignment { std::string m_name; double m_weight; double m_grade; public: Assignment(const char* name, double weight = 1.0) : m_name(name), m_weight(weight) { std::cout << "Enter " << m_name << " Grade: "; std::cin >> m_grade; } friend double FinalGrade(std::vector& v); }; double FinalGrade(std::vector& v) { double average = 0.0; double weightedSum = 0.0; for (std::vector::iterator it = v.begin(); it != v.end(); ++it) { average += it->m_weight * it->m_grade; weightedSum += it->m_weight; } return average/weightedSum; } int main() { std::vector assignments; assignments.push_back(Assignment("Midterm Exam", 0.2)); assignments.push_back(Assignment("Final Exam", 0.2)); assignments.push_back(Assignment("Quiz 1", 0.025)); assignments.push_back(Assignment("Quiz 2", 0.025)); assignments.push_back(Assignment("Quiz 3", 0.025)); assignments.push_back(Assignment("Quiz 4", 0.025)); assignments.push_back(Assignment("Lab 1", 0.125)); assignments.push_back(Assignment("Lab 2", 0.125)); assignments.push_back(Assignment("Lab 3", 0.125)); assignments.push_back(Assignment("Lab 4", 0.125)); std::cout << "Your end of semester grade is: " << FinalGrade(assignments) << std::endl; return 0; }``````
Last edited on
Topic archived. No new replies allowed.