//The following has been sellected as the best solution to the bad.cc problem //My reasons will be put as comments like this //RJB... Dr. R Botting /* Lab 03 assignment of BAD.CC. Programmer: John P. McDaniel Revision Date: 17 Apr 95 Program name: Averages. Saved as: good.cc //RJB John took the trouble to document the code. //RJB This program is going to be changed several times so it is worth it. --------Problem----------- Get five inputs and compute the average for output. --------Analysis----------- Get five inputs. Calculate the average of the five inputs by adding all five values and dividing by the number of the values. Data Types const int total_grades = 5 //RJB It is always worth naming every constant value in a program with //RJB the exception of 0, 1 and -1 perhaps. //RJB Nitpick: it should be called number_of_grades or some such Input double grade1, grade2, grade3, grade4, grade5 //RJB Fixes problem of decimal input //RJB Does not assume that we know what the grades are Output double grade_ave Formulae grade_ave = (grade1 + grade2 + grade3 + grade4 + grade5) / total_grades ----Design--------- Initial Algorithm 1. Output instructions. 2. Get inputs. 3. Calculate average. 4. Print output. Refinements 2. 1. grade_ave = (grade1 + grade2 + grade3 + grade4 + grade5) / total_grades ----Program Implementation------- */ #include int main () { //Declaring the constant. The number of grades to be averaged. const int total_grades = 5; //Declaring the variable inputs for the grades and the variable output. double grade1, grade2, grade3, grade4, grade5, grade_ave; //Output instructions cout << "\nThis program averages five grades. Please input each grade\n" << "value and press return.\nStart inputing values now.\n"; //Input the five grades. cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; //Calculate the average of the five grades. grade_ave = (grade1 + grade2 + grade3 + grade4 + grade5) / total_grades; //Producing the result to output. cout << "\nThe average is " << grade_ave << ".\n\n"; //Returning the error code of zero for 'Worked fine'. return 0; }