[7.9] Write/amend algorithms

[7.9] Write and amend algorithms using pseudocode, program code and flowcharts

In this benchmark you will practise designing clear algorithms and expressing them in three complementary representations: CAIE-style pseudocode, program code (we will use Python as a concrete example), and flowcharts. You will also learn how to amend an existing algorithm to fix errors, improve clarity, or add small features while keeping the overall intent the same. Strong exam answers show you can move between these forms confidently and keep the logic consistent.

What is an algorithm?

An algorithm is a finite, ordered sequence of steps that solves a general problem. Good algorithms have predictable inputs, clearly defined processes, and unambiguous outputs. For IGCSE you must be able to create algorithms that use common constructs: sequence (one step after another), selection (IF, CASE), and iteration (FOR, WHILE, REPEAT). You should also be comfortable with typical patterns such as counters, accumulators, flags, and search/sort routines.

Pseudocode, program code, and flowcharts compared

Form Purpose Strengths Watch-outs
CAIE pseudocode Language-independent way to describe logic using agreed keywords Concise, exam-friendly, focuses attention on logic not syntax Avoid implementation detail; use correct CAIE conventions (e.g. INPUT, OUTPUT, FOR ... NEXT, for assignment)
Program code (Python) Executable instructions in a real language Testable, precise types and operators, can be run to verify behaviour Beware off-by-one indices, integer vs real division, indentation
Flowcharts Visual plan of steps and control flow Great for planning and communicating with non-programmers Use the correct symbols: start/end (terminator), input/output (parallelogram), process (rectangle), decision (diamond), flow lines with arrows

From requirements to three representations

Suppose we need an algorithm that reads a list of positive integers, outputs the count of values over a threshold, and the average of those values. Below are aligned versions in pseudocode, Python, and a flowchart-oriented step outline. Notice how the logic remains the same while the representation changes.

// CAIE Pseudocode
DECLARE numbers : ARRAY OF INTEGER
DECLARE threshold : INTEGER
DECLARE total : INTEGER ← 0
DECLARE count : INTEGER ← 0
INPUT threshold
// assume numbers is already filled or read in a loop
DECLARE i : INTEGER
FOR i ← 0 TO LENGTH(numbers) - 1
  IF numbers[i] > threshold THEN
    total ← total + numbers[i]
    count ← count + 1
  ENDIF
NEXT i
IF count = 0 THEN
  OUTPUT "No values above threshold"
ELSE
  OUTPUT "Count = ", count
  OUTPUT "Average = ", total / count
ENDIF
# Python
numbers = [12, 4, 25, 7, 30]
threshold = int(input("Threshold: "))
total = 0
count = 0
for value in numbers:
    if value > threshold:
        total += value
        count += 1
if count == 0:
    print("No values above threshold")
else:
    print("Count =", count)
    print("Average =", total / count)
Flowchart step outline (use correct symbols when drawing)
  1. Start (terminator)
  2. Input threshold (parallelogram)
  3. Initialise total ← 0, count ← 0 (process)
  4. FOR each value in numbers (loop structure)
  5. Decision: value > threshold? (diamond)
  6. If Yes: total ← total + value; count ← count + 1 (process)
  7. Loop back to next value (arrow)
  8. Decision: count = 0? (diamond)
  9. If Yes: output message; No: output count and average (parallelograms)
  10. End (terminator)

Amending an algorithm: small, safe changes

Exams commonly ask you to amend rather than rewrite: e.g. “Modify the algorithm so it also outputs the maximum value above the threshold.” The best approach is to add the minimum number of statements necessary, preserving structure and style.

Goal: also output the maximum of the values counted. Strategy: introduce a maxAbove variable and set it when the first qualifying value is found.

// CAIE Pseudocode (amendment)
DECLARE maxAbove : INTEGER
DECLARE foundAny : BOOLEAN ← FALSE
...
IF numbers[i] > threshold THEN
  total ← total + numbers[i]
  count ← count + 1
  IF foundAny = FALSE THEN
    maxAbove ← numbers[i]
    foundAny ← TRUE
  ELSE
    IF numbers[i] > maxAbove THEN
      maxAbove ← numbers[i]
    ENDIF
  ENDIF
ENDIF
...
IF count = 0 THEN
  OUTPUT "No values above threshold"
ELSE
  OUTPUT "Count = ", count
  OUTPUT "Average = ", total / count
  OUTPUT "Max above threshold = ", maxAbove
ENDIF

Fault: a FOR loop uses TO LENGTH(array) causing an out-of-range index. Fix: change to TO LENGTH(array) − 1 or loop directly over items.

// CAIE Pseudocode (fix off-by-one)
FOR i ← 0 TO LENGTH(numbers) - 1
  // safe access: numbers[i]
NEXT i

Goal: Searching for a target should stop as soon as it is found. Strategy: use a found flag with a loop guard to prevent unnecessary iterations.

// CAIE Pseudocode (early exit linear search)
DECLARE found : BOOLEAN ← FALSE
DECLARE i : INTEGER ← 0
WHILE i < LENGTH(numbers) AND found = FALSE DO
  IF numbers[i] = target THEN
    found ← TRUE
  ELSE
    i ← i + 1
  ENDIF
ENDWHILE
IF found = TRUE THEN
  OUTPUT "Found at index ", i
ELSE
  OUTPUT "Not found"
ENDIF

Deep Dive: Designing before coding

When time is limited, jumping straight into code seems faster, but it often leads to mistakes. A quick flowchart sketch or short pseudocode draft helps you choose sensible variables and control structures. Ask yourself: What are my inputs and outputs? What must be true before the loop (initialisation), and what must change each iteration so it terminates? Which boundary values should I test first? These questions prevent common errors like infinite loops and wrong averages.

Key terminology

  • Sequence: instructions executed in order.
  • Selection: choosing a path using a condition (e.g. IF ... THEN ... ELSE).
  • Iteration: repeating a block (e.g. FOR, WHILE, REPEAT ... UNTIL).
  • Accumulator: a variable that keeps a running total (e.g. total).
  • Flag: a BOOLEAN variable used to record that an event has happened (e.g. found).
  • Amend: to change an algorithm minimally to correct or extend it without breaking existing behaviour.

 Key Takeaways

  • Express the same logic in pseudocode, program code, and a flowchart while keeping inputs, processes, and outputs aligned.
  • Use correct CAIE pseudocode conventions: declared types, INPUT/OUTPUT, for assignment, and precise loop bounds.
  • When amending, make the smallest safe change: add variables like flags or accumulators where needed and preserve structure.
  • Prevent common faults by planning initialisation, loop termination, and boundary tests before coding.
  • For searches and passes over data, consider efficiency improvements such as early exit and tracking extrema while iterating.