1 / 10

Lecture 5: Stopping with a Sentinel

Lecture 5: Stopping with a Sentinel. Using a Sentinel. Problem Develop a class-averaging program that will process an arbitrary number of grades each time the program is run. Unknown number of students Can not use counter-controlled repetition! How will the program know to end?

ejulie
Télécharger la présentation

Lecture 5: Stopping with a Sentinel

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 5: Stopping with a Sentinel

  2. Using a Sentinel • Problem Develop a class-averaging program that will process an arbitrary number of grades each time the program is run. • Unknown number of students Can not use counter-controlled repetition! • How will the program know to end? • Use sentinel value • Also called signal value, dummy value, or flag value • Indicates “end of data entry” • Loop ends when user inputs the sentinel value • Sentinel value chosen so it cannot be confused with a regular input (such as -1 in this case)

  3. Formulating Algorithms with Top-Down, Stepwise Refinement • Problem Develop a class-averaging program that will process an arbitrary number of grades each time the program is run. • Formulating algorithm • Begin with a pseudocode representation of the top: Determine the class average for the quiz • Divide top into smaller tasks and list them in order: Initialize variablesInput, sum and count the quiz gradesCalculate and print the class average Initialization Processing Termination

  4. Formulating Algorithms with Top-Down, Stepwise Refinement • Formulating algorithm • Refine the initialization phase fromInitialize variablesto: Initialize total to zero Initialize counter to zero • RefineInput, sum and count the quiz gradesto Input the first grade (possibly the sentinel)While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade (possibly the sentinel) • RefineCalculate and print the class averageto If the counter is not equal to zero Set the average to the total divided by the counter Print the averageelse Print “No grades were entered”

  5. Formulating Algorithms with Top-Down, Stepwise Refinement Pseudocode algorithm that uses sentinel-controlled repetition to solve the class average problem

  6. Tips for Developing Pseudocode Algorithm • Many programs can be divided logically into three phases: • Initialization: initializes the program variables • Processing: inputs data values and adjusts program variables accordingly • Termination: calculates and prints the final results • When to terminate the refinement process? You terminate the top-down, stepwise refinement process when the pseudocode algorithm is specified in sufficient detail for you to be able to convert the pseudocode to C. Implementing the C program is then normally straightforward.

  7. float type indicates variable can be a non-integer C code for the Class Average Problem

  8. while loop repeats until user enters a value of -1 Ensures the user entered at least one grade Converts total to float type Prints result with 2 digits after decimal point C code for the Class Average Problem

  9. Good Programming Practice When performing division by an expression whose value could be zero, explicitly test for this case and handle it appropriately in your program (such as printing an error message) rather than allowing the fatal error to occur. In a sentinel-controlled loop, the prompts requesting data entry should explicitly remind the user what the sentinel value is. Do not compare floating-point values for equality.

  10. In-Class Programming Exercise 1. Create a program that finds the sum of every number entered until '-1' is entered.  Example output: Enter your number: 1 Enter your number: 2 Enter your number: 10 Enter your number: -1 The sum is: 13 2. Create a program that prints out the input number of X's until a negative number is entered. Example output: How many X's? 5 XXXXX How many X's? 2 XX How many X's? -23 Thank you for playing!

More Related