// assn2.cpp // This program is intended to read in student grade data and calculate final // grades according to the syllabus for the class. // This is a best case design. It breaks the problem into smaller and // more managable pieces that are more likely to be useful in other contexts. // It uses the reference passing method that is shown in another example // solution to get the accumulated variables to be updated in the recursive // function. #include #include double min(double n1,double n2); // Return min of two numbers. double assnAverage(void); // Read assignment grades and return average. double quizAverage(void); // Read quiz grades and return average with one drop. double testAverage(void); // Read test grades and return average. double classAverage(void); // Read student grades and return average. int recursiveRead(int &As,int &Bs,int &Cs,int &Ds,int &Fs,double &sum); // This function reads a single student record. It uses the variables passed // by reference to accumulate values over many students. It uses recursion to // read subsequent students and returns the number of students read. void main(void) { int numAs=0; int numBs=0; int numCs=0; int numDs=0; int numFs=0; double gradeSum=0.0; int numStudents=recursiveRead(numAs,numBs,numCs,numDs,numFs,gradeSum); // Do final output. cout << "There were " << numStudents << " students in the class.\n"; if(numStudents>0) { cout << "The average grade was " << gradeSum/numStudents << ".\n"; cout << "There were " << numAs << " students with As.\n"; cout << "There were " << numBs << " students with Bs.\n"; cout << "There were " << numCs << " students with Cs.\n"; cout << "There were " << numDs << " students with Ds.\n"; cout << "There were " << numFs << " students with Fs.\n"; } else { cout << "With no students there was no average and no grades of any type.\n"; } } double min(double n1,double n2) { return(n1> assn1 >> assn2 >> assn3 >> assn4 >> assn5 >> assn6 >> assn7 >> assn8; return (assn1+assn2+assn3+assn4+assn5+assn6+assn7+assn8)/8.0; } double quizAverage(void) { // Variables for storing the quiz grades. double quiz1; double quiz2; double quiz3; double quiz4; double quiz5; double quiz6; cin >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> quiz5 >> quiz6; double quizMin=min(quiz1,min(quiz2,min(quiz3,min(quiz4,min(quiz5,quiz6))))); return (quiz1+quiz2+quiz3+quiz4+quiz5+quiz6-quizMin)/5.0; } double testAverage(void) { // Variables for storing the test grades. double test1; double test2; cin >> test1 >> test2; return (test1+test2)/2.0; } double classAverage(void) { double classParticip; // Calculate assignment average. double assnAve=assnAverage(); // Calculate quiz average. double quizAve=quizAverage(); // Calculate test average. double testAve=testAverage(); cin >> classParticip; // Calculate final average. return 0.5*assnAve+0.3*testAve+0.1*quizAve+0.1*classParticip; } int recursiveRead(int &As,int &Bs,int &Cs,int &Ds,int &Fs,double &sum) { string firstName; string lastName; cin >> firstName; if(firstName=="quit") return 0; cin >> lastName; // Calculate final average. double finalAve=classAverage(); // Output line for student. cout << firstName << " " << lastName << " " << finalAve << endl; // Increment the sum of the grades. sum+=finalAve; // Decide grade type and increment. if(finalAve>=90.0) { As++; } else if(finalAve>=80.0) { Bs++; } else if(finalAve>=70.0) { Cs++; } else if(finalAve>=60.0) { Ds++; } else { Fs++; } return recursiveRead(As,Bs,Cs,Ds,Fs,sum)+1; }