Determine algorithm purpose

Reading an Algorithm: Two Techniques

To determine the purpose of an algorithm, you can use two complementary techniques: visual inspection and trace tables. Visual inspection means reading the pseudocode carefully and reasoning about what it does - useful for very short algorithms. A trace table is a systematic method of tracking how each variable changes as you follow the algorithm step by step. Together these techniques allow you to work out exactly what an algorithm does without needing to run it on a computer.

Trace Table Rules

A trace table has one column for each variable in the algorithm, plus an OUTPUT column when the algorithm produces output. Apply these rules consistently:

  • Sequence - a run of plain assignment or output statements uses the same row. Record all updates on one line.
  • Selection or Iteration (IF, WHILE, FOR) - drop a line (start a new row) when you reach a control keyword.
  • Inside a branch or loop - if the body contains a sequence of statements, apply the sequence rule: record them on the same (dropped) line.
  • Only write a value when the algorithm explicitly changes that variable. Leave all other cells in that row blank.

Once the trace is complete, look at the OUTPUT column and the final variable values to state the algorithm's purpose.

Rules Applied: Three Algorithm Structures

All algorithms are built from three structures: sequence, selection (IF), and repetition (WHILE). The tabs below show each structure traced using the rules above.

Algorithm:

num1 ← 6
num2 ← 4
total ← num1 + num2
OUTPUT total

All four lines are sequence - so they share one row. Every variable is set, then output is produced, all on a single line.

num1num2totalOUTPUT
641010

Purpose: This algorithm adds two numbers and outputs their total.

Algorithm:

score ← 72
IF score >= 50 THEN
    OUTPUT "Pass"
ELSE
    OUTPUT "Fail"
ENDIF

Row 1: score ← 72 is sequence - recorded on one line.
Row 2: The IF keyword causes a line drop. The condition 72 >= 50 is true, so the THEN branch runs: OUTPUT "Pass" is the only statement in that branch (sequence inside) - recorded on the dropped line. The ELSE branch is not traced.

scoreOUTPUT
72
Pass

Purpose: This algorithm checks whether a score reaches 50 and outputs "Pass" or "Fail" accordingly.

Algorithm:

count ← 1
WHILE count <= 3
    OUTPUT count
    count ← count + 1
ENDWHILE

Row 1: count ← 1 is sequence.
Each iteration: The WHILE keyword causes a line drop. Inside the loop, OUTPUT count and count ← count + 1 are a sequence of two statements - both recorded on the same dropped line. This repeats for each pass through the loop.

countOUTPUT
1
21
32
43

When count becomes 4, the condition 4 <= 3 is false and the loop ends - no further output is produced.

Purpose: This algorithm outputs the numbers 1, 2, and 3 in ascending order.

 Key Takeaways

  • A trace table tracks how variables change step by step, letting you determine what an algorithm does.
  • Sequence statements share one row - do not drop a line between consecutive plain assignments or outputs.
  • IF, WHILE, and FOR keywords cause a line drop - start a new row each time a control structure is reached.
  • Sequential statements inside a branch or loop body are recorded on the same dropped line.
  • Only write a value in a cell if the algorithm explicitly changes that variable - all other cells in the row stay blank.
  • The OUTPUT column and the final variable values reveal the algorithm's overall purpose.