Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Selection Algorithm

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Conditional algorithms

The following are some conditions that can be tested using the
IF structure:

Symbol Meaning Example


IF A=B THEN
equals
THEN
IS greater than IFA> B
is less than IF A<B THEN
Is greater than or equal to IF A>=BTHEN
is less than or equal to IF A= BTHEN

IFA > BTHEN


is not equal to

Problem 5
two numbers and
Wite an algorithm that requests a user to enter
Start
Complete the following task:

lf the first number is then divide the first


larger than the second,
number by the second and output the result, else, multiply the two
Prormpt and numberS and output the result.
INPUT two
numbers X Y/ Possible algorithm
The summary for the steps to writing the possible algorithm is
shown in Table 12.3.

Table 12.3 Multiply or divide two numbers


Yes
IfX> Y Algorithm refined Algorithm refined
Algorithm
further
No Enter two numbers Prompt enter two Prompt and INPUT
and
numbers X and Y X,Y
Let ANSWER = Ifthe first number IfX is greater than Y IFX>Y THEN
Let ANSWER = LET ANSWER = XY
than the then
X*Y X/Y is larger
Second number, DivideX by Y else ELSE
then divide the Multiply X by Y LETANSWER =X
thee Y
first number by
second number; ENDIF
PRINT else multiply the
ANSWER
by the
first number
second number.
O u t p u t the answer Output answer PRINT ANSWER
would look like the one in Figure 12.8.
In a flowchart, the algorithm
END

igure 12.8 Flowchart: Multiply or


divide two numbers

223
12 PROBLEM SOLVING AND ALGORITHM DEVELOPMENT (7

Problem 6
Calculate the total and average mark made by a student in five
Start
subjects. If the student made an average of 50 or more, output the
total mark, average mark and the message Pass'; else print the total
mark, average mark and the message 'Fail.

Possible algorithm
Prompt and The sumnmary for the steps to wTiting the possible algorithm is
INPUT A, B. C
D, E shown in Table 12.4.

Table 12.4 Total and average student marks

Algorithm Algorithm refined Algorithm refined


further
Let TOTAL = Prompt and INPUT
Enter the marks Prompt and enter
A+B+C+D+E
made in the five five marks A,B,C,D.E
Subjects A, B, C, D, E
Find the total Calculate the total LET TOTAL =

marks using the A+B+C +D +E


formula Total =
Let AVERAGE
= TOTAL/5
A+B+C+D +E
Find the average Calculate the LET AVERAGE =

average mark using TOTAL5


the formula Average
Total/5
If AVERAGE If average is greater If Average is greater IFAVERAGE> 50
50 than fifty, then output than 50 then output THEN
the total mark, Total mark, Average PRINT TOTAL
NO average mark and mark and the AVERAGE, 'PassS
Pass'; else output message 'Pass' SE
the total mark, Else PRINT TOTAL
PRINT
TOTAL average mark and Output Total mark, AVERAGE, 'Fail
AVERAGE Fail Average mark and ENDIF
Fail
the message "Fail END
Endif
PRINT
TOTAL In a flowchart, the algorithm would look like the one in F1guie l 29
AVERAGE
Pass

END

Figure 12.9 Flowchart: Total and


marks (fromIF
average student
statement only)

224

You might also like