This section and Section 4.7 solve two variations of a class average problem. Consider the following problem statement:
A class of ten students took a quiz. The grades (0 to 100) for this quiz are available to you. Calculate and display the total of the grades and the class average.
The class average is equal to the sum of the grades divided by the number of students. The algorithm for solving this problem on a computer must input each of the grades, calculate the average and print the result. We use counter-controlled repetition to input the grades one at a time.
This section presents a version of class GradeBook
(Figs. 4.6–4.7) that implements the algorithm in a C++ member function, and an application (Fig. 4.8) that demonstrates the algorithm in action.
1 // Fig. 4.6: GradeBook.h
2 // Definition of class GradeBook that determines a class average.
3 // Member functions are defined in GradeBook.cpp
4 #include <string> // program uses C++ standard string class
5
6 // GradeBook class definition
7 class GradeBook
8 {
9 public:
10 explicit GradeBook( std::string ); // initializes course name
11 void setCourseName( std::string ); // set the course name
12 std::string getCourseName() const; // retrieve the course name
13 void displayMessage() const; // display a welcome message
14 void determineClassAverage() const; // averages user-entered grades
15 private:
16 std::string courseName; // course name for this GradeBook
17 }; // end class GradeBook
1 // Fig. 4.7: GradeBook.cpp
2 // Member-function definitions for class GradeBook that solves the
3 // class average program with counter-controlled repetition.
4 #include <iostream>
5 #include "GradeBook.h" // include definition of class GradeBook
6 using namespace std;
7
8 // constructor initializes courseName with string supplied as argument
9 GradeBook::GradeBook( string name )
10 {
11 setCourseName( name ); // validate and store courseName
12 } // end GradeBook constructor
13
14 // function to set the course name;
15 // ensures that the course name has at most 25 characters
16 void GradeBook::setCourseName( string name )
17 {
18 if ( name.size() <= 25 ) // if name has 25 or fewer characters
19 courseName = name; // store the course name in the object
20 else // if name is longer than 25 characters
21 { // set courseName to first 25 characters of parameter name
22 courseName = name.substr( 0, 25 ); // select first 25 characters
23 cerr << "Name "" << name << "" exceeds maximum length (25).
"
24 << "Limiting courseName to first 25 characters.
" << endl;
25 } // end if...else
26 } // end function setCourseName
27
28 // function to retrieve the course name
29 string GradeBook::getCourseName() const
30 {
31 return courseName;
32 } // end function getCourseName
33
34 // display a welcome message to the GradeBook user
35 void GradeBook::displayMessage() const
36 {
37 cout << "Welcome to the grade book for
" << getCourseName() << "!
"
38 << endl;
39 } // end function displayMessage
40
41 // determine class average based on 10 grades entered by user
42 void GradeBook::determineClassAverage() const
43 {
44 // initialization phase
45 int total = 0; // sum of grades entered by user
46 unsigned int gradeCounter = 1; // number of grade to be entered next
47
48 // processing phase
49 while ( gradeCounter <= 10
) // loop 10 times
50 {
51 cout << "Enter grade: "; // prompt for input
52 int grade = 0; // grade value entered by user
53 cin >> grade; // input next grade
54 total = total + grade; // add grade to total
55 gradeCounter = gradeCounter + 1; // increment counter by 1
56 } // end while
57
58 // termination phase
59 int average = total / 10; // ok to mix declaration and calculation
60
61 // display total and average of grades
62 cout << "
Total of all 10 grades is " << total << endl;
63 cout << "Class average is " << average << endl;
64 } // end function determineClassAverage
1 // Fig. 4.8: fig04_08.cpp
2 // Create GradeBook object and invoke its determineClassAverage function.
3 #include "GradeBook.h" // include definition of class GradeBook
4
5 int main()
6 {
7 // create GradeBook object myGradeBook and
8 // pass course name to constructor
9 GradeBook myGradeBook( "CS101 C++ Programming" );
10
11 myGradeBook.displayMessage(); // display welcome message
12 myGradeBook.determineClassAverage(); // find average of 10 grades
13 } // end main
Welcome to the grade book for
CS101 C++ Programming
Enter grade: 67
Enter grade: 78
Enter grade: 89
Enter grade: 67
Enter grade: 87
Enter grade: 98
Enter grade: 93
Enter grade: 85
Enter grade: 82
Enter grade: 100
Total of all 10 grades is 846
Class average is 84