April 5, 2008
WikiClass average problem pseudocode algorithm with sentinel-controlled repetition. Also using float
#wiki
Class average problem pseudocode algorithm with sentinel-controlled repetition. Also using float
1 Initialize total to zero
2 Initialize counter to zero
3
4 Prompt the user to enter the first grade
5 Input the first grade (possibly the sentinel)
6
7 While the user has not yet entered the sentinel
8 Add this grade into the running total
9 Add one to the grade counter
10 Prompt the user to enter the next grade
11 Input the next grade (possibly the sentinel)
12
13 If the counter is not equal to zero
14 Set the average to the total divided by the counter
15 Print the total of the grades for all students in the class
16 Print the class average
17 else
18 Print "No grades were entered"
GradeBook header file.
1 // 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 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
GradeBook source code file.
1 // GradeBook.cpp
2 // Member-function definitions for class GradeBook that solves the
3 // class average program with sentinel-controlled repetition.
4 #include <iostream>
5 using std::cout;
6 using std::cin;
7 using std::endl;
8 using std::fixed; // ensures that decimal point is displayed
9
10 #include <iomanip> // parameterized stream manipulators
11 using std::setprecision; // sets numeric output precision
12
13 // include definition of class GradeBook from GradeBook.h
14 #include "GradeBook.h"
15
16 // constructor initializes courseName with string supplied as argument
17 GradeBook::GradeBook( string name )
18 {
19 setCourseName( name ); // validate and store courseName
20 } // end GradeBook constructor
21
22 // function to set the course name;
23 // ensures that the course name has at most 25 characters
24 void GradeBook::setCourseName( string name )
25 {
26 if ( name.length() <= 25 ) // if name has 25 or fewer characters
27 courseName = name; // store the course name in the object
28 else // if name is longer than 25 characters
29 { // set courseName to first 25 characters of parameter name
30 courseName = name.substr( 0, 25 ); // select first 25 characters
31 cout << "Name \"" << name << "\" exceeds maximum length (25).\n"
32 << "Limiting courseName to first 25 characters.\n" << endl;
33 } // end if...else
34 } // end function setCourseName
35
36 // function to retrieve the course name
37 string GradeBook::getCourseName()
38 {
39 return courseName;
40 } // end function getCourseName
41
42 // display a welcome message to the GradeBook user
43 void GradeBook::displayMessage()
44 {
45 cout << "Welcome to the grade book for\n" << getCourseName() << "!\n"
46 << endl;
47 } // end function displayMessage
48
49 // determine class average based on 10 grades entered by user
50 void GradeBook::determineClassAverage()
51 {
52 int total; // sum of grades entered by user
53 int gradeCounter; // number of grades entered
54 int grade; // grade value
55 double average; // number with decimal point for average
56
57 // initialization phase
58 total = 0; // initialize total
59 gradeCounter = 0; // initialize loop counter
60
61 // processing phase
62 // prompt for input and read grade from user
63 cout << "Enter grade or -1 to quit: ";
64 cin >> grade; // input grade or sentinel value
65
66 // loop until sentinel value read from user
67 while ( grade != -1 ) // while grade is not -1
68 {
69 total = total + grade; // add grade to total
70 gradeCounter = gradeCounter + 1; // increment counter
71
72 // prompt for input and read next grade from user
73 cout << "Enter grade or -1 to quit: ";
74 cin >> grade; // input grade or sentinel value
75 } // end while
76
77 // termination phase
78 if ( gradeCounter != 0 ) // if user entered at least one grade...
79 {
80 // calculate average of all grades entered
81 average = static_cast< double >( total ) / gradeCounter;
82
83 // display total and average (with two digits of precision)
84 cout << "\nTotal of all " << gradeCounter << " grades entered is "
85 << total << endl;
86 cout << "Class average is " << setprecision( 2 ) << fixed << average
87 << endl;
88 } // end if
89 else // no grades were entered, so output appropriate message
90 cout << "No grades were entered" << endl;
91 } // end function determineClassAverage
- Using type double in the current example allows us to store the class average calculation’s result as a floating-point number.
- Variables of type float represent single-precision floating-point numbers and have seven significant digits on most 32-bit systems today. Variables of type double represent double-precision floating-point numbers. These require twice as much memory as float variables and provide 15 significant digits on most 32-bit systems todayapproximately double the precision of float variables.
- Line 81 uses the cast operator static_cast< double >( total ) to create a temporary floating-point copy of its operand in parenthesestotal. Using a cast operator in this manner is called explicit conversion. The value stored in total is still an integer.
- Cast operators are available for use with every data type and with class types as well. The static_cast operator is formed by following keyword static_cast with angle brackets (< and >) around a data type name.
- The call to setprecision in line 86 (with an argument of 2) indicates that double variable average should be printed with two digits of precision to the right of the decimal point (e.g., 92.37). This call is referred to as a parameterized stream manipulator (because of the 2 in parentheses). Programs that use these calls must contain the preprocessor directive #include
(line 10) \ - The stream manipulator fixed (line 86) indicates that floating-point values should be output in so-called fixed-point format, as opposed to scientific notation. \
- When the stream manipulators fixed and setprecision are used in a program, the printed value is rounded to the number of decimal positions indicated by the value passed to setprecision (e.g., the value 2 in line 86), although the value in memory remains unaltered. For example, the values 87.946 and 67.543 are output as 87.95 and 67.54, respectively. Note that it also is possible to force a decimal point to appear by using stream manipulator showpoint. If showpoint is specified without fixed, then trailing zeros will not print. Like endl, stream manipulators fixed and showpoint are nonparameterized and do not require the
header file. Both can be found in header .
Creating an object of class GradeBook and invoking its determineClassAverage member function.
1 // fig04_14.cpp
2 // Create GradeBook object and invoke its determineClassAverage function.
3
4 // include definition of class GradeBook from GradeBook.h
5 #include "GradeBook.h"
6
7 int main()
8 {
9 // create GradeBook object myGradeBook and
10 // pass course name to constructor
11 GradeBook myGradeBook( "CS101 C++ Programming" );
12
13 myGradeBook.displayMessage(); // display welcome message
14 myGradeBook.determineClassAverage(); // find average of 10 grades
15 return 0; // indicate successful termination
16 } // end main
Continue Reading
Back to Archive