7.8 Identifying Errors in Algorithms

Question Bank · 6 Questions

Objectives: Students should be able to —

  • 1 Identify and correct errors in flowcharts.
  • 2 Identify and correct errors in programs.
  • 3 Identify and correct errors in pseudocode.
  • 4 Use a trace table or dry run to locate logic errors in a given algorithm.
  • 5 Suggest a correction to remove each located error.
  • 6 Identify validation checks (length, double-entry) and design appropriate test data.

Identifying Errors in FOR-NEXT Loop Algorithms

10 Largest = 0 20 Sum = 0 30 FOR X = 0 TO 10 40 INPUT X 50 IF X > Largest THEN X = Largest 60 OUTPUT Largest 70 Sum = Sum + X 80 NEXT X 90 Average = Sum * 10 100 OUTPUT Average

There are four errors in this algorithm. Locate these errors and suggest a correction.

Error Correction
Error 1: Line 40 — INPUT X is using the same counter variable of the FOR-NEXT loop. Method: Change the counter variable of the FOR-NEXT loop.
For example — FOR Count = 1 TO 10
Error 2: Line 50 — X = Largest Method: It should be, Largest = X
Error 3: Line 60 — OUTPUT Largest should not be inside the loop. Method: It should be outside the loop.
For example — 85 OUTPUT Largest
Error 4: Line 90 — Incorrect formula for calculation of average. Method: It should be — Average = Sum / 10
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
Error Correction
Error 1: Line 4 — Total = Total + 1 Method: It should be, Total = Total + Num
Error 2: Line 5 — Counter = Counter + 1 Method: It should be removed because the counter variable's value increments by 1 automatically in a FOR-NEXT loop.
Error 3: Line 6 — Average = Total / Counter is inside the loop. Method: It should be outside the loop. Swap lines 6 and 7.
Error 4: Line 6 — Incorrect formula for calculation of average. Method: It should be — Average = Total / 50

Identifying Errors in WHILE Loop & Conditional Algorithms

INPUT Number WHILE Numbers <> 999 DO IF Number > 100 THEN OUTPUT Number ENDIF ENDWHILE OUTPUT Number

(a) Identify the four errors and suggest corrections:

Error Correction
Error 1: WHILE Numbers <> 999 DO Method: Numbers should be Number. It should be —
WHILE Number <> 999 DO
Error 2: IF Number > 100 Method: It should be —
IF Number >= 100
Error 3: INPUT Number is missing from inside the loop. Method: Insert INPUT Number after the ENDIF statement.
Error 4: The final OUTPUT Number outside the loop is not needed. Method: Remove it.

(b) Pseudocode statement to output all numbers between 100 and 200 inclusive:

IF Number >= 100 AND Number <= 200

This uses a compound condition with the logical operator AND.

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
Error Correction
Error 1: Line 01 — Count ← 0 Method: It should be — Counter ← 0
Error 2: Line 02 — WHILE Counter > 50 DO Method: It should be — WHILE Counter < 50 DO
Error 3: Line 03 — NumRand[Counter] ← RANDOM(1, 100) Method: It should be — NumRand[Counter] ← RANDOM(0, 100)
Error 4: Line 04 — Counter ← Counter - 2 Method: It should be — Counter ← Counter + 1

Identifying Errors in REPEAT-UNTIL with Validation & Test Data

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

(a) Identify the three errors and suggest corrections:

Error Correction
Error 1: Line 8 — PassCheck ← TRUE Method: It should be — PassCheck ← FALSE
Error 2: Line 12 — IF Password <> Password Method: It should be — IF Password <> Password2
Error 3: Line 18 — UNTIL PassCheck OR Attempt <> 3 Method: 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 Description
Type of check 1: Length check validation Checks the number of characters in the password.
Type of check 2: Double entry verification check Asks the user to input the password twice and compares both to check if they are the same.

(c) Give two sets of test data for this algorithm and a reason for choosing each set:

Test Data Reason
Set 1: ALk#24 Abnormal data — it should be rejected because it contains fewer than 8 characters.
Set 2: John@7351, John@7351 Normal data — it should be accepted because it contains not fewer than 8 characters and both entries are the same.

Identifying Errors in CASE Statement Algorithms

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

Two valid alternatives are shown below. Either set is acceptable.

Alternative 1 — Convert to a WHILE loop:

Error Correction
Error 1: Line 01 — Continue ← 1 Method: It should be — Continue ← 0
Error 2: Line 22 — UNTIL Continue = 0 Method: It should be — ENDWHILE
Error 3: Line 08 — OUTPUT Value2 Method: It should be — INPUT Value2
Error 4: Line 09 — IF Operator Method: It should be — CASE OF Operator
Error 5: Line 15 — OUTPUT "The answer is ", Value1 Method: It should be — OUTPUT "The answer is ", Answer

Alternative 2 — Convert to a REPEAT-UNTIL loop:

Error Correction
Error 1: Line 02 — WHILE Continue = 0 Method: It should be — REPEAT
Error 2: Line 20 — Continue ← 1 Method: It should be — Continue ← 0
Error 3: Line 22 — UNTIL Continue = 0 Method: It should be — UNTIL Continue = 1
Error 4: Line 08 — OUTPUT Value2 Method: It should be — INPUT Value2
Error 5: Line 09 — IF Operator Method: It should be — CASE OF Operator
Error 6: Line 15 — OUTPUT "The answer is ", Value1 Method: It should be — OUTPUT "The answer is ", Answer

Revision: Statements and Key Computing Terms

Statement Key Term
An error that causes an algorithm to produce incorrect results, even though it runs without crashing.Logic error
An error caused by breaking the rules (grammar/spelling) of the programming language.Syntax error
A table used to trace and record the value of each variable as an algorithm is dry-run.Trace table
Working through an algorithm on paper, line by line, without running it on a computer.Dry run
A loop where the number of iterations is known in advance (e.g., FOR X = 1 TO 10).FOR-NEXT loop
A loop where the condition is tested before each iteration (may run zero times).WHILE-ENDWHILE loop
A loop where the condition is tested after each iteration (always runs at least once).REPEAT-UNTIL loop
A multi-way selection structure that compares a single variable against a list of values.CASE OF ... ENDCASE
A variable whose value increases by 1 each time a loop runs.Counter variable
A variable used to add up a running total (e.g., Sum = Sum + X).Accumulator
A validation check that ensures the number of characters in an input is within an acceptable range.Length check
A verification check where the user is asked to enter the same data twice and the two inputs are compared.Double entry verification
Test data that should be accepted by the program because it is valid.Normal data
Test data that should be rejected by the program because it is invalid (also called erroneous data).Abnormal data
Test data that lies on the boundary of acceptability (e.g., 8 chars when 8 is the minimum).Boundary / Extreme data
A pre-defined function that returns the number of characters in a string.LEN(X)
A pre-defined function that generates a random integer greater than X and less than or equal to Y.RANDOM(X, Y)