GoalReview pseudocode, loops and flowcharts: follow an algorithm, find and repair a bug, and write a new algorithm of your own.
New words
pseudocode · a plan for a program written in clear, structured words; a computer does not run itcount-controlled loop · a loop that repeats a set number of times, using a counterlogic error · a mistake that lets an algorithm run but gives the wrong resulttrace table · a table in which you record the value of each variable as you follow an algorithm
Explanation
Every algorithm is built from three constructs: sequence, selection and iteration. A sequence runs lines in order. A selection (IF … ENDIF) chooses between paths, and an iteration (a loop) repeats lines. A FOR loop has a counter, a start value and an end value, and its body runs once for each value from the start to the end. To find out what an algorithm does, follow it line by line and write the variables in a trace table. To change an algorithm, edit the smallest part that must change and then test it again. A bug that does not stop the algorithm is a logic error, and the usual places to look are the start and end values of a loop, the order of lines and the comparison in an IF.
Construct
Pseudocode shape
What it does
Sequence
line 1, line 2, line 3
runs the lines in order, once
Selection
IF … THEN … ELSE … ENDIF
chooses one path
Iteration
FOR counter ← 1 TO 5 … NEXT counter
repeats the body a set number of times
Worked examples
Pseudocode
1INPUT n
2total ← 03FOR k ← 1 TO n
4 INPUT price
5 total ← total + price
6NEXT k
7IF total > 50 THEN
8 total ← total - 59ENDIF
10OUTPUT "Pay ", total
Output
Pay 58
Following an algorithm with a loop. A kite seller adds up the prices and takes 5 off a total over 50. The user types 3, then the prices 20, 18 and 25. The running total is 20, 38 and then 63. The IF is true because 63 > 50, so the total becomes 58.
Pseudocode
1FOR day ← 0 TO 52 OUTPUT day * 33NEXT day
Repairing a loop. A sunflower grows 3 cm a day, and the algorithm should show its height on days 1 to 5. The loop below starts at 0, so it shows 0, 3, 6, 9, 12 and 15. That is six lines, and the first is not wanted, so the output is wrong. Starting the counter at 1 outputs 3, 6, 9, 12 and 15.
Python
1for day inrange(1, 6):
2print(day * 3)
Output
3
6
9
12
15
The same loop in Python. The counter starts at 1 and the loop must stop after 5, so the stop value of range is 6. Note that range stops before its stop value. The program prints 3, 6, 9, 12 and 15, one per line.
Class activity
Fault finders (no computer). Write a pseudocode algorithm of 5 or 6 lines that uses a FOR loop, for an invented job such as listing the days of a camp. Show it to your partner with the input you used. Your partner traces it in a trace table and checks the output. Now each of you adds one logic error to your own algorithm without saying where, for example changing the start value of the loop. Swap again, and find the bug with a new trace.
Practice
1
The user types 4. What does this algorithm output?
Pseudocode
1INPUT lanes
2FOR lane ← 1 TO lanes
3 IF lane = 3 THEN
4 OUTPUT "Lane ", lane, " closed"5 ELSE
6 OUTPUT "Lane ", lane, " open"7 ENDIF
8NEXT lane
It outputs four lines: Lane 1 open, Lane 2 open, Lane 3 closed and Lane 4 open. Only the counter value 3 makes the IF true.
2
Follow the flowchart. What is output? Then change one box so that it outputs 20, 40 and 60.
The counter takes 1, 2 and 3, so the output is 10, 20 and 30. To output 20, 40 and 60, change the output box to OUTPUT counter * 20. The counter box and the test stay the same.
3
Write pseudocode. The user types four temperature readings, one at a time. Output the lowest reading. What does your algorithm output for 9, 4, 7 and 5?
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): INPUT low / FOR t ← 2 TO 4 / [indent] INPUT temp / [indent] IF temp < low THEN / [indent] [indent] low ← temp / [indent] ENDIF / NEXT t / OUTPUT low. Check: The first reading is stored as the lowest so far. The later readings are 4, 7 and 5; only 4 is lower than 9. The output is 4.
4
Write this Python program in pseudocode, then say what it prints.
Python
1total = 02for n inrange(1, 5):
3 total = total + n * 24print(total)
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): total ← 0 / FOR n ← 1 TO 4 / [indent] total ← total + n * 2 / NEXT n / OUTPUT total. Check: range(1, 5) gives 1 to 4, so the pseudocode loop ends at 4. The total is 2 + 4 + 6 + 8, so it prints 20.