DOC PREVIEW
UW CSE 142 - Iteration

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 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 9 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 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

H1-1H1-1CSE 142Computer Programming IIteration© 2000 UW CSEH1-2OverviewConcepts this lectureIteration - repetitive executionLoops and nested loopswhile statementsfor statementsH1-3Chapter 5Read Sections 5.1-5.6, 5.105.1 Introduction5.2-5.3 While statement5.4 For statement5.5-5.6 Loop design5.7 Nested Loops5.11 Common errorsH1-4An Old Friend: Fahrenheit to Celsius#include <stdio.h>int main(void){double fahrenheit, celsius;printf("Enter a Fahrenheit temperature: ");scanf("%lf", &fahrenheit);celsius = (fahrenheit - 32.0) * 5.0 / 9.0;printf("That equals %f degrees Celsius.", celsius);return 0;}H1-5What’s “Wrong” with Fahrenheit/Celsius Program?User has to rerun the program for every new temperatureWouldn’t it be nice if the program could process repeated requests?Program ends immediately if user types a bad inputWouldn’t it be nice the program politely asked the user again (and again, etc. if necessary)?H1-6One More Type of Control FlowSometimes we want to repeat a block of code. This is called a loop.H1-2H1-7LoopsA “loop” is a repeated (“iterated”) sequence of statementsLike conditionals, loops (iteration) give us a huge increase in the power of our programsAlert: loops are harder to master than if statementsEven experienced programmers often make subtle errors when writing loopsH1-8Motivating LoopsProblem: add 4 numbers entered at the keyboard.int sum;int x1, x2, x3, x4;printf("Enter 4 numbers: ");scanf("%d%d%d%d", &x1, &x2, &x3, &x4);sum = x1 + x2 + x3 + x4;This works perfectly! But... what if we had 14 numbers? or 40? or 4000?H1-9Finding Repeated CodeThe key to using loops to solve a problem is to discover steps that can be repeatedOur first algorithm for adding four numbers had no repeated statements at allBut it does have some repetition buried in it. Let’s rework the algorithm to make the repetition more explicitH1-10Add 4 Numbers, Repetitivelyint sum, x;sum = 0;printf("Enter 4 numbers: ");scanf("%d", &x);sum = sum + x;scanf("%d", &x);sum = sum + x;scanf("%d", &x);sum = sum + x;scanf("%d", &x);sum = sum + x;H1-11Loop to Add 4 Numbersint sum, x;sum = 0;printf("Enter 4 numbers:");scanf("%d", &x);sum = sum + x;scanf("%d", &x);sum = sum + x;scanf("%d", &x);sum = sum + x;scanf("%d", &x);sum = sum + x;int sum, x;int count;sum = 0;printf("Enter 4 numbers:");count = 1;while (count <= 4) {scanf("%d", &x);sum = sum + x;count = count + 1;}H1-12while ( condition ) {statement1;statement2;...}while Statement SyntaxLoop body:Any statement, or a compound statementLoop conditionH1-3H1-13More General Loop to Add Numbersint sum, x, count; int number_inputs; /* Number of inputs */sum = 0;printf("How many numbers? ");scanf("%d", &number_inputs);printf("Enter %d numbers: ", number_inputs);count = 1;while ( count <= number_inputs ) {scanf("%d", &x);sum = sum + x;count = count + 1;}H1-14Compute 7!What is 1 * 2 * 3 * 4 * 5 * 6 * 7? (“seven factorial”)x = 1 * 2 * 3 * 4 * 5 * 6 * 7;printf ( "%d", x ) ;H1-15Compute 7!What is 1 * 2 * 3 * 4 * 5 * 6 * 7? (“seven factorial”)x = 1 * 2 * 3 * 4 * 5 * 6 * 7;printf ( "%d", x ) ;Bite size pieces: More Regular: As a loop:x = 1; x = 1; i = 2; x = 1;x = x * 2; x = x * i; i = i + 1; i = 2;x = x * 3; x = x * i; i = i + 1; while ( i <= 7 ) {x = x * 4; x = x * i; i = i + 1; x = x * i;x = x * 5; x = x * i; i = i + 1; i = i + 1;x = x * 6; x = x * i; i = i + 1; }x = x * 7; x = x * i; i = i + 1;H1-16i <= 7 ? x = x * i ;i = i + 1 ;yesnowhile Loop Control Flowx = 1 ;i = 2 ;H1-17/* What is 1 * 2 * 3 * ...*7 */x = 1 ; /* A */i = 2 ; /* B */while ( i <= 7 ) { /* C */x = x * i ; /* D */i = i + 1 ; /* E */} /* F */printf ( "%d", x ) ; /* G */ Tracing the Loopline i x i≤7?A? 1B2 1C2 1TD2 2E3 2C3 2T......................C 6 120 TD 6 720E 7 720C 7 720 TD 7 5040E 8 5040C 8 5040 FG (Print 5040)H1-18Double Your Money/* Suppose your $1,000 is earning interest at 5% per year. How many years until you double your money? */my_money = 1000.0;n = 0;while ( my_money < 2000.0 ) {my_money = my_money *1.05;n = n + 1;}printf( "My money will double in %d years.", n);H1-4H1-19printf ( "Enter values to average, end with -1.0 \n") ;sum = 0.0 ;count = 0 ; sentinelscanf ( "%lf", &next ) ;while ( next != -1.0 ) {sum = sum + next ;count = count + 1;scanf ( "%lf", &next ) ;}if (count > 0)printf( "The average is %f. \n", sum / (double) count );Average InputsH1-20Printing a 2-D FigureHow would you print the following diagram? ∗∗∗∗∗ ∗∗∗∗∗ ∗∗∗∗∗ repeat 3 timesprint a row of 5 starsrepeat 5 timesprint ∗It seems as if a loop within a loop is needed.H1-21#define ROWS 3#define COLS 5…row = 1;while ( row <= ROWS ) {/* print a row of 5 *’s */…row = row + 1;}Nested LoopH1-22row = 1;while ( row <= ROWS ) {/* print a row of 5 *’s */col = 1;while (col <= COLS) {printf("*");col = col + 1;}printf( "\n" );row = row + 1;}inner loop: print one rowouter loop: print 3 rowsNested LoopH1-23Tracerow:col:output:1123452346123456 123456***************row = 1; while ( row <= ROWS ) {/* print a row of 5 *’s */col = 1;while (col <= COLS) {printf("*");col = col + 1;}printf( "\n" );row = row + 1;}H1-24Print a Multiplication Table12311 2 322 4 633 6 944 8 121231 1 * 1 1 * 2 1 * 32 2 * 1 2 * 2 2 * 33 3 * 1 3 * 2 3 * 34 4 * 1 4 * 2 4 * 3H1-5H1-251 2 324636948121231234Print Row 2col = 1;while (col <= 3) {printf("%4d", 2 * col );col=col+ 1;}printf("\n");row numberH1-26row = 1;while (row <= 4) {col = 1;while (col <= 3) {printf("%4d", row * col );col=col+ 1;}printf("\n");row = row + 1;}Nested LoopsPrint 4 rowsPrint one rowH1-27rowcol 11print 12print 23print 3print \n21print 22print 43print 6print \nLoop Tracerowcol31print 32print 63print 9print \n41print 42print 83print 12print \nH1-28Notes About Loop ConditionsThey offer all the same possibilities as conditions in if-statementsCan use &&, ||, !Condition is reevaluated each time through the loopA common loop condition: checking the number of times through the loopH1-29Counting LoopsA common loop condition: checking the number of times through the loopRequires keeping a "counter"This pattern occurs so often there is a separate statement type based on it: the for-statementH1-30A for Loop/* What is 1 * 2 * 3 * ... * n ? */x = 1 ; i = 2 ;while ( i <= n ) {x = x * i ;i = i+1;}printf ( "%d", x ) ;x = 1 ;for ( i = 2 ; i <= n ; i = i+1 ) {x = x * i ;} printf ( "%d", x) ;H1-6H1-31for Statement Syntaxfor ( initialization;condition; update expression)


View Full Document

UW CSE 142 - Iteration

Download Iteration
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 Iteration 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 Iteration 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?