Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
134 views

7.7 Identifying Errors in Algorithms

The document discusses identifying errors in algorithms by providing examples of pseudocode with errors and the corrections. It provides multiple questions with pseudocode containing errors and asks the reader to identify the errors and provide corrections.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

7.7 Identifying Errors in Algorithms

The document discusses identifying errors in algorithms by providing examples of pseudocode with errors and the corrections. It provides multiple questions with pseudocode containing errors and asks the reader to identify the errors and provide corrections.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Identifying errors in algorithms

Objectives: Student should be able to -


 identify and correct errors in:
 flowcharts
 programs
 pseudocode

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

Correction: Change the counter variable of FOR-NEXT loop.

For example - FOR Count = 1 TO 10

Error 2: Line 50: X = Largest

Correction: It should be, Largest = X

Error 3: Line 60: Output should not be inside the loop.

Correction: It should be outside the loop.

For example - 85 OUTPUT Largest

Error 4: Line 90: Incorrect formula for calculation of average.

CS 2210/ 0478 Page 1 of 8


Correction: It should be - Average = Sum/10

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

There are four errors in this code.

Locate these errors and suggest code corrections to remove each error. Error 1:

Line 4: Total = Total + 1

Correction: It should be, Total = Total + Num

Error 2: Line 5: Counter = Counter + 1

Correction: It should be removed because the counter variable's value increments

by 1, automatically in FOR-NEXT loop.

Error 3: Line 6: Average = Total/Counter

Correction: It should be outside the loop. Swap line 6 and 7.

Error 4: Line 6: Average = Total/Counter

Correction: Incorrect formula for calculation of average.

It should be - Average = Total/50

CS 2210/ 0478 Page 2 of 8


Q3. An algorithm has been written in pseudocode to input some numbers. It only outputs
any numbers that are greater than or equal to 100. The number 999 is not output and stops
the algorithm.

INPUT Number
WHILE Numbers < > 999 DO IF
Number > 100
THEN
OUTPUT Number
ENDI
F
ENDWHIL
E
OUTPUT Number

a) Identify the four errors in the pseudocode and suggest corrections.

Error 1: WHILE Numbers < > 999 DO

Correction: Numbers should be Number. It should be -

WHILE Number < > 999 DO

Error 2: IF Number > 100

Correction: It should be, IF Number >= 100

Error 3: INPUT Number is missing from inside the loop.

Correction: Insert INPUT Number after the ENDIF statement.

Error 4: The final OUTPUT Number outside the loop is not needed.

Correction: Remove it.

CS 2210/ 0478 Page 3 of 8


b) Write a pseudocode statement to change the corrected algorithm to output all
numbers between 100 and 200 inclusive.

You do not need to rewrite the whole algorithm.

IF Number >= 100 AND Number < = 200

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

CS 2210/ 0478 Page 4 of 8


a) Identify the three errors in the pseudocode and suggest a correction to remove
each error.

Error 1: Line 8: PassCheck ‹— TRUE

Correction: It should be, PassCheck ‹— FALSE

Error 2: Line 12: IF Password < > Password Correction: It

should be, IF Password < > Password2 Error 3: Line 18:

UNTIL PassCheck OR Attempt < > 3

Correction: It should be, UNTIL PassCheck OR Attempt = 3

b) The algorithm includes two types of check on the data input.


Identify and describe each type of check.

Type of check 1: Length check validation.

Description: Checks number of characters in password.

Type of check 1: Double entry verification check.

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.

Set 2: John@7351, John@7351

Reason: Normal data, it should be accepted because it contains characters not less than 8 and
both are same.

CS 2210/ 0478 Page 5 of 8


Q5. An algorithm has been written in pseudocode to generate 50 positive random integers
with values less than or equal to 100. These numbers are stored in the array NumRand[ ]

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:

Line 01: Count ‹— 0

Correction: It should be, Counter ‹— 0

Error 2: Line 02: WHILE Counter > 50 DO

Correction: It should be, WHILE Counter < 50 DO

Error 3: Line 03: NumRand[Counter] ‹— RANDOM(1, 100) Correction:

It should be, NumRand[Counter] ‹— RANDOM(0, 100) Error

4: Line 04: Counter ‹— Counter - 2

Correction: It should be, Counter ‹— Counter + 1

CS 2210/ 0478 Page 6 of 8


Q6. The pseudocode algorithm should work as a calculator and output the result.

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:

Line 01: Continue ‹— 1

Correction: It should be, Continue ‹— 0

Error 2: Line 22: UNTIL Continue = 0

Correction: It should be, ENDWHILE

Error 3: Line 08: OUTPUT Value2

Correction: It should be, INPUT Value2

Error 4: Line 09: IF Operator

Correction: It should be, CASE OF Operator

CS 2210/ 0478 Page 7 of 8


Error 5: Line 15: OUTPUT "The answer is ", Value1

Correction: It should be, OUTPUT "The answer is ", Answer

/ OR /

Error 1: Line 02: WHILE Continue = 0

Correction: It should be, REPEAT

Error 2: Line 20: Continue ‹— 1

Correction: It should be, Continue ‹— 0

Error 3: Line 22: UNTIL Continue = 0 Correction:

It should be, UNTIL Continue = 1

Error 4: Line 08: OUTPUT Value2

Correction: It should be, INPUT Value2

Error 5: Line 09: IF Operator

Correction: It should be, CASE OF Operator

Error 6: Line 15: OUTPUT "The answer is ", Value1

Correction: It should be, OUTPUT "The answer is ", Answer

CS 2210/ 0478 Page 8 of 8

You might also like