Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.

Similar presentations


Presentation on theme: "Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition."— Presentation transcript:

1 Lecture 4: Calculating by Iterating

2 The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition remains true. Pseudocode (go shopping) While there are more items on my shopping list Purchase next item and cross it off my list while loop repeated until condition becomes false Can loop a known or unknown number of times for repeating calculations or function calls (printf). Flowchart condition The body of the while condition while body

3 Counter-Controlled Repetition Problem Write a power algorithm (ex. 2 x, where x is a user defined number) Formulating algorithm Pseudocode Set product to 1 Type in exponent Set counter to 1 While counter is less than or equal to exponent product = 2 * product add one to the counter Print out product 1 Product 2 0 2*product product 2 1 x times 2*product product 2 2 2*product product 2 3 2*product product 2 4 2*product product 2 5

4 Counter-Controlled Repetition Formulating algorithm Flowchart begin product = 1; counter = 1; Input exponent counter< exponent product = 2*product; counter = counter+1; true false Print product end

5 C code Counter-Controlled Repetition Pseudocode Set product to 1 Type in exponent Set counter to 1 While counter is less than or equal to exponent product = 2 * product add one to the counter Print out product Counter to control while loop /* Computing 2^x, where x is a user defined number */ #include /* function main begins program execution */ int main( void ) { int product; /* product of 2^x */ int counter; /* number of 2's multipled */ int exponent; /* user defined the power of 2 */ product = 1; counter = 1; printf( "Enter the exponent:\n" ); /* prompt */ scanf( "%d", &exponent ); /* read an integer */ while ( counter <= exponent ) { product = 2 * product; counter = counter + 1; } printf("2^%d equals to %d\n", exponent, product); return 0; /* indicate that program ended successfully */ } /* end function main */ Initialize counter to 1 while loop iterates as long as counter <= exponent Increment the counter Do we really need the variable power ? Can we save one variable?

6 /* Computing 2^x, where x is a user defined number */ #include /* function main begins program execution */ int main( void ) { int product; /* product of 2^x */ int exponent; /* user defined the power of 2 */ product = 1; printf( "Enter the exponent:\n" ); /* prompt */ scanf( "%d", &exponent ); /* read an integer */ printf("2^%d equals to ", exponent ); while ( exponent > 0 ) { product = 2 * product; exponent = exponent - 1; } printf("%d\n", product); return 0; /* indicate that program ended successfully */ } /* end function main */ C code - using one less variable Counter-Controlled Repetition

7 Loop repeated until counter reaches a certain value Definite repetition: number of repetitions is known Counter-Controlled Repetition

8 Write a program that completes the following: 1. Read in an integer from the user. 2. Print out that number of numbers from the Fibonacci sequence. This would require you to calculate the values of the sequence, not hard code them in your program. (Hint: You should calculate the Fibonacci sequence using two variables.) 3. Use a while loop counter to print the values (you should need only 1 print statement in your loop). (Hint: The Fibonacci number looks like this: 1 1 2 3 5 8 13 21 34... ) Submit your program in the dropbox called Fibonacci Sequence and complete Program Quiz 3 using your program. In-Class Programming Exercise

9 1 1 2 3 5 8 13 21 34... F 1 = 1 F 2 = 1 F n = F n-1 + F n-2 Fibonacci Sequence

10 begin Initializing first two Fibonacci numbers; Enter # of Fibonacci numbers, num counter<=num Compute next Fibonacci number; Print the Fibonacci number; counter = counter +1; true false end Print first two Fibonacci numbers counter = 3; Program assumes that input of 2 or higher is used.

11 Fibonacci Sequence begin last = 1; current = 1; Enter # of Fibonacci numbers, num counter<=num next = last + current; Print the Fibonacci number next; last = current; current = next; counter = counter +1; true false end Print first two Fibonacci numbers counter = 3; Program assumes that input of 2 or higher is used.


Download ppt "Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition."

Similar presentations


Ads by Google