Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer...
Transcript of Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer...
Lecture 4: Calculating by Iterating
The while Repetition StatementRepetition 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
Counter-Controlled RepetitionProblemWrite a power algorithm (ex. 2x, where x is a user defined number)Formulating algorithm
PseudocodeSet product to 1Type in exponentSet counter to 1While counter is less than or equal to exponent product = 2 * product
add one to the counterPrint out product
1 Product 20
2*product product 21
x times
2*product product 22
2*product product 23
2*product product 24
2*product product 25
Counter-Controlled RepetitionFormulating algorithm
Flowchart
begin
product = 1;counter = 1;
Input exponent
counter< exponentproduct = 2*product;counter = counter+1;
true
false
Print product
end
C code
Counter-Controlled RepetitionPseudocode
Set product to 1Type in exponentSet counter to 1While counter is less than or
equal to exponent product = 2 * product
add one to the counterPrint out product
Counter to control while loop
/* 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; }
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?
/* 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 */
C code - using one less variable
Counter-Controlled Repetition
Loop repeated until counter reaches a certain valueDefinite repetition: number of repetitions is known
Counter-Controlled Repetition
Write a program that completes the following:Read in an integer from the user.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.)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
1 1 2 3 5 8 13 21 34 ...
F1 = 1
F2 = 1
Fn = Fn-1 + Fn-2
Fibonacci Sequence
Fibonacci Sequencebegin
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.
Fibonacci Sequencebegin
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.