Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Algorithms, pseudocode and trace tables

After this lesson: I can break a problem into steps and trace what a program does.

50 min lesson1 commandEveryday Linux commands lab: the thirty commands engineers type from memory

Every program starts as a list of steps

An algorithm is a list of steps that solves a problem, and every program starts as one. If you can write the steps in plain words and follow them by hand, you can write the program.

Take a teacher who wants the average of three test marks. She does not need a computer to see the steps: add the marks, then divide by three.

By the end of this lesson

Note

I can break a problem into steps and trace what a program does.

Key words

WordWhat it meansExample
Algorithma list of steps, in order, that solves a problem
Decompositionsplitting a big problem into small ones
Pseudocodesteps written in plain words that look a little like code
Variablea name that holds a valuetotal
Programsteps written in a language a computer can follow
Trace tablea table that records each variable after each step

The cells students fill in are left blank in this preview.

An algorithm is steps in a fixed order

An algorithm has three features. Each step is clear enough that anyone can follow it. The steps come in a fixed order. And the list ends with an answer.

"Make it better" is not a step. "Add 1 to the total" is a step. A computer cannot guess what you mean, so every step must be exact.

Worked example: an algorithm for an average

StepWhat you doWith the marks 7, 5 and 9
1Start the total at 0total is 0
2Add the first marktotal is 7
3Add the second marktotal is 12
4Add the third marktotal is 21
5Divide by the number of marks21 divided by 3 is 7

The cells students fill in are left blank in this preview.

Check the answer with Python

Command
python3 -c "print((7 + 5 + 9) / 3)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Decomposition splits a big problem into small ones

Most real problems are too big to solve in one go. Decomposition means cutting a problem into smaller parts, and then solving each part alone.

A results report is one example. The parts are: read the marks, add them up, count them, divide, and show the answer. Each part is easy.

Pseudocode is a plan written in plain words

Pseudocode is a plan between a sentence and a program. It uses short, exact lines, but it does not follow the rules of any language, so no computer can run it.

Writing pseudocode first lets you check the steps before you worry about spelling and symbols.

The average as pseudocode and as Python

PseudocodePython
set total to 0total = 0
add 7 to totaltotal = total + 7
add 5 to totaltotal = total + 5
show total divided by 3print(total / 3)

The cells students fill in are left blank in this preview.

A trace table follows each line

A trace table is how you work out what a program does without running it. Draw a column for each variable. Then go down the program one line at a time and write the new value after every line.

Finding a bug is the most useful job for a trace table. The row where the value goes wrong shows you the faulty line.

Worked example: trace the program

After this linemarktotal
total = 0no value yet0
mark = 770
total = total + mark77
mark = 557
total = total + mark512
mark = 9, then total = total + mark921

The cells students fill in are left blank in this preview.

The same trace, printed by Python

Code: python
# Three marks, added one at a time. Read it like a trace table.
total = 0
mark = 7
total = total + mark
print("after 7:", total)
mark = 5
total = total + mark
print("after 5:", total)
mark = 9
total = total + mark
print("after 9:", total)
print("average:", total / 3)
# Change the last mark from 9 to 12, then run it again.

A common mistake

Warning

Many people fill in a trace table from what the program is meant to do. A trace follows the lines exactly, even when the program is wrong. Read one line, write one row, and ignore what you hope will happen.

Going further

On paper, write an algorithm for finding the largest of three numbers. Use pseudocode, and keep each line to one action.

Then trace it with the numbers 4, 9 and 6. Draw a column for each variable and write a row after every line. Say what the answer should be before you start.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.