Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 108 Computing Fundamental Notes for Thursday, October 5, 2017

Similar presentations


Presentation on theme: "CS 108 Computing Fundamental Notes for Thursday, October 5, 2017"— Presentation transcript:

1 CS 108 Computing Fundamental Notes for Thursday, October 5, 2017

2 GHP #10 Let’s review the requirements

3 Selection Practice The table below shows the normal boiling points of several substances. Develop an algorithm that prompts the user for the observed boiling point of a substance in degrees C and then identifies the substance if the observed boiling point is within 5% of  the expected boiling point. If the input data is more than 5% higher or lower than any of the boiling points in the table, the program  should output  the message  “Substance Unknown.” The normal boiling points of various substances (degrees C): Water 100 Mercury 357 Copper Silver Gold Develop an algorithm!!

4 A Poor Algorithm Ask user to enter observed boiling temperature in degrees C Record the user's input Determine the substance present Terminate Step 3 is magic… no one could accomplish this task on paper nor write a program to accomplish the task. Algorithms cannot have magical steps… a number of you have tried to submit your GHP #10 programs with similar magical steps.

5 Let’s Demystify Step 3 (1)
Chalk talk… start with what you know, build on it, and then try to generalize (if possible) Let’s start with water

6 Let’s Demystify Step 3 (2)
Ask user to enter observed boiling temperature in degrees C Record the user's input Determine the substance present 3a. Test to determine if the observed boiling point is within 5% of 100 degrees C; if true, communicate that the substance is water formula: observed_bp >= 100 – (100 *.05) and observed_bp <= (100 * .05) 3a1. Go to step 4 4. Terminate Can we generalize?

7 Let’s Demystify Step 3 (3)
Ask user to enter observed boiling temperature in degrees C Record the user's input Determine the substance present 3a. Test to determine if the observed boiling point is within 5% of 100 degrees C; if true, communicate that the substance is water formula: observed_bp >= 100 – (100 *.05) and observed_bp <= (100 * .05) 3a1. Go to step 4 4. Terminate Can we generalize? observed_bp >= substance_bp – (substance_bp *.05) and observed_bp <= substance_bp + (substance_bp *.05)

8 Let’s Demystify Step 3 (4)
Let’s expand the algorithm to cover all the substances in the table Let’s code the first few easy algorithm steps (one step at a time)

9 Let’s Demystify Step 3 (5)
Let’s continue coding essentially one step at a time

10 GHP #10 The same approach we just used on the previous several slides is exactly the same approach you should use to tackle the homework

11 Iteration (Looping) Iteration or looping is the ability to execute the same set of instructions repeatedly. Almost all loops have a starting point (or a starting situation) and a conditional component (used to decide if another pass is required or to stop). In C there are three kinds of looping statements: while statements do … while statements for statements

12 While Statement A while statement checks the loop condition before the execution of the loop body. The loop body is executed as long as the loop condition is TRUE. A while statement is not always executed… testing first means loop may never be entered

13 While Statement Syntax : while (loop condition) {
Loop Body Statement(s); } A snippet of code to demonstrate a while statement total = 0; // total of the integers between 0-5 counter = 0; // count or "track" the integers between 0-5 while (counter < 5) total = total + counter; counter = counter +1; // counter++ works, too

14 While Statement Let’s develop a program that offers the user the opportunity to pick the range of positive integers he/she wants to total (in other words, the user picks the loop’s starting point (the lower bound) and ending point (the upper bound)… the program totals all the integers between those boundaries (inclusive)). How should we proceed?

15 While Statement What are our inputs and outputs?
Inputs: 1) a lower bound and 2) an upper bound Outputs: The total of the integers between to lower and upper bound (inclusive)

16 While Statement Greet user Ask the user to enter lower bound integer
Read/record the user's input for lower bound integer Ask the user to enter upper bound integer greater than the lower bound Read/record user’s input for upper bound integer Set an "index" to the value of the lower bound Test to see if the index is less than or equal to the value of the upper bound If true Add index value to the running total Add 1 to index value Go to step 7 If false Go to step 8 Display the lower bound Display the upper bound Display the total Terminate

17 While Statement After you have the algorithm, use paper and a pen/pencil to test the algorithm Chalk talk

18 While Statement Let’s code the first few easy algorithm steps
Let’s code algorithm step 6

19 While Statement Let’s work on algorithm steps 7 through 7a2 (notice that I start with an if statement, not a while statement) Now let's handle algorithm step 7a3 (makes us go back to a previous step… this is the essence of looping… replace if with while) Now let's finish up


Download ppt "CS 108 Computing Fundamental Notes for Thursday, October 5, 2017"

Similar presentations


Ads by Google