Unit 1-Algorithm Design
Unit 1-Algorithm Design
Unit 1
Writing Algorithms
Student will:
1. Learn the fundamentals of Algorithms
2. Learn the steps of software development
3. Learn the different types of Algorithms
2. DESIGN A SOLUTION/PROGRAM
1. Write the Algorithm (stepping, looping, and choosing)
2. I/O Screens, data files, network, etc.
1. ANALYSE THE PROBLEM
When you analyze a problem, you look at the problem in front of
you and decide what you need to do to solve the problem.
We need to define the problem:
2.DESIGN A SOLUTION/PROGRAM
Program design is the most important part in producing a
computer program. This is the stage where you decide how your
program will work to meet the decisions you made during
analysis.
Program design does not require the use of a computer -
you will carry out your design using pencil and paper.
ALGORITHM.
Algorithm:
Creating an Algorithm
To begin with we will look at three methods
used in creating an algorithm, these are
STEPPING OUT
First of all we will look at a method of creating an algorithm called
STEPPING.
This is where all the instructions needed to solve our problem are set
out one after the other. Here are some examples.
Find the sum of two numbers.
ALGORITHM:
Input -> Process -> Output
PROBLEM2:
Change the temperature in Fahrenheit to Centigrade.
ALGORITHM:
We will need to have a bit of knowledge and may have to find out some more
information.
This information may be found from a few different sources, for example a
textbook, the library or from your teacher.
•As an example, let's work through a problem together:-
PROBLEM: Find the volume of a cone given its diameter and height.
The Input:
1. The diameter () لاــقطر
2. The height ( ) اـالرتـفاـعof the cone
The Output:
The volume ( ) لاــحجمof the cone.
To solve this problem, we must find out the mathematical formula that allows us to calculate
the volume of the cone.
Where could we find this formula if we don't already know it?
We could look in a mathematics textbook or we could even try asking our Mr. Google
In Computer Form:
We still have a bit of a problem here, we don't know how to find the radius of the cone,
so again we ask google who tells us that the radius is half of the diameter.
THE ALGORITHM:
ALGORITHM:
1. open can using can opener
2. pour contents of can into saucepan
3. place saucepan on ring of cooker
4. turn on correct cooker ring
5. stir soup until warm
This may seem a bit of a silly example, but it does show us that the order
of the events is important since we cannot pour the contents of the can
into the saucepan before we open the can.
EXERCISES:
Write an algorithm for each of the following problems (remember you
may not have all of the knowledge you need to solve some of these
problems ~ ask for help):-
1) Find the average speed of a car given the journey time and the
distance travelled.
2) Find the volume of a cube given the length of a side.
3) Find the volume of a pyramid, given the length and breadth of its
base and its height
‰ GOING LOOPY ! [LOOPING]
To repeat an instruction or a set of instructions a number of times
to find the solution.
We will look at three different types of loops:-
i. the REPEAT UNTIL loop (do-while)
ii. the WHILE loop
iii. the FOR loop
now we will look at each of these a little closer....
i. REPEAT UNTIL LOOP
This type of loop keeps on carrying out a command or commands
UNTIL a given condition is satisfied, the condition is given with
the UNTIL command, for example:-
ALGORITHM:
1 REPEAT
2 wash with warm soapy water
3 UNTIL the whole car is clean
ii. While LOOP
In this type of loop the condition is given along with the WHILE
command and the loop keeps on carrying out a command or
commands until an END WHILE command is given, for example:-
PROBLEM:
To ask for a number more than 10, then stop when such a number
is given
ALGORITHM:
1 WHILE number given is less than 10
2 ask for a number more than 10
3 END WHILE
1. Keep asking for a number less than 5 and stop when a number less than 5
is given.
2. Keep asking for a password, and to give the message "accepted" when
the correct password is given.
3. Ask for the length of one side of a square, then keep asking for guesses
for the area of the square until the correct area is given (think about
stepping and looping).
iii. For LOOP
A FOR loop keeps on carrying out a command or commands, FOR a
given number of times. In this type of loop the number of times it
is repeated must be known. The commands to be repeated are
sandwiched between the FOR and END FOR commands, for
example :-
ALGORITHM:
1 FOR number = 10 to 20
2 Print number
3 END FOR
NOTE:
In this loop, the value of number starts at 1 and goes up to 11. Its value increases by
one each time it goes through the loop until it reaches 11. Since the value of number
changes or varies, number is called a variable. A variable can be given any name provided
the name remains the same throughout your program. We will use the word variable
many times in this work.
VARIABLE :
ALGORITHM:
1 FOR z = 1 to 10
2 Print 13 x z
3 END FOR
In this loop our variable is z, who’s value starts at one and then increases in value by 1
each time through the loop until z has the value of 10.
Line (2) of this algorithm prints the value of the variable z multiplied by 13.
Write down an algorithm for each of the following problems: (All of these
problems can be solved by using FOR loops):-
3) To print a table giving the price of petrol from 1 liter to 25 liters, given
that the price of petrol is 69.5p per liter.
5) To print a table showing the area of circles with radii from 10cm to 20cm
(ask if you are not sure how to find the area of a circle).
‰ CHOOSING ! [IFs, THENs AND ELSEs]
So far we have come across the method of stepping commands
and looping commands. Now we will look at a method for making a
choice or a decision.
ALGORITHM:
1 IF fire is detected condition
2 THEN sound fire alarm action
ALGORITHM:
1 IF it is a weekday AND it is not a holiday
2 THEN go to school
3 ELSE stay at home
In your jotters write down an algorithm for each of the following problems
(All of these problems can be solved by using the choosing method):-
‰ All together Now !
When you are programming you will find very few problems
that can be solved using just stepping, looping or choosing. In
fact, most problems will probably need a combination of all
three techniques. These examples combine STEPPING,
LOOPING and CHOOSING to find a solution to the problems
ALGORITHM:
1. Largest = 0
2. WHILE still number to check
3. Read number
Stepping 4. IF number is bigger than largest Choosing Looping
5. THEN largest = number
6. END WHILE
7. Print Largest
ALGORITHM:
1. number = 0
2. WHILE still boxes to check
3. Read color
4. IF color is red
5. THEN number = number + 1
6. ELSE number = number
7. END WHILE
8. Print number
Write down an algorithm for each of the following problems (Stepping, looping
and choosing will have to be used in different combinations for each problem):-
3) To check a list of job applicants and reject all those who either smoke or
drink.
4) To ask for a number, less than 50, and then keep adding 1 to it until 50 is
reached.
First of all we break the problem down into smaller steps and
then produce a Top Down Design for each step.
ALGORITHM:
1 ask for the dimensions
2 calculate the volume
3 display the volume
Step 1: Refinement:
1. Ask for the dimensions
1.1 get the length
1.2 get the breadth
1.3 get the height
Step 2: Refinement:
2. Calculate the volume
2.1 volume = length x breadth x height
ALGORITHM:
1. remove the tire
Step 2.1: 2.1 Find the position of the hole in the tube
Refinement: 2.1.1 WHILE hole cannot be found
2.1.2 Dip tube in water
2.1.3 END WHILE
In your jotters write down a Top Down Design for each of the following problems (Show your
initial algorithm and refined sub-problems as shown in the above example):-
2) To ask for the length of all the walls in a room, ask for the height of the room, calculate and
then display the total area of the room.