Chap 9-Part 3 1
Chap 9-Part 3 1
Algorithm that checks if a percentage mark is valid and whether it is a pass or a fail
This makes use of 2 IF statements, the second IF statement is part of the first ELSE path.
This is called Nested IF
Pseudocode:
OUTPUT “Please enter a mark”
INPUT PercentageMark
IF (PercentageMark < 0) OR (PercenrageMark >100)
THEN
OUTPUT “Invalid Mark”
ELSE
IF PercentageMark > 49
THEN
OUTPUT “Pass”
ELSE
OUTPUT “Fail”
ENDIF
ENDIF
A Sample Pseudocode: to find the
average of a number of integers input
A more realistic algorithm would include checks that all the values input are
whole numbers and that the number input to determine how many integers are
input is also positive.
We’ll make use of the function INT(x) that returns the integer part of x:
INT(x : REAL) RETURNS INTEGER
returns the integer part of x
Example: INT(27.5415) returns 27
➢ The identifier table for this algorithm is:
Solution using Nested Loops
Activity 9E
In pseudocode, write statements to check that a number input is between 10 and 20 or
over 100. Make use of brackets to ensure that the order of the comparisons is clear.
Number <- 0
REPEAT
PRINT "Enter a number, your number must be between
10 and 20 or over 100"
INPUT Number
UNTIL (Number > 10 AND Number < 20) OR (Number > 100)
Activity 9F
In pseudocode, write an algorithm to set a password for a user when they have to input the same word
twice. Then allow the user three attempts to enter the correct password. Complete an identifier table
for your algorithm.
Finally, check your pseudocode algorithm works by writing a short program from your
pseudocode statements using the same names for your identifiers.
DECLARE Password1, Password2 : STRING
DECLARE Counter : INTEGER
Counter <- 0
REPEAT
OUTPUT "Enter your password "
INPUT Password1
OUTPUT "Enter your password again "
INPUT Password1
Counter <- Counter + 1
UNTIL (Counter = 3) OR (Password1 = Password2)
IF Password1 = Password2
THEN
OUTPUT "Password correct"
ELSE
OUTPUT "Password incorrect"
ENDIF