Repetition Algorithm Examples
Repetition Algorithm Examples
Problem 3
Start A bus picks up passengers at several stops along its route Wnte
an algorithm to read the number of passengers picked up at each
stop, and add it to the total number of passengers. Calculate
the total number of passengers picked up in one day if the data
is teminated by the user entering a value of 999. Print the total
Set total number of
passengers to 0
number of passengerS.
Solution
Algorithm rough
Set the total number of passengers to 0
Enter number
of passengers Prompt and enter the number of passengers at the stop
at the stop
ENDWHLE
Total number of
Is ne No
nurnber of
passengers = total
Print "Total Number of Passengers"
number of passangers+|
passengers number of passengers
999 at the stop Algorithm refined
Yes
Total number of passengers =0
STOp ENDWHILE
Print "Total Number of Passengers"
Figure 11.8 Flowchart for Problem 3
Algorithm further refined
Total Passengers = 0
ENDWHILE
Print "Total Number of Passengers
192
WHILE-DO statemen=
Problem 4
Write an algorithm to enter the marks students obtained in English,
and count the number of students in a class. Calculate the average
English mark of the group of students if the data is terminated by
the value 999. Print the number of students in the class and the
average mark of the students.
Set the number of
students to 0
Solution
Algorithm rough
Set the number of students to 0
Set the total student Set the total students marks to 0
mark to O
Enter student
ENDWHILE
mark
Find the average of the students mark by dividing the total
students mark by the total number of students
ENDWHILE
Stop