Representing Algorithms
Representing Algorithms
Not all the problems can be solved by computers. A problem that can be solved by computers has
these features:
The problem is clearly stated
The solution is clearly stated
There are logical steps that can get us from the problem to the solution.
Sometimes it is easy to work out the solution. In other cases, it can be difficult.
HOW CAN WE SOLVE A PROBLEM?
1. ABSTRACTION
The process of taking away or removing characteristics from a problem in order to do it to understand.
A man called Fred wishes to cross a 10 meter wide river with a wolf, a white goat and a bail of newly cut hay.
He has a small blue boat and oars, but unfortunately he can only take one thing across at a time. The problem
is, if he leaves the wolf and the goat alone together, the wolf will eat the goat, and if he leaves the goat with
the hay, the goat will eat the hay. They are currently all together on one side of the river, which we will call
bank B, and they want to get to the other side, called bank A. How does he do it?
First of all we should find out all the irrelevant details and ignore them. Then we can try to find the solution.
TASK 2 – BOOK (page 101)
1. Abstraction means removing unnecessary details. Explain in your own words how we know which
details are necessary or unnecessary?
2. Explain how abstraction helps with reusability.
3. A programmer was asked to write a program to work out the average weight of apples in an
orchard. What details can she leave out of the algorithm? What details would she include?
2. DECOMPOSITION
Decomposition is when you are breaking a problem into a number of sub-problems, so that each sub-
problem accomplishes an identifiable task, which might itself be further subdivided.
The main purpose of decomposition is to help you find your solution much easier!!
LET’S MAKE A LEMON PIE
Each part of the recipe acts as a Modularity allows the programmer to truckle
separate module! problems in a logical fashion
TIPS FOR A GOOD DECOMPOSITION
The solutions to the subproblems can be combined to solve the original problem.
ADVANTAGES OF DECOMPOSITION:
It saves time
Reduces complexity
Increasing reliability as the modules will have already been tested in another program.
It is easier to update or fix the program by replacing individual modules rather than larger amounts of
code
Avoids the increased possibility of data corruption often caused by using a large number of lines of code
Ensures that there is a logical structure in the program being written, which make the code more efficient
and easier to understand and modify.
DISADVANTAGES
The solutions to the subproblems might not combine to solve the original problem.
BUT
You use code to tell a computer what to do. Before you write code you need an algorithm.
An algorithm is a list of rules to follow in order to solve a problem.
Algorithms need to have their steps in the right order. Think about an algorithm for getting dressed
in the morning. What if you put on your coat before your jumper? Your jumper would be on top of
your coat and that would be silly! When you write an algorithm the order of the instructions is very
important.
HOW DO YOU USE ALGORITHMS?
People use lots of algorithms every day even if they don’t realise it. Choose from three problems and guess
what the algorithm might be!
We need to build
We need to make We can’t find the
a toy!
a cake! park!
Step
Add milk to the
Imagine if we missed out one of the steps or
1 blender
Step
reversed the order. We could end up switching on Put the lid on the
the blender with nothing in it. Or there could just 2 blender
be milk in the blender and no fruit. Step
Switch the blender
That wouldn't make a very tasty smoothie, would
it? In fact, it wouldn't make a smoothie at all! 3 on
Step
Add fruit to the
4 blender
TASK 3 – BOOK (page 99)
Write an algorithm that adds 2 number. The number should be inputted by the user
SEQUENCE
Sequence is a set of steps taken in order, one thing following after another.
Your commands should have the correct order otherwise your program will not be able to run.
For example:
1. INPUT first number
2. ADD first and second number together
This will not
3. INPUT second number run…
4. STORE as total
5. OUTPUT total
ASSIGNMENT & VARIABLE
Assignment is when you are saving an input with a name (in this instance: first number, second number)
We can set and/or re-set the value stored in a storage location. For example: the first number could be 5 and
the second number could be 6.
If we re-run the algorithm we could re-set the first number to be number 8 and the second number could be 2.
The program will run again and find the new solution.
What is an algorithm? Why do programmers create algorithms before they start to write program
code?
KEY WORDS
Abstraction is the process of removing unnecessary details from a problem to make it easier to solve it.
Decomposition is the process of breaking a problem into a number of sub-problems, so that each sub-problem
accomplishes an identifiable task, which might itself be further subdivided.
Algorithm is a sequence of steps that can be followed to complete a task and always terminates (runs in finite
time).
Sequence is a set of steps taken in order, one thing following after another.