H2-1 University of Washington Computer Programming I Lecture 10: Loop Development and Program Schemas © 2000 UW CSE.

Slides:



Advertisements
Similar presentations
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Kernighan/Ritchie: Kelley/Pohl:
CS1010 Programming Methodology
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Tips for fishtank programming living with the lab © 2013 David Hall.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
By the end of this session you should be able to...
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
Algorithms and Pseudocode
CISC105 – General Computer Science Class 4 – 06/14/2006.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Lecture S1: Sample Lecture.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
ECE Application Programming
Lesson #5 Repetition and Loops.
CS1010 Programming Methodology
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Lesson #5 Repetition and Loops.
Lecture 13 & 14.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
The while Looping Structure
Lesson #5 Repetition and Loops.
The while Looping Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Week 6 CPS125.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Lesson #5 Repetition and Loops.
Assignment Operators Topics Increment and Decrement Operators
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ICS103 Programming in C Lecture 12: Arrays I
Arrays, Part 1 of 2 Topics Definition of a Data Structure
The while Looping Structure
ICS103: Programming in C 6: Pointers and Modular Programming
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

H2-1 University of Washington Computer Programming I Lecture 10: Loop Development and Program Schemas © 2000 UW CSE

H2-2 Goals for Loop Development Getting from problem statement to working code Systematic loop design and development Recognizing and reusing code patterns

H2-3 Example: Rainfall Data General task: Read daily rainfall amounts and print some interesting information about them. Input data: Zero or more numbers giving daily rainfall followed by a negative number (sentinel).

H2-4 Example: Rainfall Data General task: Read daily rainfall amounts and print some interesting information about them. Input data: Zero or more numbers giving daily rainfall followed by a negative number (sentinel). Example input data: Empty input sequence: Given this raw data, what sort of information might we want to print?

H2-5 Rainfall Analysis Some possibilities: Just print the data for each day Compute and print the answer to one of these questions How many days worth of data are there? How much rain fell on the day with the most rain? On how many days was there no rainfall? What was the average rainfall over the period? What was the median rainfall (half of the days have more, half less)? On how many days was the rainfall above average? What’s similar about these? Different?

H2-6 Rainfall Analysis Some possibilities: Just print the data for each day Compute and print the answer a question like: How many days worth of data are there? How much rain fell on the day with the most rain? On how many days was there no rainfall? What was the average rainfall over the period? What’s similar about these? Different?

H2-7 #include int main (void) { double rain; /* current rainfall from input */ /* read rainfall amounts and print until sentinel (<0) */ scanf(“%lf”, &rain); while (rain >= 0.0) { printf(“%f ”, rain); scanf(“%lf”, &rain); } return 0; } Example: Print Rainfall Data

H2-8 #include int main (void) { double rain; /* current rainfall from input */ int ndays; /* number of days of input */ /* read rainfall amounts and count number of days */ ndays = 0; scanf(“%lf”, &rain); while (rain >= 0.0) { ndays = ndays + 1; scanf(“%lf”, &rain); } printf(“# of days input = %d.\n”, ndays); return 0; } Example: # Days in Input

H2-9 #include int main (void) { double rain; /* read rainfall amounts */ scanf(“%lf”, &rain); while (rain >= 0.0) { printf(“%f ”, rain); scanf(“%lf”, &rain); } return 0; } Is There a Pattern Here? #include int main (void) { double rain; int ndays; /* read rainfall amounts */ ndays = 0; scanf(“%lf”, &rain); while (rain >= 0.0) { ndays = ndays + 1; scanf(“%lf”, &rain); } printf(“# of days %d.\n”, ndays); return 0; }

H2-10 A program schema is a pattern of code that solves a general problem Also called a “design pattern” Learn patterns through experience, observation. If you encounter a similar problem, try to reuse the pattern Program Schema

H2-11 Given a problem to solve, look for a familiar pattern Work the problem by hand to gain insight into possible solutions. Ask yourself “what am I doing?” Check your code by hand-tracing on simple test data. Tips For Problem Solving

H2-12 #include int main (void) { double variable; /* current input */ declarations; initial; scanf(“%lf”, &variable); while (variable is not sentinel) { process; scanf(“%lf”, &variable); } final; return 0; } Schema: “Read until Sentinel”

H2-13 Schema Placeholders (1) In this schema, variable, declarations, sentinel, initial, process, and final are placeholders. variable holds the current data from input. It should be replaced each place it occurs with the same appropriately named variable. sentinel is the value that signals end of input. declarations are any additional variables needed.

H2-14 Schema Placeholders (2) initial is any statements needed to initialize variables before any processing is done. process is the “processing step” - work done for each input value. final is any necessary operations needed after all input has been processed.

H2-15 #include int main (void) { double rain; /* current rainfall */ declarations; initial; scanf(“%lf”, &rain); while (rain >= 0.0) { process; scanf(“%lf”, &rain); } final; return 0; } Schema for Rainfall

H2-16 Loop Development Tips Often helps to start with What has to be done to process one more input value? What information is needed for final? Declare variables as you discover you need them. When you create a variable, write a comment describing what’s in it! Often easiest to write initial last initial is “what’s needed so the loop works the 1st time”

H2-17 Loop Development Examples We will fill in the “Read Until Sentinel” program schema to solve a couple of problems To save room on the slide, we will leave out this boilerplate: #include int main(void) { Loop Schema return 0; }

H2-18 double rain; /* current rainfall */ decls: initial: scanf(“%lf”, &rain); while (rain >= 0.0) { process: scanf(“%lf”, &rain); } final: Print Rainfall Data printf(“%f ”, rain);

H2-19 double rain; /* current rainfall */ decls: initial: scanf(“%lf”, &rain); while (rain >= 0.0) { process: scanf(“%lf”, &rain); } final: Print # Days of No Rain int nDryDays; /* days without rain */ nDryDays = 0; if (rain == 0.0) nDryDays = nDryDays + 1; printf (“Dry days: %d\n”,nDryDays);

H2-20 double rain; /* current rainfall */ decls: initial: scanf(“%lf”, &rain); while (rain >= 0.0) { process: scanf(“%lf”, &rain); } final: Print Largest Daily Rainfall double maxRain; /* Largest amount seen so far */ maxRain = 0.0; if (rain > maxRain) maxRain = rain; printf (“Largest rainfall: %f\n”, maxRain);

H2-21 double rain; /* current rainfall */ decls: initial: scanf(“%lf”, &rain); while (rain >= 0.0) { process: scanf(“%lf”, &rain); } final: Print Average Daily Rainfall double totalRain; /* rain amount */ int nRain; /* days */ totalRain = 0; nRain = 0; totalRain = totalRain + rain; nRain = nRain + 1; printf (“average rainfall is %f\n”, totalRain / nRain);

H2-22 double rain; /* current rainfall */ decls: initial: scanf(“%lf”, &rain); while (rain >= 0.0) { process: scanf(“%lf”, &rain); } final: Print Average Daily Rainfall (2) double totalRain; /* rain amount */ int nRain; /* days */ totalRain = 0; nRain = 0; totalRain = totalRain + rain; nRain = nRain + 1; if (nRain > 0) printf (“avg: %f\n”, totalRain / nRain); else printf(“No data”);

H2-23 Summary Loop design is not always a top-to-bottom process Sometimes “process”/ “init”/ “final” is useful, with “decls” as needed A program schema is a pattern of code that solves a general problem We looked at one, “Read Until Sentinel.” Look for other general patterns as you get more experience