Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 4: Calculating by Iterating 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 condition The body of the while 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 while body Counter-Controlled Repetition Problem Write a power algorithm (ex. 2x, where x is a user defined number) 0 1 Product 2 Formulating algorithm 2*product product 21 Pseudocode 2*product product 22 2*product product Set product to 1 2*product product Type in exponent 2*product product Set counter to 1 While counter is less than or equal to exponent product = 2 * product add one to the counter Print out product 23 24 25 x times Counter-Controlled Repetition Formulating algorithm Flowchart begin product = 1; counter = 1; Input exponent counter< exponent false Print product end true product = 2*product; counter = counter+1; Counter-Controlled Repetition C code /* Computing 2^x, where x is a user defined number */ #include <stdio.h> /* 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; } Pseudocode Set product to 1 Type in exponent Set counter to 1 While counter is less than or equal to exponent Counter to control while loop product = 2 * product add one to the counter Print out product Initialize counter to 1 while loop iterates as long as counter <= exponent Increment the counter printf("2^%d equals to %d\n", exponent, product); return 0; /* indicate that program ended successfully */ } /* end function main */ Do we really need the variable power? Can we save one variable? Counter-Controlled Repetition C code - using one less variable /* Computing 2^x, where x is a user defined number */ #include <stdio.h> /* 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 */ Counter-Controlled Repetition Loop repeated until counter reaches a certain value Definite repetition: number of repetitions is known In-Class Programming Exercise 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. Fibonacci Sequence 1 1 2 3 5 8 13 21 34 ... F1 = 1 F2 = 1 Fn = Fn-1 + Fn-2 Fibonacci Sequence Program assumes that input of 2 or higher is used. begin Initializing first two Fibonacci numbers; Enter # of Fibonacci numbers, num Print first two Fibonacci numbers counter = 3; counter<=num false end true Compute next Fibonacci number; Print the Fibonacci number; counter = counter +1; Fibonacci Sequence Program assumes that input of 2 or higher is used. begin last = 1; current = 1; Enter # of Fibonacci numbers, num Print first two Fibonacci numbers counter = 3; next = last + current; counter<=num false end true Print the Fibonacci number next; last = current; current = next; counter = counter +1;