B1.1.4 · Approaches to computational thinking
Tracing flowcharts
By the end you can
Read a flowchart made of loops, decisions and input and output, and trace it with a trace table.
An algorithm written as numbered steps is easy to write and hard to see. A draws the same algorithm as boxes and arrows, so the decisions and the loops stand out as shapes. It is also the quickest way to check that an algorithm does what you think, because you can follow it with a finger, one box at a time. Doing that carefully, and writing down what happens to every variable, is called tracing.
The symbols#
Flowcharts use a small set of standard shapes, so that anyone can read someone else's chart. A chart runs from the top of the page downwards, and from left to right.
Start / End (terminator). Marks the beginning and the end of the algorithm or of a sub-process.
Process. An operation that changes a value, such as TOTAL ← TOTAL + 1.
Input / Output. Reading data in (Input N) or writing it out (Output TOTAL).
Decision. A yes/no (true/false) question. Exactly two exits, each labelled.
Flowline. An arrow showing the order in which the boxes run.
Connector. Joins two parts of a chart without a long line across the page.
A decision box must hold a question that can only be answered yes or no, with each exit labelled. A loop is nothing special in a flowchart: it is an arrow that goes back to a box above it, usually to a decision. A subroutine (a named block of steps defined elsewhere) is drawn as one box and is traced as one step, then its own chart is followed separately.
Tracing with a trace table#
A is a table with a column for each variable (and one for output). Doing it on paper with chosen values is a . The method never changes:
- Make a column for every variable, plus one for output.
- Start at the Start box. Write down any starting values the chart sets.
- Follow the arrows one box at a time. For a process, work out the new value from the current values and write it in a new row.
- For a decision, test the condition with the current values and follow the matching exit. Write down the answer if it helps.
- Keep going until you reach End. Read the output off the table.
Here is the loop that you will meet in the first chart below, traced by the same engine that drives the figure. A starts at 10 and goes up in tens, and B starts at 100 and goes down in tens. The loop stops as soon as A is no smaller than B.
| When | A | B |
|---|---|---|
| Start | 10 | 100 |
| After pass 1 | 20 | 90 |
| After pass 2 | 30 | 80 |
| After pass 3 | 40 | 70 |
| After pass 4 | 50 | 60 |
| After pass 5 | 60 | 50 |
The loop body ran 5 times: the test A ≥ B was false after passes 0 to 4 and true after pass 5, where A = 60 and B = 50. The condition was tested six times, one more than the body ran. Mixing up those two numbers is the most common way to lose marks on a counting question.
A loop with one decision. How many times does it go round before A catches up with B?
Next box: Start
| # | Box | A | B | Output |
|---|---|---|---|---|
| Nothing has run yet. | ||||
A shaded cell is a value that just changed. In an exam you usually write only the changes, one row for each time round the loop.
Loops that read input#
Many algorithms ask for input inside the loop. In the guessing game the loop reads a guess, tests it, and goes back if it was wrong; each trip round reads the next value from the player. When you trace one, write the inputs down in order beforehand, and cross each off as it is used, so that you cannot read the same one twice.
The digit-sum chart uses two operators that exam questions love. N MOD 10 is the remainder after dividing by 10, which is the last digit; N DIV 10 is the whole-number result of dividing by 10, which chops that digit off. Starting from 472 the loop adds 2, then 7, then 4, and stops when N reaches 0.
| Box | N | TOTAL | Output |
|---|---|---|---|
| Input N | 472 | ||
| TOTAL ← 0 | 472 | 0 | |
| TOTAL ← TOTAL + N MOD 10 | 472 | 2 | |
| N ← N DIV 10 | 47 | 2 | |
| TOTAL ← TOTAL + N MOD 10 | 47 | 9 | |
| N ← N DIV 10 | 4 | 9 | |
| TOTAL ← TOTAL + N MOD 10 | 4 | 13 | |
| N ← N DIV 10 | 0 | 13 | |
| Output TOTAL | 0 | 13 | 13 |
A real algorithm: linear search#
The second chart in the figure is a linear search, the simplest way to find something in a list: look at the first item, then the next, and stop when you find it or run out. Try the three starting targets. The one at the end of the array takes the longest to find, and the one that is not there takes just as long to give up on. That is the cost you will put a name to in B2.4, and the trace table is how you count it.
Check yourself
Tracing flowcharts
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
5 questions · 16 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Trace the flowchart below and state what it outputs.
[4]START X ← 3 TOTAL ← 0 repeat while X > 0: TOTAL ← TOTAL + X X ← X − 1 OUTPUT TOTAL ENDTrace: Follow the program or process step by step, recording the values.
2.State the flowchart symbol used for a decision, and the number of ways out of it.
[2]State: Give a short answer, no explanation needed.
3.Trace this algorithm, and state how many times the loop body runs and the final values of A and B.
[3]A ← 3 B ← 40 repeat until A ≥ B: A ← A + 4 B ← B − 2Trace: Follow the program or process step by step, recording the values.
4.Construct a trace table for this algorithm with N = 905, and state the output.
[4]TOTAL ← 0 while N > 0: TOTAL ← TOTAL + N MOD 10 N ← N DIV 10 OUTPUT TOTALConstruct: Draw or write it out (a table, a diagram, a program).
5.Explain why a programmer might use a trace table when testing an algorithm.
[3]Explain: Give reasons: say what happens AND why, or how one thing causes another.
In a nutshell
- A flowchart draws an algorithm with standard symbols joined by arrows: a rounded terminator for Start and End, a rectangle for a process, a parallelogram for input or output, and a diamond for a decision with two labelled exits.
- Flowcharts run from the top down and left to right. A loop is an arrow that returns to an earlier box; a decision makes two paths.
- A trace table has a column for each variable and for output. Start with the initial values, then add a row each time a value changes, following the arrows exactly.
- Always trace the decision as well as the processes. Check the condition with the current values, take the matching exit, and write the final check that ends a loop.
- To count iterations, count how many times the loop body ran, not how many times the condition was tested (that is one more).
- A subroutine box is traced as one step: pass the values in, trace the subroutine on its own, and carry the returned value back.
