DOC PREVIEW
UW CSE 142 - Loop Development and Program Schemas

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

University of Washington Computer Programming IPowerPoint PresentationSlide 3Slide 4Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Loop Development ExamplesSlide 18Slide 19Slide 20Slide 21Slide 22SummaryH2-1University of WashingtonComputer Programming ILecture 10:Loop Development and Program Schemas© 2000 UW CSEH2-2Goals for Loop DevelopmentGetting from problem statement to working codeSystematic loop design and developmentRecognizing and reusing code patternsH2-3Example: Rainfall DataGeneral 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-4Example: Rainfall DataGeneral 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: 0.2 0.0 0.0 1.5 0.3 0.0 0.1 -1.0Empty input sequence: -1.0Given this raw data, what sort of information might we want to print?H2-6Rainfall AnalysisSome possibilities:Just print the data for each dayCompute 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 <stdio.h>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 DataH2-8#include <stdio.h>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 InputH2-9#include <stdio.h>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 <stdio.h>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-10A program schema is a pattern of code that solves a general problemAlso called a “design pattern”Learn patterns through experience, observation.If you encounter a similar problem, try to reuse the patternProgram SchemaH2-11Given a problem to solve, look for a familiar patternWork 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 SolvingH2-12#include <stdio.h>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-13Schema 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-14Schema 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 <stdio.h>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 RainfallH2-16Loop Development TipsOften helps to start withWhat 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-17Loop Development ExamplesWe will fill in the “Read Until Sentinel” program schema to solve a couple of problemsTo save room on the slide, we will leave out this boilerplate:#include <stdio.h>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 Dataprintf(“%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 Rainint 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 Rainfalldouble 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 Rainfalldouble 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)


View Full Document

UW CSE 142 - Loop Development and Program Schemas

Download Loop Development and Program Schemas
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Loop Development and Program Schemas and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Loop Development and Program Schemas 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?