7 Algorithm design and problem-solving

Show All Section Notes

Identifying and Correcting Errors

1. Key Terms

TermMeaning
BugAn error in a program that stops it working as intended
DebuggingThe process of finding and removing errors from a program
Dry runWorking through an algorithm by hand, on paper, without running it on a computer
Trace tableA table used to record the value of every variable, output and prompt at each step of a dry run

2. Types of Error

Syntax errors

A syntax error breaks the rules of the language: a misspelled keyword, a missing bracket, a missing quotation mark. The program will not translate, so it cannot run at all.

Logic errors

A logic error means the program runs perfectly but produces the wrong result. The syntax is legal, so nothing warns you. These are the errors trace tables are designed to catch.

Syntax errorLogic error
Does the program run?NoYes
How it is spottedThe translator reports itThe output is wrong — found by testing or a dry run
ExampleOUPUT TotalUsing + where * was intended
“It does not work” is never an answer. When asked to identify an error, state which line is wrong, what is wrong with it, and what it should be. All three parts are usually needed for full marks.

3. The Five Errors That Appear Most Often

Exam questions give you a short algorithm containing deliberate mistakes. Almost all of them are one of the following.

(a) Initialising inside the loop

// WRONG - Total resets every pass FOR Count ← 1 TO 10 Total ← 0 INPUT Number Total ← Total + Number NEXT Count
// CORRECT - initialise once, before the loop Total ← 0 FOR Count ← 1 TO 10 INPUT Number Total ← Total + Number NEXT Count

Symptom: the total equals the last value entered.

(b) Off-by-one loop bounds

// WRONG - only 9 iterations, and misses item 10 FOR Count ← 1 TO 9

Symptom: one item is always missed, or the program tries to read past the end of an array. Check the count carefully: 1 TO 10 runs ten times, 0 TO 10 runs eleven.

(c) The wrong comparison operator

// WRONG - excludes the boundary value 50 IF Mark > 50 THEN OUTPUT "Pass"
// CORRECT - if 50 is a pass IF Mark >= 50 THEN OUTPUT "Pass"

Symptom: everything works except at the exact boundary. Confusing > with >= is the single most common logic error in this topic.

(d) An infinite loop

// WRONG - Index never changes, so the loop never ends Index ← 1 WHILE Index <= 10 OUTPUT Names[Index] ENDWHILE

Cause: the variable in the loop condition is never updated inside the loop. Every condition-controlled loop must contain something that eventually makes the condition false.

(e) A swap without a temporary variable

// WRONG - the first value is destroyed; both end up the same A ← B B ← A
// CORRECT - three steps via Temp Temp ← A A ← B B ← Temp

4. How to Find an Error Systematically

Do not read an algorithm hoping the mistake will stand out. Work through it:

  1. Read the stated purpose. You cannot judge whether an algorithm is wrong without knowing what it is meant to do.
  2. Check every initialisation. Are totals and counters set before the loop, not inside it?
  3. Check every loop bound. Count the iterations by hand.
  4. Check every comparison. Should it be > or >=?
  5. Dry run with a trace table using a small amount of data — three or four values is enough to expose most errors.
  6. Test the boundary specifically, because that is where operator errors hide.

Worked example

This algorithm should output the highest of five numbers. Find the errors.

Highest ← 0 FOR Count ← 1 TO 4 INPUT Number IF Number < Highest THEN Highest ← Number ENDIF NEXT Count OUTPUT Highest
LineErrorCorrection
Highest ← 0Fails if all five numbers are negativeInput the first number and set Highest to it
FOR Count ← 1 TO 4Only four numbers are read, not fiveFOR Count ← 1 TO 5
IF Number < HighestWrong operator — this finds the lowestIF Number > Highest
Notice that the algorithm would run without complaint and print a number. Nothing is syntactically wrong. All three faults are logic errors, and only a dry run reveals them.

5. Exam Focus

Suggest a correction, do not just identify the fault. The command word is usually “identify the errors and suggest ways of correcting them”. Half the marks are in the correction. Write the corrected line out in full.
Quote the line, or refer to its number. Answers that say "the loop is wrong" without saying which line, or what about it is wrong, cannot be credited.
Be precise, and use symbols. The syllabus requires precision in algorithms: x > y is acceptable, "x is greater than y" is not.

Quick self-check

  • State the difference between a syntax error and a logic error, and how each is detected.
  • Why does a total initialised inside a loop give the last value entered?
  • Write the three lines that correctly swap two values.
  • Give one reason a WHILE loop might never end.
  • An algorithm should accept marks of 40 and above. It uses IF Mark > 40. Which test data exposes the error?