OCR H446, section 2.1.3 (b)
Once the problem is understood (2.1.3 (a)), the next step is to decide what the solution will be made from.
Components of a solution
| Component | What it provides |
|---|---|
| Inputs | How data is collected, and how it is validated |
| Data and variables | What is stored, with what names, types and structures |
| Processing steps | The calculations that turn inputs into outputs |
| Decisions (selection) | Where the path depends on a condition |
| Repetition (iteration) | Steps that must be done many times |
| Subroutines | Named parts that do one job each (2.1.3 (d)) |
| Outputs | How results are presented |
| Error handling | What happens with invalid data or failures |
| Storage | Files or databases used to keep data |
| User interface | How the user interacts |
| Algorithms | Standard methods that can be used (searching, sorting) |
| Existing components | Libraries or code that can be reused (2.1.2 (d)) |
The three basic programming constructs that every algorithm is made from are sequence, selection and iteration (2.2.1 (a)).
From problem components to solution components
| Problem component | Solution component |
|---|---|
| An input from the user | An input() statement with validation |
| Data that doesn't change (prices, rates) | Constants |
| A collection of items | A list/array or dictionary |
| "If ... then ..." in the rules | A selection statement |
| "For each ..." or "until ..." | A loop |
| A repeated calculation | A function |
| An output | A print() or display routine |
| A constraint (maximum of 40) | A validation check |
Planning the solution
Ways of showing a solution before you code it:
- Pseudocode or flowcharts (2.3.1 (a)).
- A list of data items with name, type and purpose.
- A hierarchy chart of subroutines (2.1.3 (d)).
- Sketches of the user interface.
- Test plans that say what outputs to expect.
Data table
| Name | Type | Purpose |
|---|---|---|
ADULT |
integer constant | Price of an adult ticket |
CHILD |
integer constant | Price of a child ticket |
adults |
integer | Number of adult tickets requested |
children |
integer | Number of child tickets requested |
total |
real | Price to pay |
Worked example
Continuing the cinema booking from 2.1.3 (a), the solution components are:
- Input subroutine
get_tickets(): asks for the two numbers, repeats until they are valid (whole, not negative, a total from 1 to 40). This uses iteration and selection. - Calculation function
price(adults, children): returns the total, and applies the discount if there are 10 or more tickets. It uses selection. - Output procedure
show(total): displays the price with 2 decimal places. - Main program that calls these in order.
def get_tickets():
while True:
a = int(input("Adults: "))
c = int(input("Children: "))
if a >= 0 and c >= 0 and 1 <= a + c <= 40:
return a, c
print("Invalid, try again")
def price(adults, children):
total = adults * 9 + children * 6
if adults + children >= 10:
total = total * 0.85
return total
def show(total):
print(f"Total: £{total:.2f}")
adults, children = get_tickets()
show(price(adults, children))
Each component has one job and can be tested separately: price(2, 1) should give 24, and price(10, 0) should give 76.5.
Exam tip: when asked for the components of a solution, link each one to the problem: name the data, the decisions, the loops and the subroutines that your solution needs for that scenario.
Check yourself
Name four components of a solution
Any four of: inputs, variables and data structures, processing steps, selection, iteration, subroutines, outputs, validation and error handling, storage, user interface.
Which solution component would deal with a rule that says "if the customer is under 16"?
A selection statement (an if statement).
Why list the data items, with their types, when planning a solution?
It makes clear what must be stored and how, so that the right variable types and data structures are chosen before coding.
Give two ways of showing a planned solution before coding it
Any two of: pseudocode, a flowchart, a hierarchy chart, a data table, interface sketches.