At school photo day, how would you tell a computer which of two pupils is taller?
At school photo day, you're asked to put the taller pupil at the back. That's easy when you can see them. What instructions would you give a computer, and what should it say if they're the same height?
To tell who is taller, you need A's height, one hundred and sixty-four centimetres, and B's height, one hundred and fifty-eight centimetres. You can leave out their jumper colours. Keeping the details that matter and leaving out unnecessary ones is called abstraction. With both heights in centimetres, you can see A is taller.
Break the task into smaller parts: read, compare and report. This is decomposition. Start by reading both heights. If A is taller, report A. Otherwise, test whether A is shorter. If so, report B. If both answers are no, report the same height. Then stop.
Both pupils are one hundred and sixty centimetres tall. The first test asks whether A is taller than B. The second asks whether A is shorter than B. Pick the result the steps should report.
An algorithm is a sequence of steps you can follow to complete a task. This pseudo-code represents the same method as the flowchart. Read both heights as inputs. The comparisons are the processing. Output A if A is taller, B if A is shorter, or ‘Same height’ if neither test succeeds.
Trace the method with both heights set to one hundred and sixty-two centimetres. The first test is false because one hundred and sixty-two is not greater than itself. The second test is false too, so you reach the final case and report the same height.
This time A is one hundred and fifty-five centimetres tall and B is one hundred and sixty-three. Trace both comparisons in order, just as you did for the tie. Pick the output you would get.
Enter two whole-number heights in centimetres. The if line tests whether A is taller and prints ‘A is taller’ if true. Otherwise, elif tests whether A is shorter and prints ‘B is taller’ if true. If both tests are false, as with two inputs of one hundred and sixty-two, else prints ‘Same height’.
A common mistake is saying an algorithm has to be written in Python. The comparison steps are the method. The Python code is one program that carries them out. You could write a different program that follows the same steps.
An exam question might ask, ‘Describe the purpose of this algorithm.’ The trace table follows one run. You record each height, then the first test is false and the second is true, so the program prints ‘B is taller’. Read the other branches too: the method reports the taller pupil or a tie.