Is there a shorter way
Jan 10, 2013 at 10:46pm UTC
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 :
Midterm Exam Grade (20%)
Final Exam Grade (20%)
Lab Grade (50%) – Assume 4 Lab Assignments
Quiz Grade (10%) – Assume 3 quizzes and drop the lowest quiz grade.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// Ch. 2 Exercise
// Created by: Tucker Peebles
#include <iostream>
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: " <<grade<<endl;
system("pause" );
return 0;
}
Jan 10, 2013 at 11:01pm UTC
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.
1 2 3 4 5 6 7 8 9 10 11 12
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 Jan 10, 2013 at 11:02pm UTC
Jan 11, 2013 at 6:57am UTC
It's only a little shorter, but if you wanted to add 100 more assignments, it's less complicated to expand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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<Assignment>& v);
};
double FinalGrade(std::vector<Assignment>& v)
{
double average = 0.0;
double weightedSum = 0.0;
for (std::vector<Assignment>::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<Assignment> 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 Jan 11, 2013 at 6:58am UTC
Topic archived. No new replies allowed.