Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Algorithms

What is an algorithm?
An algorithm is simply a set of instructions that are applied in order to a situation. The instructions take an input and produce an output. The same input will always produce the same output. It is important that algorithms are finite they do not loop forever. Example:
Line 10: Line 20: Line 30: Line 40: Line 50: Line 60: Line 70: Line 80: Line 90: Line 100: Read the set of numbers Order the numbers in ascending order If there is only one number in the list go to Line 70 If there are two numbers in the list go to Line 90 Delete the largest and smallest numbers Go to Line 30 Output the number End Output the mean of these two numbers End

Flow Diagrams
We can also show algorithms as flow diagrams. Example: Diamond-shaped boxes are used for questions which then determine future actions.

Oval boxes are used for starting and stopping and for inputting and outputting data.

Rectangular boxes are used for calculations or instructions.

What does this algorithm do? A: Works out the mean of a set of numbers

Tracing an Algorithm
When we trace an algorithm we work through by hand and record what we are doing. We often use a table to help. Trace the algorithm above for the set of numbers 8, 7, 3, 2, 5
Line 10 20 30 40 50 60 30 40 50 60 30 70 80 List 8, 7, 3, 2, 5 2, 3, 5, 7, 8 2, 3, 5, 7, 8 2, 3, 5, 7, 8 3, 5, 7 3, 5, 7 3, 5, 7 3, 5, 7 5 5 5 5 5 Numbers ordered More than one number More than 2 numbers Deleted largest and smallest Go to 30 More than one number More than 2 numbers Deleted largest and smallest Go to 30 One number so go to 70 Output 5 End Comments

Completing or amending an algorithm.


Completing or amending an algorithm involves finishing off an algorithm with a specific purpose, or adjusting one so that a problem is avoided. Example: This algorithm is intended to generate the Fibonacci Sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...
Line 10: Line 20: Line 30: Line 40: Line 50: Line 10: Line 20: Line 30: Line 40: Line 50: Line 60: Let A = 1 and B = 1 Print the value of A and the value of B Replace A with A + B Print the value of A Go to line 30 Let A = 1 and B = 1 Print the value of A and the value of B Replace A with A + B Print the value of A If A < 55 go to line 30 End

Amend the algorithm so that it stops after printing 10 terms:

Does the algorithm produce the intended sequence?

You might also like