2.1.2 Designing, creating and refining algorithms · Paper 2
Planning algorithms: pseudocode, flowcharts and trace tables
Each section has pictures and things to try, then its key points and key words. Exam practice is at the end of the page.
You need to know
- Identify the inputs, processes and outputs for a problem
- Draw and read structure diagrams
- Create, read, fix, complete and improve algorithms using pseudocode, flowcharts, and the OCR Exam Reference Language or a programming language
- Spot syntax and logic errors and suggest fixes
- Use trace tables
- Nesting of selection and loops
You don't need to know
- OCR doesn't rule anything out here.
Inputs, processes and outputs
Key points
Inputs, processes and outputs
Before this: 2.1.1Algorithmic thinking
- Input: the data that goes into the program, e.g. typed by the user or read from a sensor.
- Process: what the program does with the data, e.g. calculations or decisions.
- Output: what comes out, e.g. a message on screen, a sound or a saved file.
- Working out the inputs, processes and outputs first is a good way to plan a program.
Key words
Say what each one means, then tap to check.
Structure diagrams
A structure diagram shows a problem broken down (decomposed) into smaller parts.
Key points
Structure diagrams
Before this: 2.1.1Decomposition
- A structure diagram shows a problem broken down into smaller parts, like a family tree.
- The whole problem goes in the box at the top.
- Each level down splits a part into smaller sub-parts.
- Read it from the top down and left to right.
- It is a picture of decomposition.
Key words
Say what each one means, then tap to check.
Flowchart symbols
Try it
Which flowchart symbol would you use?
- Start
- Ask the user for their name
- total = total + price
- Is the score over 50?
- Run the drawBoard sub program
- Display "Well done"
Key points
Flowcharts
Before this: 2.1.1Algorithmic thinking
- Terminal (rounded box): Start and Stop.
- Process (rectangle): a calculation or action, e.g. total = total + 1.
- Decision (diamond): a yes/no question with two ways out.
- Input/Output (slanted box): getting data in or showing data.
- Sub program (rectangle with double sides): runs a separate block of code.
- Line (arrow): shows which way the algorithm flows.
Key words
Say what each one means, then tap to check.
Following a flowchart
Start at the terminal at the top and follow the lines. At each decision, answer the question with the data you have, then take the Yes or No line.
Try it
Pick an age, then follow the cinema ticket flowchart. You decide at each diamond.
Pseudocode
Pseudocode writes an algorithm in short, code-like steps. It doesn't have to follow any one language's rules exactly.
The exam uses the OCR Exam Reference Language. You can answer in it, or in a high-level language like Python.
OCR Exam Reference Language
score = int(input("Score?")) if score >= 50 then print("Pass") else print("Try again") endif
Python
score = int(input("Score?")) if score >= 50: print("Pass") else: print("Try again")
Key points
Pseudocode and the OCR reference language
Before this: 2.1.1Algorithmic thinking
- Pseudocode is a way of writing an algorithm in code-like steps, without worrying about exact rules.
- OCR use their own pseudocode called the Exam Reference Language in exam papers.
- Some Paper 2 questions must be answered in the OCR Exam Reference Language or a high-level language such as Python.
- Small mistakes like a missing colon don't lose marks if the logic is clear.
- The table below shows the main differences from Python.
| What | Python | OCR reference language |
|---|---|---|
| Output | print("Hi") | print("Hi") |
| Input | name = input("Name?") | name = input("Name?") |
| If | if x > 5: | if x > 5 then … endif |
| Else if | elif | elseif |
| For loop | for i in range(1, 11): | for i = 1 to 10 … next i |
| While loop | while x < 5: | while x < 5 … endwhile |
| Do until | no direct version | do … until x == 5 |
| MOD, DIV, power | % // ** | MOD DIV ^ |
| Length | len(word) | word.length |
| Comment | # note | // note |
Nesting
Before this: 2.2.1Selection (if)2.2.1Count-controlled loops (for)
- Nesting means putting one construct inside another.
- You can nest an if inside a loop, a loop inside a loop, or an if inside an if.
- Indentation shows which code is inside which block.
- Each nested block must be closed properly, e.g. endif before next i.
A loop inside a loop (Python)
for row in range(1, 4): for col in range(1, 4): print(row * col)
The same in OCR reference language
for row = 1 to 3 for col = 1 to 3 print(row * col) next col next row
Key words
Say what each one means, then tap to check.
Trace tables
A trace table follows an algorithm line by line, writing down each variable's value when it changes. It helps you find logic errors and work out what a program outputs.
- One column for each variable, plus one for output
- Each time round a loop usually starts a new row
- Only write a value when it changes
total = 0 for i in range(1, 4): total = total + i print(total)
Key points
Trace tables
Before this: 2.2.1Count-controlled loops (for)
- A trace table records the value of each variable as the algorithm runs, line by line.
- Make one column for each variable, plus one for the output.
- Add a new row each time a value changes.
- Trace tables help you find logic errors and work out what an algorithm does.
Key words
Say what each one means, then tap to check.
Common errors
Logic errors to look for: a loop that runs one time too many or too few, < where it should be <=, a total reset inside a loop, and maths done in the wrong order.
Try it
Each program has one logic error. Tap the line that's wrong.
1. This should print the numbers 1 to 5.
count = 1 while count < 5: print(count) count = count + 1
2. This should print the average of three scores.
a = 6 b = 8 c = 10 average = a + b + c / 3 print(average)
Key points
Completing and fixing algorithms
Before this: FlowchartsPseudocode and the OCR reference language
- Exam questions often ask you to fill in gaps or find and fix errors in an algorithm.
- Common mistakes: using > instead of >=, loops running one time too many or too few, and resetting a total inside a loop.
- Check that every variable is given a value before it is used.
- Check that input is cast to a number before doing maths with it.
- A trace table is a good way to find where it goes wrong.
Syntax and logic errors
Taught in more depth on: 2.3.2Testing: finding and fixing errors
- A syntax error breaks the rules of the language, so the program won't run.
- Examples: a missing bracket, pritn instead of print, or a missing endif.
- A logic error means the program runs but gives the wrong result.
- Examples: > instead of >=, adding instead of multiplying, or a loop running one time too few.
- The IDE finds syntax errors for you. Logic errors are found by testing and trace tables.
Key words
Say what each one means, then tap to check.
Also useful: Syntax and logic errors
Exam practice
19 questions in the style of the OCR exam, 53 marks in total. Try each one, then check the mark scheme.
Inputs, processes and outputs
Identify one input, one process and one output.
Hint
The input is what the user enters. The process is the maths. The output is what is shown.
Identify means pick out or name the right thing. A short answer is enough.
3 marks: make 3 separate points.
Show mark scheme
- Input: number of adult tickets / number of child tickets.
- Process: multiply each number by its price / add the costs together.
- Output: the total cost.
Don't accept: Input: "the price". The user doesn't type the price in. It is stored in the program.
1 mark for each.
State the input and the output.
Hint
What does the program read every minute? What does it control?
State means give a short, clear answer. You don't need to explain it.
2 marks: make 2 separate points.
Show mark scheme
- Input: the temperature (from a sensor).
- Output: switching the fan on (or off).
1 mark for each.
Structure diagrams
Hint
It is a picture of decomposition.
State means give a short, clear answer. You don't need to explain it.
1 mark: one answer is enough.
Show mark scheme
- To show how a problem is broken down / decomposed into smaller parts.
Draw a structure diagram for this program.
Hint
Put the whole program in the top box. Then add one box for each main part, and split Pay into its two smaller parts.
Draw means use the correct symbols, and label them.
4 marks: check every part before you finish.
Show mark scheme
- Top box: Online order (or similar).
- Three boxes below it: Log in, Choose products, Pay.
- Two boxes below Pay: Enter card details, Confirm order.
- Boxes joined correctly by lines.
Also accept: Boxes and lines in any layout, as long as the levels are clear.
Don't accept: A flowchart. A structure diagram shows the parts of a problem, not the order of the steps.
Example answer
Online order ├── Log in ├── Choose products └── Pay ├── Enter card details └── Confirm order
1 mark for each point.
Flowchart symbols
Hint
It is the shape with two ways out: Yes and No.
Name means a word or a short phrase is enough.
2 marks: make 2 separate points.
Show mark scheme
- Decision.
- Asks a yes/no question / a condition, with two possible paths.
1 mark for the name, 1 mark for what it is used for.
Hint
Follow the flowchart from Start. What is input? What question is in the diamond? What is output on each path?
Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.
4 marks: each part that works earns marks, even if the whole thing isn't perfect.
Show mark scheme
- Input the mark.
- Check if mark >= 50 (or > 49).
- Output Pass if true…
- …otherwise output Fail.
Don't accept: mark > 50. A mark of exactly 50 would then fail.
Example answer
mark = int(input("Enter mark")) if mark >= 50 then print("Pass") else print("Fail") endif
1 mark for each point. Python is fine too.
Pseudocode
Hint
A number is even if dividing it by 2 leaves no remainder. Which operator gives the remainder?
Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.
3 marks: each part that works earns marks, even if the whole thing isn't perfect.
Show mark scheme
- Input a number (cast to integer).
- Use MOD 2 to check the remainder.
- Output Even / Odd correctly.
Also accept: Checking num MOD 2 == 1 for odd.
Don't accept: num / 2 == 0. That only checks whether the number is 0, not whether it is even.
Example answer
num = int(input("Enter a number")) if num MOD 2 == 0 then print("Even") else print("Odd") endif
1 mark for each point. Python is fine too (use % instead of MOD).
for i in range(1, 6): print(i * 2)
Hint
In OCR's language, range(1, 6) becomes for i = 1 to 5. Don't forget how the loop ends.
Rewrite means write the code again with the change made. Keep everything else working.
2 marks: each part that works earns marks, even if the whole thing isn't perfect.
Show mark scheme
- for i = 1 to 5 … next i
- print(i * 2) inside the loop.
Don't accept: for i = 1 to 6. range(1, 6) stops before 6.
Example answer
for i = 1 to 5 print(i * 2) next i
1 mark for each point.
Hint
Nested means one inside another. Selection means an if.
State means give a short, clear answer. You don't need to explain it.
1 mark: one answer is enough.
Show mark scheme
- An if statement inside another if statement.
Hint
What do you need to keep track of, and what should it start at? Which part repeats 5 times, and where does the check go?
Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.
4 marks: each part that works earns marks, even if the whole thing isn't perfect.
Show mark scheme
- Loop that runs 5 times.
- Input a number inside the loop.
- If the number is > 10, add 1 to a count.
- Count set to 0 before the loop and output after the loop.
Also accept: A while loop with a counter, as long as it runs exactly 5 times.
Don't accept: num >= 10. A number equal to 10 is not bigger than 10.
Example answer
count = 0 for i = 1 to 5 num = int(input("Enter a number")) if num > 10 then count = count + 1 endif next i print(count)
1 mark for each point. Python is fine too.
Trace tables
x = 1 y = 10 while y > x x = x * 2 y = y - 2 endwhile print(x, y)
Hint
Check the condition before each time round the loop. Stop as soon as y > x is False.
Complete means fill in every gap.
4 marks: check every part before you finish.
Show mark scheme
- x = 2, y = 8
- x = 4, y = 6
- x = 8, y = 4
- Output: 8 4
Don't accept: Output 16 2. The loop stops once y is no longer bigger than x, which happens when x = 8 and y = 4.
Example answer
| x | y | Output |
|---|---|---|
| 1 | 10 | |
| 2 | 8 | |
| 4 | 6 | |
| 8 | 4 | |
| 8 4 |
1 mark for each correct row after the first
total = 0 for i = 1 to 5 if i MOD 2 == 1 then total = total + i endif next i print(total)
Hint
Which values of i make i MOD 2 equal 1? Add those up.
State means give a short, clear answer. You don't need to explain it.
2 marks: make 2 separate points.
Show mark scheme
- 9
- It adds the odd numbers 1 + 3 + 5.
1 mark for 9, 1 mark for showing working
Common errors
01 total = 0 02 for i = 1 to 3 03 total = 0 04 num = int(input("Enter a number")) 05 total = total + num 06 next i 07 print("Average: " + str(total / 2))Give the line number of each error and how to fix it.
Hint
Trace it with 3, 6 and 9. The average should be 6. Where does it go wrong?
Give means give a short, clear answer. You don't need to explain it.
4 marks: make 4 separate points.
Show mark scheme
- Line 03.
- Remove it / total should not be reset inside the loop.
- Line 07.
- Divide by 3 instead of 2.
Don't accept: Line 01. Setting total to 0 before the loop is correct.
1 mark for each line number, 1 mark for each fix.
password = input("Enter password") while password ______ "Secret1" print("Wrong, try again") password = ______ endwhile print("Welcome")
Hint
The loop should keep going while the password is wrong. How do you write "not equal to"? And the user must be asked again inside the loop.
Complete means fill in every gap.
2 marks: check every part before you finish.
Show mark scheme
- !=
- input("Enter password")
Also accept: <> for the first gap.
1 mark for each gap.
Mixed questions on the whole page
Hint
The inputs are what the user enters. The process is the calculation. The output is what is shown.
Identify means pick out or name the right thing. A short answer is enough.
3 marks: make 3 separate points.
Show mark scheme
- Inputs: length and width.
- Process: area = length × width.
- Output: the area.
Don't accept: Putting the calculation as an input or output. Working out the area is the process.
1 mark for each.
Hint
Think about the shape of each one, what goes inside it, and how many lines come out of it.
Describe means say what it is like or what happens. Make each point a separate fact.
2 marks: make 2 separate points.
Show mark scheme
- A process symbol (rectangle) shows an action or calculation, e.g. total = total + 1, and has one way out.
- A decision symbol (diamond) asks a yes/no question and has two ways out (Yes and No).
1 mark for each.
x = 1 total = 0 while x < 4 total = total + x * 2 x = x + 1 endwhile print(total)
Hint
Make columns for x, total and output. Check x < 4 before each time round the loop.
Complete means fill in every gap.
4 marks: check every part before you finish.
Show mark scheme
- total becomes 2 when x is 1, then x becomes 2.
- total becomes 6, then x becomes 3.
- total becomes 12, then x becomes 4.
- Output: 12.
Also accept: The same values in a different layout, e.g. a new row each time a value changes.
Don't accept: Output 20. The loop stops when x is 4, so 4 is never added.
1 mark for each point.
a = int(input("First number")) b = int(input("Second number")) average = a + b / 2 print(average)
Hint
Try it with 4 and 6. The average should be 5. What does the code give? Which happens first, + or /?
Show mark scheme
- Line 3: only b is divided by 2.
- Corrected: average = (a + b) / 2.
1 mark for the error, 1 mark for the fix.
Hint
Ask for the hours first. Then use if / else: one cost for 2 hours or less, a calculation for more. Test it: 5 hours should cost £9.
Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.
4 marks: each part that works earns marks, even if the whole thing isn't perfect.
Show mark scheme
- Inputs the hours (as a number).
- Checks whether hours <= 2.
- If so, cost = 3…
- …otherwise cost = 3 + (hours - 2) * 2.
- Outputs the cost.
Also accept: Checking hours > 2 first, as long as both costs are worked out correctly.
Don't accept: cost = 3 + hours * 2. This also charges £2 for the first 2 hours.
Example answer
hours = int(input("Hours stayed")) if hours <= 2 then cost = 3 else cost = 3 + (hours - 2) * 2 endif print("Cost: £" + str(cost))
1 mark for each point, up to 4. Python is fine too.