Solving a problem step by step: analyse it, write an algorithm as a flowchart or pseudocode, code it, test it and debug it, tracing every step with values.
| Flowchart symbols | oval: start or stop; parallelogram: input or output; rectangle: process; diamond: decision |
| Lines and joins | arrow: the direction of flow; small circle: a connector |
| Swap A and B | SET temp = A, SET A = B, SET B = temp |
| Digits of N | N MOD 10 is the last digit and N DIV 10 drops it; R = R × 10 + N MOD 10 reverses N |
| GCD (Euclid) | WHILE B ≠ 0: R = A MOD B, A = B, B = R; then A is the GCD (48 and 18 → 6) |
| Prime check of N | up to N − 1: N − 2 divisions; up to √N: (whole part of √N) − 1 divisions |
| Counting a loop | list the values I takes; the loop stops at the first value that fails the test |
Trace this pseudocode.
1 INPUT N (N = 10)
2 SET S = 0
3 SET I = 2
4 WHILE I <= N
5 SET S = S + I * I
6 SET I = I + 3
7 END WHILE
8 PRINT S
(a) What does it print? (b) How many times does the loop run? (c) What is I when the loop stops?
Working: I: 2, 5, 8, then 11 > 10
Answer: (a) 93; (b) 3; (c) 11