7.7 Identifying Errors in Algorithms
7.7 Identifying Errors in Algorithms
Q1. A piece of pseudocode was written to input ten numbers and then calculate
and output the largest and average value of the input data.
1 Largest = 0
2 Sum = 0
3 FOR X = 0 TO 10
4 INPUT X
5 IF X > Largest THEN X = Largest
6 OUTPUT Largest
7 Sum = Sum + X
8 NEXT X
9 Average = Sum * 10
10 OUTPUT Average
There are four errors in this algorithm. Locate these errors and suggest a
correction.
Error 1: Line 40: INPUT X is using the same counter variable of FOR-NEXT loop
Q2. Read this section of program code that should input 50 numbers and then output the
average.
1 Total = 0
2 FOR Counter = 1 TO 50
3 INPUT Num
4 Total = Total + 1
5 Counter = Counter + 1
6 Average = Total/Counter
7 NEXT Counter
8 PRINT Average
Locate these errors and suggest code corrections to remove each error. Error 1:
INPUT Number
WHILE Numbers < > 999 DO IF
Number > 100
THEN
OUTPUT Number
ENDI
F
ENDWHIL
E
OUTPUT Number
Error 4: The final OUTPUT Number outside the loop is not needed.
Q4. An algorithm allows a user to input their password and checks that there are at least
eight characters in the password. Then, the user is asked to re-input the password to check
that both inputs are the same. The user is allowed three attempts at inputting a password of
the correct length and a matching pair of passwords.
The pre-defined function LEN(X) returns the number of characters in the string, X. 01
Attempt ‹— 0
02 REPEAT
03 PassCheck ‹— TRUE
04 OUTPUT "Please enter your password "
05 INPUT Password
06 IF LEN(Password) < 8
07 THEN
08 PassCheck ‹— TRUE
09 ELSE
10 OUTPUT "Please re-enter your password "
11 INPUT Password2
12 IF Password < > Password
13 THEN
14 PassCheck ‹— FALSE
15 ENDIF
16 ENDIF
17 Attempt ‹— Attempt + 1
18 UNTIL PassCheck OR Attempt < > 3
19 IF PassCheck
20 THEN
21 OUTPUT "Password success"
22 ELSE
23 OUTPUT "Password fail"
24 ENDIF
Description: Ask to input the password twice and compares both to check if they are
same.
c) Give two sets of test data for this algorithm and a reason for choosing each set.
Each set of test data and its reason must be different.
Set 1: ALk#24
Reason: Abnormal data, it should be rejected because it contains characters less than 8.
Reason: Normal data, it should be accepted because it contains characters not less than 8 and
both are same.
The function RANDOM(X, Y) generates a random integer greater than X and less than or equal
to Y. For example, RANDOM(1, 4) generates 2 or 3 or 4
01 Count ‹— 0
02 WHILE Counter > 50 DO
03 NumRand[Counter] ‹— RANDOM(1, 100)
04 Counter ‹— Counter - 2
05 ENDWHILE
Find the four errors in the pseudocode and write a correction for each error. Error 1:
01 Continue ‹— 1
02 WHILE Continue = 0
03 OUTPUT "Enter 1 for +, 2 for -, 3 for *, or 4 for /"
04 INPUT Operator
05 OUTPUT "Enter the first value"
06 INPUT Value1
07 OUTPUT "Enter the second value"
08 OUTPUT Value2
09 #IF Operator
10 1 : Answer ‹— Value1 + Value2
11 2 : Answer ‹— Value1 - Value2
12 3 : Answer ‹— Value1 * Value2
13 4 : Answer ‹— Value1 / Value2
14 ENDCASE
15 OUTPUT "The answer is ", Value1
16 OUTPUT "Do you wish to enter more values (Yes or No)?"
17 INPUT MoreValues
18 IF MoreValues = "No"
19 THEN
20 Continue ‹— 1
21 ENDIF
22 UNTIL Continue = 0
Find the five errors in the pseudocode and suggest a correction for each error. Error 1:
/ OR /