 C++ for Programmers: Control Statements: Part 1

• Print
This chapter is from the book

4.6 Counter-Controlled Repetition

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 (integers in the range 0 to 100) for this quiz are available to you. Calculate and display the total of all student grades and the class average on the quiz.

The class average is equal to the sum of the grades divided by the number of students. The program for solving this problem 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 (Fig. 4.6–Fig. 4.7) that implements the class average 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 // program uses C++ standard string class 5 using std::string; 6 7 // GradeBook class definition 8 class GradeBook 9 { 10 public: 11 GradeBook( string ); // constructor initializes course name 12 void setCourseName( string ); // function to set the course name 13 string getCourseName(); // function to retrieve the course name 14 void displayMessage(); // display a welcome message 15 void determineClassAverage(); // averages grades entered by the user 16 private: 17 string courseName; // course name for this GradeBook 18 }; // end class GradeBook ```

Fig. 4.7 Class average problem using counter-controlled repetition: GradeBook source code file.

 ``` 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 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include "GradeBook.h" // include definition of class GradeBook 10 11 // constructor initializes courseName with string supplied as argument 12 GradeBook::GradeBook( string name ) 13 { 14 setCourseName( name ); // validate and store courseName 15 } // end GradeBook constructor 16 17 // function to set the course name; 18 // ensures that the course name has at most 25 characters 19 void GradeBook::setCourseName( string name ) 20 { 21 if (name.length() <= 25 ) // if name has 25 or fewer characters 22 courseName = name; // store the course name in the object 23 else // if name is longer than 25 characters 24 { // set courseName to first 25 characters of parameter name 25 courseName = name.substr( 0, 25 ); // select first 25 characters 26 cout << "Name \"" << name << "\" exceeds maximum length (25).\n" 27 << "Limiting courseName to first 25 characters.\n" << endl; 28 } // end if...else 29 } // end function setCourseName 30 31 // function to retrieve the course name 32 string GradeBook::getCourseName() 33 { 34 return courseName; 35 } // end function getCourseName 36 37 // display a welcome message to the GradeBook user 38 void GradeBook::displayMessage() 39 { 40 cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" 41 << endl; 42 } // end function displayMessage 43 44 // determine class average based on 10 grades entered by user 45 void GradeBook::determineClassAverage() 46 { 47 int total; // sum of grades entered by user 48 int gradeCounter; // number of the grade to be entered next 49 int grade; // grade value entered by user 50 int average; // average of grades 51 52 // initialization phase 53 total = 0; // initialize total 54 gradeCounter = 1; // initialize loop counter 55 56 // processing phase 57 while ( gradeCounter >= 10 ) // loop 10 times 58 { 59 cout << "Enter grade: "; // prompt for input 60 cin >> grade; // input next grade 61 total = total + grade; // add grade to total 62 gradeCounter = gradeCounter + 1; // increment counter by 1 63 } // end while 64 65 // termination phase 66 average = total / 10; // integer division yields integer result 67 68 // display total and average of grades 69 cout << "\nTotal of all 10 grades is " << total << endl; 70 cout << "Class average is " << average << endl; 71 } // end function determineClassAverage ```

Fig. 4.8 Class average problem using counter-controlled repetition: Creating an object of class GradeBook (Fig. 4.6–Fig. 4.7) and invoking its determineClassAverage function.

 ``` 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 return 0; // indicate successful termination 14 } // 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```

Before we discuss the class average algorithm's implementation, let's consider an enhancement we made to our GradeBook class. In Fig. 3.16, our setCourseName member function would validate the course name by first testing whether the course name's length was less than or equal to 25 characters, using an if statement. If this was true, the course name would be set. This code was then followed by another if statement that tested whether the course name's length was larger than 25 characters (in which case the course name would be shortened). Notice that the second if statement's condition is the exact opposite of the first if statement's condition. If one condition evaluates to true, the other must evaluate to false. Such a situation is ideal for an if...else statement, so we've modified our code, replacing the two if statements with one if...else statement (lines 21–28 of Fig. 4.7).

Implementing Counter-Controlled Repetition in Class GradeBook

Class GradeBook (Fig. 4.6–Fig. 4.7) contains a constructor (declared in line 11 of Fig. 4.6 and defined in lines 12–15 of Fig. 4.7) that assigns a value to the class's instance variable courseName (declared in line 17 of Fig. 4.6). Lines 19–29, 32–35 and 38–42 of Fig. 4.7 define member functions setCourseName, getCourseName and displayMessage, respectively. Lines 45–71 define member function determineClassAverage.

Lines 47–50 declare local variables total, gradeCounter, grade and average to be of type int. Variable grade stores the user input. Notice that the preceding declarations appear in the body of member function determineClassAverage.

In this chapter's versions of class GradeBook, we simply read and process a set of grades. The averaging calculation is performed in member function determineClass-Average using local variables—we do not preserve any information about student grades in the class's instance variables. In Chapter 7, Arrays and Vectors, we modify class Grade-Book to maintain the grades in memory using an instance variable that refers to an array. This allows a GradeBook object to perform various calculations on the same set of grades without requiring the user to enter the grades multiple times.

Lines 53–54 initialize total to 0 and gradeCounter to 1. Variables grade and average (for the user input and calculated average, respectively) need not be initialized here—their values will be assigned as they are input or calculated later in the function.

Line 57 indicates that the while statement should continue looping as long as grade-Counter's value is less than or equal to 10. While this condition remains true, the while statement repeatedly executes the statements between the braces that delimit its body (lines 58–63).

Line 59 displays the prompt "Enter grade: ". Line 60 reads the grade entered by the user and assigns it to variable grade. Line 61 adds the new grade entered by the user to the total and assigns the result to total, which replaces its previous value.

Line 62 adds 1 to gradeCounter to indicate that the program has processed a grade and is ready to input the next grade from the user. Incrementing gradeCounter eventually causes gradeCounter to exceed 10. At that point the while loop terminates because its condition (line 57) becomes false.

When the loop terminates, line 66 performs the averaging calculation and assigns its result to the variable average. Line 69 displays the text "Total of all 10 grades is " followed by variable total's value. Line 70 then displays the text "Class average is " followed by variable average's value. Member function determineClassAverage then returns control to the calling function (i.e., main in Fig. 4.8).