The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by inaazlin, 2018-12-11 21:12:33

2.1 (c) Design a Solution-Repetition

Design a Solution-Repetition

Keywords: Repetition

Control Structure : Looping

1. Counter Controlled - Practice 2

Based on the pseudocode below, draw the flowchart.

count assigned five
repeat
display “Blast off is soon”
decrement one of count

Until count < one

No.1

Write a pseudo code for a program that allows a
teacher to read quiz mark from 5 students. The
program should calculate and display the total mark of
the students.

SOLUTION Initialize student to 1
Initialize total to 0
Pseudocode:
Condition.
start To check whether student
student = 1, total = 0 still less than or equal to 5
while ( student<= 5)
read mark Accumulating
total = total + mark
student=student+1 Incrementation:
endwhile Increment student by
print total
1
stop

SOLUTION start
student=1,total=0
flowchart:
While true
false student<=
Read mark
Print total 5 total=total+mark
stop
student=student+1

TRACING TABLE.

ACCUMULATE INCREMENT

student student<=5 read total=total+mark student = student +1
mark
1 1<=5 T 30 30 =0+30 2=1+1
2 2<=5 T 20 50=30+20 3=2+1
3 3<=5 T 40 90 =50 + 40 4=3+1
4 4<=5 T 30 120= 90 + 30 5=4+1
5 5<=5 T 30 150=120+ 30 6=5+1
6 6<=5 F
OUTPUT: 150 - - -

No.2

Write a pseudo code for a program that allows a
teacher to read quiz mark from 5 students. The
program should calculate and display the average mark
of the students.

SOLUTION

Pseudocode:

start
student = 1, total = 0
while ( student<=5)

• read mark
total = total + mark
student=student+1

endwhile
average=total/5
print average
stop

start
Student=1,total=0

false While true
student<=
average=total/5 Read mark
Print average 5 total=total+mark
stop
student=student+1

ACCUMULATE INCREMENT

student student<=5 read total=total+mark student = student +1
mark
1 1<=5 T 30 30 =0+30 2=1+1
2 2<=5 T 20 50=30+20 3=2+1
3 3<=5 T 40 90 =50 + 40 4=3+1
4 4<=5 T 30 120= 90 + 30 5=4+1
5 5<=5 T 30 150=120+ 30 6=5+1
6 6<=5 F
- - -

Total = 150
Average = 150/ 5

= 30

No.3 - 5

Write a pseudo code, flow chart and tracing table for the
following problem statement:

3. Program to display number 1 until 10

4. Program to calculate and display sum of number 1
until 10.
5. Program to calculate and display sum of ten numbers
input by user.

Pseudocode

CHALLENGE 3 CHALLENGE 4

Start Start
number =1 counter =1,sum=0
while (number<=10) while (counter<=10)

Print number read number
number=number+1 sum=sum+number
endwhile counter=counter+1
stop endwhile
print sum
stop

Pseudocode

CHALLENGE 5

Start
counter =1,sum=0
while (counter<=10)

read number
sum=sum+number
counter=counter+1
end while
print sum
stop

Control Structure : Looping
Loops can be ended in 2 ways :-

1. Counter controlled 2. Sentinel value
controlled

Control Structure : Looping

2. Sentinel Value Controlled
• Number of loop is not known before the loop begin

executing.
• The loop will stop when a sentinel value is reached.
• Sentinel value is a special value (called signal value,

dummy value or flag value) such as 0, -1, 999, Y, N, T, F,
XXX, to indicate ‘end of data entry’.

Control Structure : Looping

2. Sentinel Value Controlled

• General Format :Pseudocode :
start

initialization
repeat while (condition)

one or more instructions

Loop body .

.
.
update(increment/decrement)
end repeat/while
stop

Control Structure : Looping

2. Sentinel Value Controlled

• General Format :Pseudocode :
start

read input
repeat while (input != sentinel value)

one or more instructions
.

Loop body .

read input
end repeat/while
stop

Example sentinel value control

Loops is controlled
by sentinel value controlled

Write a pseudo code for a program that receive
and display back age of students. The program
will terminate if user enter age 0 or less.

Example sentinel value control

start •If these are the ages entered by
read age a user:
while (age > 0)
print age 2, 15, 60, -2
read age What is the output?
end while
•These are the output produced
stop by the computer:

