GoalFollow, edit and correct a flowchart in which decisions choose which sub-routine to run.
New words
main program · the part of a program that decides what happens and calls the sub-routinesedit · to change a flowchart or program to add something or improve itunreachable · a path that can never be followed because of an earlier question
Explanation
In a longer program, the main flowchart is a kind of manager. It asks questions and calls the sub-routine that fits the answer. Each sub-routine does one job. To follow the program, trace the decisions and note which sub-routine is called, then trace that sub-routine. When you edit a program, you add one decision and one sub-routine and leave the others alone. When you correct a flowchart, check the order of the questions. An earlier question can swallow a case, and then a later path is unreachable. Test with a value on every boundary.
Worked examples
Sub-routine
What it outputs
cold_advice
Wear a coat, Drink something warm
hot_advice
Wear a hat, Drink water
mild_advice
Enjoy the day
These are the sub-routines of the weather flowchart. The main flowchart decides which one runs. Each one is a short flowchart of its own.
temp
temp < 10?
temp > 25?
Sub-routine that runs
4
yes
–
cold_advice
30
no
yes
hot_advice
18
no
no
mild_advice
10
no
no
mild_advice
25
no
no
mild_advice
Trace the weather flowchart with five inputs. The boundary values are 10 and 25. Neither of them is below 10 or above 25.
Adding a new case is easy because the sub-routines are separate. For a ‘windy’ warning you would add one new decision and one new sub-routine, and the three old sub-routines would not change at all.
Class activity
Weather cards (no computer): write the three sub-routine names on three cards and put each card’s two outputs on its back. Write temperatures from 0 to 40 on small slips. Draw the weather flowchart on a big sheet. A partner picks a slip, you place a coin on the flowchart and answer each diamond, and then you pick up the matching card and read its back. Try 10, 25 and 26. Now invent a fourth card called ‘freezing_warning’ for temperatures below 0, and decide where its decision must go. If you have a computer or tablet, draw your new flowchart in a drawing program.
Practice
1
The sub-routine small_portion shows ‘Give 50 g’ and large_portion shows ‘Give 120 g’. Trace the flowchart for weights 3, 5 and 8.
3: 3 < 5 is yes, so small_portion runs and shows Give 50 g. 5: 5 < 5 is no, so large_portion runs and shows Give 120 g. 8: no, so Give 120 g.
2
The flowchart should give hot advice for 30. It gives mild advice instead. Find the mistake and correct the flowchart.
Every temperature above 10 goes to mild_advice at the first question, so 30 never reaches the hot question. The hot path is unreachable (and 10 itself wrongly gets cold_advice). Fix: ask temp > 25? first, with its yes arrow going to run: hot_advice. On its no arrow ask temp < 10?: yes goes to run: cold_advice and no goes to run: mild_advice.
3
Edit the weather flowchart so that a temperature above 35 runs a new sub-routine called heat_warning instead of hot_advice. Describe what you change.
On the yes branch of temp > 25?, replace the call to hot_advice with a new diamond: temp > 35? Its yes arrow goes to run: heat_warning, and its no arrow goes to run: hot_advice. The other parts do not change.
4
Fill in the missing sub-routine for each temperature in the weather flowchart.