Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35COSC 181 – Foundations of Computer ProgrammingClass 11Announcement1st TestFri. Feb 20thComprehensive through the end of todayClosed Note/Closed BookRead and Interpret Code“trace” codeWrite Code SnippetsSmall programs or parts of programsSyntax matters!Multiple-ChoiceFormulating Algorithms: Counter-Controlled RepetitionProblem statementA 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 ofall student grades and the class average on the quiz.Finding the Algorithm is the hardest partCounter-controlled repetitionLoop repeated until counter reaches certain valueAlso known as definite repetitionNumber of repetitions known beforehandFormulating Algorithms: Counter-Controlled Repetition (Cont.)Counter-controlled repetition (Cont.)Counter variableUsed to countIn example, indicates which of the 10 grades is being enteredTotal variableUsed to accumulate the sum of several valuesNormally initialized to zero beforehandOtherwise it would include the previous value stored in that memory locationPseudocode1 Set total to zero2 Set grade counter to one34 While grade counter is less than or equal to ten5 Prompt the user to enter the next grade6 Input the next grade7 Add the grade into the total8 Add one to the grade counter910 Set the class average to the total divided by ten11 Print the total of the grades for all students in the class12 Print the class averageFig. 4.7Adding the Interface1 // Fig. 4.8: GradeBook.h2 / / Defin it io n of class GradeBook that determines a class average.3 / / Member funct ions are def ined in GradeBook.cpp4 #inc lude <string> // program uses C++ standard string class5 using std::string;67 // GradeBook class definition8 class GradeBook9 {10 public:11 GradeBook( string ); // constructor initializes course name12 void setCourseName( string ); // function to set the course name13 string getCourseName(); // function to retrieve the course name14 void displayMessage(); // display a welcome message15 void determineClassAverage(); // averages grades entered by the user16 pr ivate:17 string courseName; // course name for this GradeBook18 }; // end class GradeBook Function determineClassAverage implements the class average algorithm described by the pseudocodeAdding the Functionality – Part 131 // funct ion to ret r ie ve the course name32 string GradeBook::getCourseName()33 {34 return courseName;35 } // end funct ion getCourseName3637 // display a welcome message to the GradeBook user38 void GradeBook::displayMessage()39 {40 cout << "Welcome to the grade book for \n" << getCourseName() << "!\n" 41 << endl;42 } // end funct ion displayMessage4344 // determine class average based on 10 grades entered by user45 void GradeBook::determineClassAverage()46 {47 int total; / / sum of grades entered by user48 int gradeCounter; / / number of the grade to be entered next49 int grade; / / grade value entered by user50 int average; / / average of grades51Function determineClassAverage implements the class average algorithm described by the pseudocodeDeclare counter variable gradeCounterDeclare total variable totalAdding the Functionality – Part 252 // initialization phase53 total = 0; // initialize total54 gradeCounter = 1; // initialize loop counter5556 // processing phase57 while ( gradeCounter <= 10 ) // loop 10 times58 {59 cout << "Enter grade: "; // prompt for input60 cin >> grade; // input next grade61 total = total + grade; // add grade to total62 gradeCounter = gradeCounter + 1; // increment counter by 163 } // end while6465 // termination phase66 average = total / 10; // integer division yields integer result6768 // display total and average of grades69 cout << "\nTotal of all 10 grades is " << total << endl;70 cout << "Class average is " << average << endl; 71 } // end function determineClassAverageInitialize counter variable gradeCounter to 1Continue looping as long as gradeCounter’s value is less than or equal to 10Increment counter by 1, which causes gradeCounter to exceed 10 eventuallyPerform the averaging calculation and assign its result to the variable averageInitilize total variable total to 0Add the current grade to totalMain Function1 // Fig. 4.10: f ig04_10.cpp2 / / Create GradeBook object and invoke it s determ ineClassAverage funct ion .3 #inc lude "GradeBook.h" // include definition of class GradeBook45 in t main()6 {7 // create GradeBook object myGradeBook and 8 // pass course name to constructor9 GradeBook myGradeBook( "CS101 C++ Programming" );1011 myGradeBook.displayMessage(); // display welcome message12 myGradeBook.determineClassAverage(); // find average of 10 grades13 return 0; // indicate successful termination14 } // end mainWelcome to the grade book forCS101 C++ ProgrammingEnter grade: 67Enter grade: 78Enter grade: 89Enter grade: 67Enter grade: 87Enter grade: 98Enter grade: 93Enter grade: 85Enter grade: 82Enter grade: 100Total of all 10 grades is 846Class average is 84Important DetailsUninitialized variablesContain “garbage” (or undefined) valuesNotes on integer division and truncationInteger divisionWhen dividing two integersPerforms truncationFractional part of the resulting quotient is lostTruncation does not mean rounding7 ÷ 4 = 1.75Formulating Algorithms: Sentinel-Controlled RepetitionProblem statementDevelop a class average program that processes grades for an arbitrary number of students each time it is run.Sentinel-controlled repetitionAlso known as indefinite repetitionUse a sentinel valueIndicates “end of data entry”A sentinel value cannot also be a valid input valueAlso known as a signal, dummy or flag valueExample of Using a Sentinel Valuecout << “Enter a number; 0 to quite” << endl;cin >> myNumber;while(myNumber != 0){…}Choosing a sentinel value that is also a legitimate data value


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?