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

Slides:



Advertisements
Similar presentations
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Advertisements

CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
Repetition statements
Lesson #5 Repetition and Loops.
UNIT 5 Lesson 15 Looping.
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CS 1430: Programming in C++ No time to cover HiC.
Repetition Structures Chapter 9
Control Structures II Chapter 3
Repetition (While-Loop) version]
CS161 Introduction to Computer Science
Lesson #5 Repetition and Loops.
CHAPTER 5A Loop Structure
EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2.
Chapter 5: Control Structure
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Lecture 07 More Repetition Richard Gesick.
Algorithm and Ambiguity
Iterations Programming Condition Controlled Loops (WHILE Loop)
Lecture 4B More Repetition Richard Gesick
Writing Functions( ) (Part 5)
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Learning to Program in Python
Control Structure Senior Lecturer
Lesson #5 Repetition and Loops.
Fundamentals of Programming
More Loops.
More Loops.
Repetition Structures
Java Programming Loops
Algorithm Discovery and Design
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
For Loops.
Repetition Structures
Introduction to Problem Solving and Control Statements
3. Decision Structures Rocky K. C. Chang 19 September 2018
Algorithm and Ambiguity
3.1 Iteration Loops For … To … Next 18/01/2019.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Computing Fundamentals
CSC115 Introduction to Computer Programming
M150: Data, Computing and Information
Flowcharts and Pseudo Code
A LESSON IN LOOPING What is a loop?
CPS125 Week
Java Programming Loops
Topics Introduction to Repetition Structures
Chapter 4: Repetition Structures: Looping
Lesson #5 Repetition and Loops.
More Loops Topics Relational Operators Logical Operators for Loops.
Continue with conditional
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Software Development Techniques
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
Iteration – While Loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

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