Counter-controlled repetition requires
- Name of control variable/loop counter
- Initial value of control variable
- Condition to test for final value of control variable
- Increment/decrement operator to modify control variable when looping
General format when using for loops is:
for ( initialization;
LoopContinuationTest; increment )
statement
note that there is no semicolon after the increment statement
For example:
for( int counter = 1; counter <=
10; counter++ )
cout << counter <<
endl;
the above statement prints integers from one to ten
for
loops can usually be rewritten as while
loops
initialization;
while ( loopContinuationTest){
statement
increment;}
For
multiple variables, use comma-separated lists
for (int i = 0, j = 0; j + i <= 10; j++, i++)
cout << j + i << endl;
General format when using for loop is:
Consider a program
to calculate compound interest:
A
person invests $1000.00 in a savings account yielding 5 percent interest.
Assuming that all interest is left on deposit in the account, calculate and
print the amount of money in the account at the end of each year for 10 years.
Use the following formula for determining these amounts:
a
= p(1+r)
where p is the original amount invested (i.e.,
the principal),
r is the annual interest rate,
n is the number of years and
a is the amount on deposit at the end of
the nth year
While Repetition Structure:
In this repetition
structure action is repeated while some condition remains true.
Pseudo-code
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, for example;
int product = 2;
while ( product <= 1000 )
product = product * 2;
A flowchart for while structure is:
The code example of while loop is:
Formulating Algorithms (Counter-Controlled Repetition):
Counter-controlled
repetition is a ‘Definite Repetition’, each loop is repeated until the counter reaches a certain value, the number
of repetitions is known;
for example;
A class of ten students took a quiz. The
grades (integers in the range 0 to 100) for this quiz are available to you.
Determine the class average on the quiz.
Pseudocode example:
Set total to zero
Set grade counter to one
While grade counter is less than or equal to ten
Input the next grade
Add the grade into the total
Add one to the grade counter
Input the next grade
Add the grade into the total
Add one to the grade counter
Set the class average to the total divided by ten
Print the class average
Print the class average
C++ code for this example is:
Formulating Algorithms (Sentinel-Controlled Repetition):
Sentinel
controlled repetition is an ‘Indefinite Repetition’, Suppose
problem becomes:
Develop
a class-averaging program that will process an arbitrary number of grades each
time the program is run
In this case:
- Unknown number of students
- How will program know when to end?
- Indicates “end of data entry”
- Loop ends when sentinel value is input
- Sentinel chosen so it cannot be confused with regular input
- -1 in this case
Nested Control Structures:
Problem
statement:
A
college has a list of test results (1 = pass, 2 = fail) for 10 students. Write a program that analyzes the
results. If more than 8 students pass,
print "Raise Tuition".
Notice
that:
Program
processes 10 results ie Fixed
number, use counter-controlled loop
Two
counters can be used, one
counts number that passed and another
counts number that fail
Each test result is 1 or 2, so if
not 1, assume 2
Top
level outline:
Analyze exam results and decide if tuition should be raised
First
refinement
Initialize variables
Input the ten quiz grades and
count passes and failures
Print a summary of the exam
results and decide if tuition should be raised
Further Refinement:
Initialize variables
to
Initialize passes to zero
Initialize failures to zero
Initialize student counter to one
Input the ten quiz grades and count passes and failures
to
While student counter is less than or equal to ten
Input the next exam result
Input the next exam result
If the student passed
Add one to passes
Else
Add one to failures
Else
Add one to failures
Add one to student counter
Print a summary of the exam results and decide if tuition should be raised
to
Print the number of passes
Print the number of failures
If more than eight students passed
Print “Raise tuition”
Print “Raise tuition”
Post a Comment