CS 108 Computing Fundamental Notes for Thursday, October 5, 2017
GHP #10 Let’s review the requirements
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 1187 Silver 2193 Gold 2660 Develop an algorithm!!
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.
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
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 + (100 * .05) 3a1. Go to step 4 4. Terminate Can we generalize?
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 + (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)
Let’s Demystify Step 3 (4) Let’s expand the algorithm to cover all the substances in the table http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05a1.txt Let’s code the first few easy algorithm steps (one step at a time) http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05a2.txt
Let’s Demystify Step 3 (5) Let’s continue coding essentially one step at a time http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05a3.txt http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05a4.txt http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05a5.txt http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05a6.txt http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05a7.txt http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05a8.txt http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05a9.txt
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
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
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
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
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?
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)
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
While Statement After you have the algorithm, use paper and a pen/pencil to test the algorithm Chalk talk http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05b1.txt
While Statement Let’s code the first few easy algorithm steps http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05b2.txt Let’s code algorithm step 6 http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05b3.txt
While Statement Let’s work on algorithm steps 7 through 7a2 (notice that I start with an if statement, not a while statement) http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05b4.txt 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) http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05b5.txt Now let's finish up http://web.cs.sunyit.edu/~urbanc/cs_108_oct_05b6.txt