Professional Documents
Culture Documents
0
DESIGN A SOLUTION
2
Control Structure Repetition
Repetition structure:
• also called looping control structure
4
Control Structure Repetition
Counter
Counter Controlled
Repetition
Sentinel Controlled
(when we don’t know exactly
know how many times loop
body will be executed
Repetition structure:
• also called looping control structure
5
Repetition Counter-Controlled
# a counter is a numeric variable used for
Concept counting something.
1 & 2. Name & Initialize loop counter control variable, before the loop begins
3. Condition, test the loop counter control variable in the while condition; if the
condition is true, the loop begins executing
4. Update the value of the loop counter control variable (increment or decrement)
8
Repetition Counter-Controlled
Flowchart:
Counter loop
Start variable is named &
initialized
9
Repetition
Example 1
Counter Controlled
Problem statement
Create a program that will print ‟Hello” four times.
IPO analysis
Input -
Process -
Output ‟Hello” is printed 4 times
10
Repetition
Example 1
Counter Controlled
Problem statement
Create a program that will print ‟Hello” four times.
Pseudocode: Counter loop
variable is named &
initialized
Start Counter loop
set count = 1 1&2 variable is
tested
repeat while (count < 5) 3
Print ‟Hello”
count = count + 1 4 Counter loop
variable is
end repeat updated
Stop
11
Repetition
Example 1
Counter Controlled
Condition to test
counter loop
variable
true
3 count < 5
Print Update
false ‟Hello” counter loop
variable
Stop count = 4
count + 1
12
Repetition
Example 1
Counter Controlled
Counter Table:
set count repeat while
Print “Hello” count = count + 1
=1 (count < 5)
1 true “Hello” 1+1=2
2 true “Hello” 2+1=3
3 true “Hello” 3+1=4
4 true “Hello” 4+1=5
5 false end repeat
13
Repetition
Example 2
Counter Controlled
Problem statement
Create a program that calculates and displays the pay of 5 employees given
hours worked and hourly pay rate.
14
Repetition
Example 2
Counter Controlled
Pseudocode:
Start
1&2
set x to 1
repeat while (x ≤ 5) 3
Read hours worked, pay rate
15
Repetition
Example 2
Counter Controlled
Start name &
Flowchart: initialize
x=1 1&2
test
true
3 x≤5
Read hours
false worked, pay rate
4 x=x+1 16
Repetition
Example 2
Counter Controlled
Counter Table:
17
Repetition
Example 3
Counter Controlled
Problem statement
Create a program that calculates and displays the pay for a number of
employees given hours worked and hourly pay rate.
18
Repetition
Example 3
Counter Controlled
Pseudocode:
Start
set employee to 1 1&2
Read n
repeat while (employee <= n) 3
Read hours worked, pay rate
Flowchart:
Read n
condition
employee ≤ true
3
n
Read hours
false worked, pay rate
4 employee = employee + 1 20
Repetition
Example 3
Counter Controlled
Counter Table:
Repeat
employee while Read hours Calculate Print employee =
Read n
=1 (employee worked, pay rate pay pay employee + 1
≤ n)
1 5 true 40.00 , 20.00 800 1+1=2
2 true 50.00 , 22.50 1 125 2+1=3
3 true 55.00 , 25.00 1 375 3+1=4
4 true 60.00 , 23.00 1 380 4+1=5
5 true 66.00 , 25.00 1 650 5+1=6
6 false end repeat
21
1. Display “Great” 3 times and display “Bye” when you quit
the program.
22
Solution
1. Display “Great” 3 times and display “Bye” when you quit the
program
Pseudocode Start
Start
set count = 1
set count = 1
repeat while (count < 4)
Print “Great” true
count <
count = count + 1
4
end repeat
Print
Print “Bye” false ‟Great”
Stop 23
Repetition Counter-Controlled (Accumulation)
Concept
Running total:
• also known as the total of a series of numbers
25
Repetition Counter-Controlled (Accumulation)
Concept
26
Repetition Counter-Controlled (Accumulation)
General logic for calculating a running total:
Initialize counter,
1
accumulator
true
condition
Update the
Read number accumulator
false add number to
2
Stop accumulator
add 1 to counter 27
Repetition
Accumulator
Example 1
Problem statement
Calculate the sum of three integers.
28
Repetition
Accumulator
Example 1
Pseudocode:
name & initialize
Start count, sum
set count to 1 condition
1&2
set sum to 0
repeat while count < 4 3
Read num accumulate
sum
sum = sum + num 5
Note:
count = count + 1 4 • sum is the variable used
to accumulate values of
end repeat num
update
Print sum count
Stop
29
Repetition
Accumulator
Example 1
Flowchart: Start name &
initialize
count = 1 1&2
sum = 0
condition
true
3 count < 4
false Read
num
Print accumulate
sum
sum = sum + num 4
count = count + 1 5
Stop
update
30
Repetition
Accumulator
Example 1
Counter Table:
Repeat
while Read sum = sum + count = count Print
count =1 sum =0
(count < num num +1 sum
4)
1 0 true 90 0 + 90 = 90 1+1=2
2 90 true 70 90 + 70 = 2+1=3
160
3 160 true 80 160 + 80 = 3+1=4
240
4 240 false end repeat 240
31
Repetition
Accumulator
Example 2
Problem statement
Suppose you are writing a program that calculates a business’s total
sales for a week. The program would read the sales for each day as
input and calculate the total of those numbers.
32
Repetition
Accumulator
Example 2
Input sales
sales1, sales2, sales3,
sales4, sales5, sales6,
sales7
33
Repetition
Accumulator
Example 2
Pseudocode:
initialize counter,
Start accumulator
set i to 1 condition
1&2
set total sales to 0
repeat while (i ≤ 7) 3
Read sales accumulate
total sales = total sales + sales 4
total sales
i=i+1 5
end repeat
update
Print total sales counter
Stop
34
Repetition
Accumulator
Example 2
set
repeat total sales = Print
set i total Read
while total sales + i = i + 1 total
=1 sales = sales
(i ≤ 7) sales sales
0
1 0 true 1000 0 + 1000 = 1000 1+1=2
2 1000 true 1250 1000 + 1250 = 2+1=3
2250
3 2250 true 3000 2250 + 3000 = 3+1=4
5250
4 5250 true 12000 5250 + 12000 = 4+1=5
17250
5 17250 true 10000 17250 + 10000 5+1=6
= 27250
6 27250 true 3250 27250 + 3250 = 6+1=7
30500
7 30500 true 7500 30500 + 7500 = 7+1=8
38000
8 38000 false end repeat 38000
35
Repetition
Accumulator
Example 3
Problem statement
Calculate and display the average of 100 numbers input by the
user.
IPO
Sequence Looping
analysis
num1, num2,
Input num
…num100
Process Calculate the average Determine the average of
of 100 numbers 100 numbers.
36
Repetition
Accumulator
Example 3
Pseudocode: name &initialize
counter, accumulator
Start
set i to 1 1&2 condition
set sum to 0
repeat while count < 101 3
Read num accumulate
sum
sum = sum + num 4
Note:
i=i+1 • sum is the variable used
5
to accumulate values of
end repeat num
update
average= sum/100 counter
Print average
Stop
37
Repetition
Accumulator
Example 3
Start
Flowchart: initialize
set i = 1 1 &2
set sum = 0
condition
count <
true
3
101
false Read
num
accumulate
average = sum/100
sum = sum + num 4
i=i+1 5
Print
average
update
Stop 38
Repetition
Accumulator
Example 3
Counter Control Table:
1 0 true
2 true
3 true
4 true
5 true
101 false XXXX XXXX
39
1. Create a program that calculates and displays the total
bonus for 10 employees based on 20% from their monthly
salary.
2. Create a program that calculates and displays the total
bonus for a number of employees based on 20% from
their monthly salary.
40
Repetition
Sentinel Control
Compare the questions
Input
radius
Process
Calculate area of circles as long as the
radius is positive.
Output
area
Repetition
Sentinel Control
Solution – Question 2
Initial value of sentinel
variable
Start
Condition to test
read radius sentinel variable
while (radius ≥ 0)
area = 3.142 x radius x radius
print area
read radius Update the value of
sentinel variable
repeat while
Stop
1. Create a C++ program that will calculate the salary of
workers in a company. Salary will be calculated based on
hours work and rate per hour. The program will
terminate if user enter hour < 0.