2
15
60

Example sentinel value control

start while (15 > 0) while (60 > 0)
read 2
while (2 > 0) print 60
read -2
print 2 print 15 end while

read 15 read 60 2
15
• end while end while 60

22
15

Example sentinel value control

while (-2 > 0)
instructions will not be
processed because condition is
false

end while
stop

2 Therefore, the computer will
15 only display these three
60 numbers.

Example sentinel value control

flowchart

start What is the output if the user
Read age enters these numbers?
51, 0, 3

false age > 0 true

stop Print age
Read age

Control Structure : Looping

2. Sentinel Value Controlled

Example of Problem 1 Write the IPO Analysis, pseudocode and draw
flowchart to calculate the total score. When user
enters -1, the program will display total score and
terminate the program.

Step 1

 Number of loops – unknown, depends on user input
 Loop condition – while user input is not equal -1
 Task to do before loop:

 Assign totalscore=0 and read score
 Task to do in every pass of the loops:

 To total up score
 To read/input score
 Task to do after the loop stops:
 Display totalscore

Example of Problem 1 – IPO Analysis

Write the IPO Analysis, pseudocode and draw flowchart to
calculate the total score. When user enters -1, the program will
display total score and terminate the program

Input score
Process
Output To repeat calculating the total score
until the condition is false

Display the total score

Control Structure : Looping

2. Sentinel Value Controlled

Example of Problem 1 Write the IPO Analysis, pseudocode and draw
flowchart to calculate the total score. When user
enters -1, the program will display total score
and terminate the program

Start
totalscore=0
read score
while score not equal to -1
totalscore = totalscore + score
read score
end while
display totalscore

stop

Control Structure : Looping
2. Sentinel Value Controlled - Flowchart

Example of Problem 1 Write the IPO Analysis, pseudocode and draw
flowchart to calculate the total score. When
user enters -1, the program will display total
score and terminate the program

start

totalscore= 0

input score

display False score True totalscore =
totalscore != -1 totalscore + score

stop input score

Control Structure : Looping

2. Sentinel Value Controlled

Example of Problem 2 Write the IPO Analysis, pseudocode and draw
flowchart that accepts numbers from the user.
Print positive numbers only. Loop will stop when
the user enters number 0.
Step 1

 Number of loops – unknown, depends on user input
 Loop condition – while user input is not equal 0
 Task to do before loop:

 To read/input number
 Task to do in every pass of the loops:

 To determine the number entered is positive
or not
 If True, print the number.

 To read/input score
 Task to do after the loop stops:

 none

Example of Problem 2 – IPO Analysis

Write the IPO Analysis, pseudocode and draw flowchart that
accepts numbers from the user. Print positive numbers only. Loop
will stop when the user enters number 0.

Input number
Process
Output To repeat printing positive numbers
until the condition is false.

Display the positive numbers only.

Control Structure : Looping
2. Sentinel Value Controlled – Pseudocode

Example of Problem 2 Write the IPO Analysis, pseudocode and draw
flowchart that accepts numbers from the user.
Print positive numbers only. Loop will stop when
the user enters number 0.
Step 2

Start
read num
repeat while num not equal to 0
if (num > 0)
print num
read num
end repeat while

stop

Control Structure : Looping
2. Sentinel Value Controlled – Flowchart

Example of Problem 2 Write the IPO Analysis, pseudocode and draw
flowchart that accepts numbers from the user.
Print positive numbers only. Loop will stop when
the user enters number 0.

start
read num

num True if num True
!= 0 >0 print num

False False read num

stop

No.6

Write a pseudo code and flow chart for the following problem
statement:

6. Program that receives and display number . It stop
only when user enter any negative number.

CHALLENGE 6

Start
read number
while (number>=0)

Print number
read number
end while
stop

No.7

Write a pseudo code and flowchart for the following problem
statement:

7. Write a program that repeatedly collects positive
integers from the user, stopping when the user enters a
negative number or zero. Calculate the total sum of all
the positive integers.

Control Structure : Looping

PRACTICE

Identify the IPO Analysis, write the pseudocode and draw
a flowchart that reads and prints a series of student's
names and exams scores. Before the program ends, print
the average score. Loop will only stop when user enters
999.

Control Structure : Looping

THANK YOU


Click to View FlipBook Version