You are on page 1of 49

2.

0
DESIGN A SOLUTION

2.1 D) CONTROL STRUCTURE


Learning Outcome
At the end of this topic, students should be able to:

2.1 Design a Solution


d) Apply appropriate control structures
Repetition/Looping
- Counter-controlled, sentinel-controlled

2
Control Structure Repetition

Programmers use the repetition structure, when


they need the computer to repeatedly process one
or more program instructions

Repetition structure:
• also called looping control structure

4
Control Structure Repetition
Counter
Counter Controlled

2 ways to stop loop


(when we know how Accumulator
many times loop body
will be executed ) Both Counter &
Accumulator

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.

Definition : A counter-controlled /Looping condition specifies the


requirement for repeating the instructions.
Indicates when the computer should continue “looping”
through the instructions.

The counter-controlled has 4 important parts:


(1) Name the counter : any names can be given to the loop counter
(2) Initialize the counter :
* initializing means to assign a beginning value to the counter .
Counters are usually initialized to the number 0 or 1; however they can be
initialized to any number; depending on the value required by the algorithm.
(3) Condition to test the final value of loop counter.
(4) Update the counter (increment/decrement) :
*means adding a number to the value stored in the counter. The number can be
either positive or negative, integer or non integer.
7
Repetition Counter-Controlled
Pseudocode
Start
name & initialize counter 1 &2

repeat while (condition) 3


statement_n
update the counter 4
end repeat
Stop

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

name &initialize 1&2


counter
Counter loop
variable is
tested
true
3 condition

statement_n Counter loop


false variable is
updated
counter
Stop increment 4

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

Flowchart: Counter loop


Start variable is named &
initialized

set count = 1 1&2

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.

IPO analysis Sequence Repetition

Input hours worked, pay rate hours worked, pay rate

Process Calculate the pay for an Determine the pay for 5


employee. employees.

Output pay pay

14
Repetition
Example 2
Counter Controlled

Pseudocode:

Start
1&2
set x to 1
repeat while (x ≤ 5) 3
Read hours worked, pay rate

pay = hours worked x pay rate


Print pay
x=x+1 4
end repeat
Stop

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

Stop pay = hours worked


x pay rate

update Print pay

4 x=x+1 16
Repetition
Example 2
Counter Controlled

Counter Table:

repeat pay =hours


Read hours worked, Print
x =1 while worked x pay x=x +1
pay rate pay
(x ≤ 5) rate
1 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

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.

IPO analysis Sequence Repetition

Input hours worked, pay rate hours worked, pay rate, n

Process Calculate pay of an Determine the pay for a number


employee of employees (n)

Output pay pay

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

pay = hours worked x pay rate


Print pay
employee = employee + 1 4
end repeat
Stop
19
Repetition Start initialize Example 3
Counter Controlled employee = 1 1&2

Flowchart:
Read n

condition

employee ≤ true
3
n
Read hours
false worked, pay rate

Stop pay = hours worked


x pay rate

update Print pay

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.

2. Create a program that calculates and displays the bonus


for 10 employees based on 20% from their monthly
salary.

3. Create a program that calculates and displays the bonus


for a number of employees based on 20% from their
monthly salary.

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 Print count =


‟Bye” count + 1

Stop 23
Repetition Counter-Controlled (Accumulation)

Concept

Some problems require you to calculate a subtotal, a


total, or an average. An accumulator is a numeric
variable used for accumulating (adding together)
something.

Running total:
• also known as the total of a series of numbers
25
Repetition Counter-Controlled (Accumulation)

Concept

The loop counter-controlled (accumulation) has 2 important parts :


(1) Name & Initialize the Accumulator :
* initializing means to assign a beginning value to the accumulator
Accumulator are usually initialized to the number 0;
however they can be initialized to any number; depending on the
value required by the algorithm.
(2) Update the accumulators
* means adding a number to the value stored in the accumulator.
The number can be either positive or negative, integer or non
integer. The accumulator is updated by a value that varies.

26
Repetition Counter-Controlled (Accumulation)
General logic for calculating a running total:

Start Counter, accumulator is named &


initialized

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.

IPO analysis Sequence Looping

Input num1, num2, num3 num


Process Calculate the sum of Determine to calculate the sum for
three integer 3 integers.
Output sum sum

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

IPO analysis Sequence Looping

Input sales
sales1, sales2, sales3,
sales4, sales5, sales6,
sales7

Process Calculate total sales by Determine total sales for a week.


a week.

Output total sales total sales

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.

Output average average

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

1. Create a program that will calculate the area


of 100 circles.

2. Create a program that will calculate the area


of circles based on radius. The program will
stop if user enter negative radius.
Repetition
Sentinel Control Question 1
Create a program that will calculate the area
of 100 circles.

In this question, you know that the program will


calculate the area of 100 circles based on the
radius entered.
 Repetition control structure using
counter-controlled
Repetition
Sentinel Control Question 2
Create a program that will calculate the area of
circles based on radius. The program will stop if
user enter negative radius.
In this question, you did not know how many
times the program will calculate the area of
circle. But you know that the program will STOP
if user enter negative radius.
 Repetition control structure using sentinel-
controlled
Repetition Repetition control structure using
Sentinel Control sentinel-controlled

To solve this kind of problems, we need this:

1. Initial value of sentinel control variable –


input from user
2. Condition to test sentinel control variable
3. Update the value of sentinel control
variable – input from user
Repetition
Sentinel Control Question 2
1. Initial value of sentinel control
variable – input from user read radius
2. Condition to test sentinel
control variable while (radius ≥ 0)
3. Update the value of sentinel
control variable read radius
Repetition
Sentinel Control Solution – Question 2
Create a program that will calculate the area of circles based
on radius. The program will stop if user enter negative radius.

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.

2. Create a C++ program that will display message based on


marks entered. Message “You are great” will be displayed
if marks is greater or equal to 50. otherwise program will
display “Try again”. The program will be terminate if user
enter 0 as mark.

You might also like