2.1.2 Designing, creating and refining algorithms
An algorithm is a set of steps that solves a problem. Before you write a program you plan it. After you write it you test it and make it better. This topic covers how to plan an algorithm, how to show it, how to find mistakes in it and how to follow it by hand. All code is in the OCR Exam Reference Language.
Inputs, processes and outputs
Every program can be split into these three parts. Ask yourself three questions: what does the user give the program, what does the program do with it, and what comes out?
| Part | Example: a program that works out the area of a rectangle |
|---|---|
| Inputs | The length and the width |
| Process | Multiply the length by the width |
| Output | The area, shown on screen |
Spotting the inputs, processes and outputs first makes the rest of the design easier. You know which variables you need and what the program must print at the end.
Structure diagrams
A structure diagram is drawn as boxes in a tree. The whole problem is at the top, and each level below it is more detailed. You can show the same thing as an indented list. This list describes a diagram for a quiz game.
* Quiz game
* Set up
* Load the questions
* Enter the player name
* Play a round
* Ask a question
* Check the answer
* Update the score
* Finish
* Show the score
The top box is the whole program. The three boxes under it are the main sub-problems. The boxes at the bottom are small jobs. Each small job can become a sub program.
Ways to show an algorithm
You can write an algorithm in plain steps, as pseudocode, as a flowchart or in the reference language.
Pseudocode
Pseudocode has no fixed rules, so two people may write it differently. That is fine for planning. It is not accepted when a question asks you to write or refine an algorithm. In that case you must use the reference language or a high-level language.
The reference language
The reference language is the style used for every program printed in the exam. These are the main points.
* Keywords are lower case: if, then, else, elseif, endif, while, endwhile, for, next, procedure, function.
* Use == to compare and = to assign.
* A for loop includes its last value. for i = 0 to 9 runs ten times.
* Comments start with //.
* Arrays start at index 0.
// ask for a number and say if it is big
number = int(input("Enter a number"))
if number > 10 then
print("Big")
else
print("Small")
endif
Flowcharts
You need to know six parts of a flowchart.
| Symbol | Shape | Use |
|---|---|---|
| Terminal | A rounded oval (like a running track) | Start or end of the algorithm |
| Input/output | A parallelogram (a slanted rectangle) | Taking in data or showing a result |
| Process | A plain rectangle | A calculation or an assignment, such as total = total + 1 |
| Decision | A diamond with two arrows leaving it, labelled Yes and No | A question that has a true or false answer |
| Sub program | A rectangle with an extra vertical line inside each of its two sides | A call to a procedure or function |
| Line | An arrow | Shows the direction of flow from one symbol to the next |
Here is a flowchart in words. Start (terminal) goes to Input number (parallelogram), then to the decision "number > 10?" (diamond). The Yes arrow goes to Output "Big" (parallelogram). The No arrow goes to Output "Small" (parallelogram). Both arrows then join and go to End (terminal). It does the same job as the program above.
A loop in a flowchart is made by an arrow that goes back up to an earlier symbol. The decision diamond decides whether the loop goes round again or leaves.
Errors
| Type | What happens | Example |
|---|---|---|
| Syntax error | The program stops or will not start | if x > 5 with the word then missing |
| Logic error | The program runs but the output is wrong | Using > when it should be >= |
Syntax errors
Typical syntax errors are a missing then, endif, endwhile or next i, a misspelt keyword, using = when you need ==, and brackets that do not match. The fix is to correct the line so it follows the rules.
// syntax error: then is missing
if score >= 50
print("Pass")
endif
Fixed: add then at the end of the if line.
Logic errors
The program runs, so there is no warning. You find a logic error by testing and by tracing. Typical causes are the wrong operator, a loop that runs one time too many or too few, a missing bracket in a calculation, or the wrong variable.
a = 4
b = 5
c = 6
average = a + b + c / 3
print(average)
This prints 11, not 5. The division happens first, so c / 3 is 2. The fix is to add brackets: average = (a + b + c) / 3.
Trace tables
A trace table helps you find logic errors, work out what an algorithm does and check that it is correct. Follow the program one line at a time and do not skip a pass of a loop.
Worked example: a for loop
total = 0
for i = 1 to 4
n = i * 2
total = total + n
next i
print(total)
| i | n | total | Output |
|---|---|---|---|
| 0 | |||
| 1 | 2 | 2 | |
| 2 | 4 | 6 | |
| 3 | 6 | 12 | |
| 4 | 8 | 20 | |
| 20 |
The loop runs for i = 1, 2, 3 and 4, and then the program prints 20.
Worked example: a while loop
x = 20
count = 0
while x > 3
x = x DIV 2
count = count + 1
endwhile
print(count)
| x | count | x > 3 | Output |
|---|---|---|---|
| 20 | 0 | True | |
| 10 | 1 | True | |
| 5 | 2 | True | |
| 2 | 3 | False | |
| 3 |
DIV gives the whole number part of a division, so 5 DIV 2 is 2. When x is 2 the test is false, so the loop stops. The program prints 3.
Nesting selection and iteration
The inner structure is indented so you can see where it starts and ends. The inner loop runs completely for each pass of the outer loop.
Worked example: a selection inside two loops
for row = 1 to 3
for col = 1 to 2
if (row + col) MOD 2 == 0 then
print("X")
else
print("O")
endif
next col
next row
| row | col | row + col | Output |
|---|---|---|---|
| 1 | 1 | 2 | X |
| 1 | 2 | 3 | O |
| 2 | 1 | 3 | O |
| 2 | 2 | 4 | X |
| 3 | 1 | 4 | X |
| 3 | 2 | 5 | O |
MOD gives the remainder after division. An even total has a remainder of 0 when divided by 2, so it gives X. The outer loop runs 3 times and the inner loop runs 2 times for each of those, so there are 6 outputs: X, O, O, X, X, O.
Refining an algorithm
To refine an algorithm means to make it better after the first version. You might fix errors, make it shorter, make it easier to read or make it do fewer steps. For example, a row of separate if statements can often be replaced with if, elseif and else.
// first version: three tests every time
if mark >= 70 then
print("Distinction")
endif
if mark >= 50 AND mark < 70 then
print("Merit")
endif
if mark < 50 then
print("Pass")
endif
// refined version
if mark >= 70 then
print("Distinction")
elseif mark >= 50 then
print("Merit")
else
print("Pass")
endif
The refined version gives the same outputs. It is shorter, easier to read and does not repeat tests.
Writing and completing an algorithm
Follow these steps when you write an algorithm for a question.
- 1.Underline the inputs and the outputs.
- 2.Decide if you need a loop (count-controlled if you know how many times, condition-controlled if not) and if you need selection.
- 3.Write the code in the reference language.
- 4.Trace it with a small test value to check it.
Exam technique
* State or identify: one short answer, such as the name of a flowchart symbol or the line with an error.
* Describe: say what the algorithm does overall, or what a structure diagram shows.
* Explain: give a reason, such as why a logic error does not stop the program.
* Complete a trace table: write each change on a new row, follow every pass of a loop and put the output in the output column.
* Correct an algorithm: give the line number and the corrected line.
* Write, complete or refine an algorithm: use the reference language or a high-level language. Plain English and bullet points earn no marks. Check that you have then, endif, endwhile and next i, and that loops stop at the right value.