COSC 181 Foundations of Computer Programming Class 11 Announcement 1st Test Fri Feb 20th Comprehensive through the end of today Closed Note Closed Book Read and Interpret Code Write Code Snippets trace code Small programs or parts of programs Syntax matters Multiple Choice Formulating Algorithms Counter Controlled Repetition 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 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 beforehand Formulating Algorithms CounterControlled 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 location Pseudocode 1 Set total to zero 2 Set grade counter to one 3 4 While grade counter is less than or equal to ten 5 Prompt the user to enter the next grade 6 Input the next grade 7 Add the grade into the total 8 Add one to the grade counter 9 10 Set the class average to the total divided by ten 11 Print the total of the grades for all students in the class 12 Print the class average Fig 4 7 Adding the Interface 1 Fig 4 8 GradeBook h 2 Def in it io n of c lass GradeBook that determines a c lass average 3 Member funct ions are def ined in GradeBook cpp 4 inc lude string program uses C standard string class 5 us ing std string 6 7 GradeBook class definition 8 c lass GradeBook 9 10 public 11 12 13 GradeBook string constructor initializes course name Function determineClassAverage vo id setCourseName string function to set the course name implements the class average algorithm string getCourseName function to retrieve the course name described by the pseudocode 14 vo id displayMessage display a welcome message 15 vo id determineClassAverage averages grades entered by the user 16 pr iva te 17 string courseName course name for this GradeBook 18 end class GradeBook Adding the Functionality Part 1 31 funct ion to ret r ie ve the course name 32 string GradeBook getCourseName 33 34 return courseName 35 end funct ion getCourseName 36 37 disp lay a welcome message to the GradeBook user 38 void GradeBook displayMessage 39 40 cout Welcome to the grade book for n getCourseName n endl 41 42 end funct ion disp layMessage 43 44 Function determineClassAverage implements the class average algorithm described by the pseudocode determine c lass average based on 10 grades entered by user 45 void GradeBook determineClassAverage Declare total variable total 46 47 int total 48 int gradeCounter 49 int grade 50 int average 51 sum of grades entered by user number of the grade to be entered nex t grade va lue entered by user average of grades Declare counter variable gradeCounter Adding the Functionality Part 2 52 initialization phase total 0 initialize total 53 54 Initilize total variable total to 0 gradeCounter 1 initialize loop counter 55 56 processing phase 57 while gradeCounter 10 loop 10 times 58 Initialize counter variable gradeCounter to 1 Continue looping as long as gradeCounter s value is less than or equal to 10 Add the current grade to total 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 69 70 Increment counter by 1 which causes gradeCounter to exceed 10 eventually Perform the averaging calculation and assign its display total and average of grades cout nTotal of all 10 grades is total endl result to the variable cout Class average is average endl average 71 end function determineClassAverage Main Function 1 Fig 4 10 f ig04 10 cpp 2 Create GradeBook objec t 3 inc lude GradeBook h include definition of class GradeBook and invoke it s determ ineC las sAverage func t io n 4 5 in t 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 re turn0 indicate successful termination 14 end main Welcome to the grade book for CS101 C Programming Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter grade 67 grade 78 grade 89 grade 67 grade 87 grade 98 grade 93 grade 85 grade 82 grade 100 Total of all 10 grades is 846 Class average is 84 Important 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 75 Formulating Algorithms Sentinel Controlled Repetition Problem statement Develop 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 value Example of Using a Sentinel Value cout Enter a number 0 to quite endl cin myNumber while myNumber 0 Choosing a sentinel value that is also a legitimate data value is a logic error Formulating Algorithms 1 Top down stepwise refinement Development technique for well structured programs Top step Single statement conveying overall function of the program Example Determine the class average for the quiz First refinement Multiple statements using only the sequence structure Example Initialize variables Input sum and count the quiz grades Calculate and print the total of all student grades and the class average General Idea of Refinement Each refinement as well as the top itself is a complete specification of the algorithm only the level of detail varies Last step should be code level detail 3 normal program phases an initialization phase that initializes the program variables a processing phase that inputs data values and adjusts program variables such as counters and totals accordingly a termination phase that calculates and outputs the
View Full Document