Algorithms
The eight steps of programming, what an algorithm is, flow charts, Nassi-Schneiderman diagrams and pseudo code, and the three building blocks: sequence, selection and iteration.
Based on: lecture 3 (algorithms) and the algorithm exercise sessions · About 60 minutes
Eight steps from problem to programImportant
Solving a problem with a computer follows a fixed route. Learn the steps in order: exam questions ask for them, and the algorithm lecture keeps referring to them by number.
- Problem definition: describe exactly the starting situation, the result you need and the link between both.
- Solution strategy: choose a method and the data you need; split the problem into smaller sub-problems (modules) and agree what each one does.
- Representation of the algorithm: write the solution as a flow chart, a Nassi-Schneiderman diagram or pseudo code.
- Coding: translate the finished algorithm into a programming language (Python, Java, C++ and many more).
- Compiling: detect syntax errors and translate into machine language (ones and zeros), with a compiler or an interpreter.
- Testing: the results must also be logically correct; compare with results obtained another way and test every different case.
- Documenting: everything that helps someone else understand, change, improve or extend the program.
- Maintenance: after a while the program has to be updated, and every change goes into the documentation.
What an algorithm isImportant
The word comes from the name of al-Khwarizmi, a Persian mathematician of the ninth century whose book on solving equations also gave us the word algebra. In the course an algorithm is a method to solve a problem with four characteristics:
- a sequence of actions, commands or rules,
- that starts from a given initial situation,
- and reaches a stated goal,
- and stops after a finite time.
On top of that, every step has to be unambiguous: everyone (and every computer) must understand them the same way. A recipe for pancakes or the steps to change a flat tyre are everyday algorithms; computers use them for routing network traffic, cryptography, planning, image and text processing.
Three ways to represent an algorithmImportant
| Representation | How it looks | Strong point |
|---|---|---|
| Flow chart | basic figures with the instructions inside, connected by arrows | easy to read, shows the flow visually |
| Nassi-Schneiderman diagram (block diagram, structogram) | nested rectangles without arrows; a selection is a split block, a loop a block that wraps its body | forces a clean structure: no jumping around |
| Pseudo code | structured text with keywords such as Read, Write, If, While, EndDo | closest to real code; easy to translate into Python |
Flow chart symbols used in the course: an oval for begin and end, a rectangle for a process step (an assignment), a parallelogram for input and output (Read, Write), a diamond for a choice with yes and no branches. The course adds one rule for clarity: every rectangle gets exactly one arrow in and one arrow out.
In pseudo code the assignment is written with an arrow: km ← mile × 1.609 means "km gets the value of mile times 1.609". The left-hand side receives the value of the right-hand side. A user-friendly algorithm first writes a message (Write 'Give distance in miles') so the user knows what to type.
Miles to kilometres in pseudo code
Problem definition: convert a distance in miles into kilometres; 1 mile = 1.609 km.
Solution strategy
Pseudo code
Test with 10 miles
This uses only three primitives: input (Read), assignment and output (Write).
Sequence, selection and iterationImportant
Every algorithm is built from three building blocks.
| Block | Variants | In words |
|---|---|---|
| Sequence | one step after the other | read, compute, write |
| Selection (conditional) | If Then; If Then Else; Case (several options, built from nested Ifs) | do something only when a condition is True |
| Iteration (loop, repetition) | While; Repeat (Until); For | repeat a block of actions |
| Loop | Condition tested | Runs at least once? | Stops when | Best for |
|---|---|---|---|---|
| While | before the body | no: can run 0 times | the condition becomes False | unknown number of repetitions |
| Repeat ... Until | after the body | yes: at least once | the condition becomes True | actions that must happen once before you can test |
| For X from E1 to E2 [Step E3] | counter compared with E2 | no (if E1 is past E2) | the counter passes E2 | a known number of repetitions; step is 1 by default |
Every Repeat loop can be rewritten as a While loop (run the body once before the While, or use a flag), and every For loop can be built with a While and a counter. The reverse is not always possible without extra tests, because a While may run zero times.
Sum of a series closed by zero
Read numbers until the user types 0, then write the sum. Series 7, 4, 3, 0.
Initialisation
Read the first number
While Number ≠ 0
Number = 0
Write 'The sum is ', Sum gives 14. The EndDo marks exactly which actions belong to the loop; forget to read inside the loop and you get an endless loop.
The cash dispenser exam questionImportant
A recurring exam question asks for a flow chart, a Nassi-Schneiderman diagram or pseudo code of an ATM. Split it into two parts, exactly as the solution strategy step recommends.
- Part 1, checking card and code: after the card is inserted, ask for the PIN in a user-friendly way. Allow three attempts with a counter. After three wrong codes, the card is swallowed and the transaction stops.
- Part 2, the menu: only when the code is correct (so an If around part 2). Show four options: withdraw money, change PIN, consult balance, return card. After options 1 to 3 the menu comes back (a loop); option 4 stops. The choice itself is a Case.
Counting attempts is the counter technique: start at 0, add 1 in every pass, and test the counter in the loop condition. The final exercise session stresses that this technique comes back in the exam. You can program the PIN part yourself in the Python control flow unit.
Key takeaways
- Eight steps: problem definition, solution strategy, representation, coding, compiling, testing, documenting, maintenance.
- An algorithm: a sequence of unambiguous actions from a starting situation to a goal in finite time.
- Represent it as a flow chart, a Nassi-Schneiderman diagram or pseudo code.
- Sequence, selection (If, If Else, Case) and iteration (While, Repeat, For).
- While tests first and can run 0 times; Repeat tests after and runs at least once.
All cards known
Reset the progress or pick another unit to keep practising.
Multiple choice (8)
Which is NOT a characteristic of an algorithm?
Which statement is correct?
In which step are syntax errors detected?
Which flow chart symbol stands for Read and Write?
A loop must allow at most three PIN attempts. Which technique fits best?
Sum ← 0; read Number; While Number ≠ 0: Sum ← Sum + Number. What is wrong?
For X from 2 to 10 Step 3 runs the body for:
What is a Nassi-Schneiderman diagram also called?
Exam-style open questions (2)
ATM in pseudo code
Write pseudo code for the cash dispenser: three attempts for the PIN, then a menu with withdraw, change PIN, balance and return card, where the menu returns after options 1 to 3.
Show a model answer outline
- Counter Attempts ← 0; Repeat: Write prompt, Read code, Attempts ← Attempts + 1 Until code correct or Attempts = 3.
- If code not correct: swallow card, stop.
- Else Repeat: show menu, Read choice, Case choice 1, 2, 3: module Until choice = 4; return card.
- Name the building blocks used: sequence, selection (If, Case), iteration (Repeat).
While or Repeat?
Rewrite this While loop as a Repeat loop and explain when the two give a different result: z ← 0; x ← 1; While x < 6: z ← z + x; x ← x + 1.
Show a model answer outline
- Repeat: z ← z + x; x ← x + 1 Until x ≥ 6.
- Both give z = 15 here, because the start value 1 satisfies the condition.
- If x started at 6 or more, While would run 0 times (z = 0) but Repeat once (z = x).
- A For x from 1 to 5 is also suitable because the number of repetitions is known.
LabLoop tracer: While versus Repeat
Type a series of numbers that ends with 0, choose the loop type and follow the trace table. Try a series that starts with 0.
LabFor loop counter
The pseudo-code For includes the end value when the counter reaches it exactly. Compare with Python range in the Python control flow unit.
LabFlow chart symbols and loops
Two short sorting games about the representation of algorithms.