Purpose of Simple Algorithms (AQA GCSE Computer Science): Revision Notes
Purpose of simple algorithms
Understanding what algorithms do
When you're given an algorithm, the first thing you need to figure out is what it's actually supposed to do. This means examining its behaviour and finding out what problem it solves or what task it performs.
For very straightforward algorithms, you can often work out their purpose just by looking at the code directly. This approach works well when the algorithm follows a familiar pattern or when the steps are clear enough that you can easily see what's happening.
However, as algorithms become more complicated, simply reading through the code isn't always enough to understand what's going on. You need a more systematic approach to track what's happening at each step.
Using trace tables for complex algorithms
When visual inspection becomes too difficult, a trace table is an incredibly useful tool. Think of it as a detailed record that helps you follow an algorithm's execution step by step.
A trace table works by recording:
- Each line of code as it executes
- The values of all variables at each step
- Any outputs the programme produces
- Comments explaining what's happening
By filling in this table systematically, you can track how the algorithm processes data and see exactly what it does at each stage. This method helps you understand both individual lines of code and the algorithm's overall purpose.
Worked example: Stock management system
Let's look at how trace tables work in practice using a stock management algorithm. This algorithm helps decide whether to reorder items based on current stock levels.
Worked Example: Stock Management Algorithm
| Line | ItemCode | StockLevel | ReorderLevel | Output | Comments |
|---|---|---|---|---|---|
| 01: ItemCode ← USERINPUT | 009 | Item code 009 entered by user | |||
| 02: StockLevel ← USERINPUT | 009 | 8 | Stock level of 8 entered by user | ||
| 03: ReorderLevel ← USERINPUT | 009 | 8 | 3 | Reorder level of 3 entered by user | |
| 04: IF StockLevel ≤ ReorderLevel THEN | 009 | 8 | 3 | 8 is NOT smaller than or equal to 3, so line 7 executed next | |
| 07: OUTPUT "Stock not ordered" | 009 | 8 | 3 | 'Stock not ordered' | Correct output printed |
The trace table above shows how the algorithm processes information about item 009:
- Input phase: The user enters ItemCode (009), StockLevel (8), and ReorderLevel (3)
- Decision phase: The algorithm checks if the stock level is less than or equal to the reorder level
- Output phase: Since 8 is greater than 3, it outputs "Stock not ordered"
This systematic tracking reveals the algorithm's purpose: to automatically determine when stock needs to be reordered based on current inventory levels.
Important points about trace tables
Variable persistence: Once a variable gets a value, it keeps that value throughout the programme unless it's changed. That's why you'll see the same ItemCode (009) repeated in multiple rows - it doesn't disappear after being entered once.
Step-by-step tracking: Every single line of the algorithm gets recorded, even if it seems obvious. This thoroughness ensures you don't miss any important logic or calculations.
Testing different scenarios: You could run this same algorithm with different values (like when stock is below the reorder level) to confirm it works correctly in all situations.
Exam tips
Exam Strategy Tips:
- Always label your trace table columns clearly - include Line, variable names, Output, and Comments
- Show every step - don't skip lines even if they seem simple
- Write clear comments explaining what each step does
- Test edge cases - what happens when values are equal or at boundaries?
- Check your logic - make sure the algorithm's behaviour matches its intended purpose
Remember!
Key Points to Remember:
- Algorithm purpose can be found by examining what the algorithm actually does
- Simple algorithms can often be understood through visual inspection
- Complex algorithms need systematic analysis using trace tables
- Trace tables record variables, outputs, and execution steps
- Variable values persist throughout programme execution unless explicitly changed