1. Introduction
1. Introduction
I. Introduction
In this unit of work you will learn what is the algorithm, algorithm notations, the
Algorithm is defined as a solution for a problem that comes in well defined collection of
instructions (steps, conditions, and repetitions) in which this solution uses given
parameters to produce the correct result (achieve defined goal).The study of algorithms is
answer to “how to make” question. For example, if we like to answer the question “how
do you make a cake?” the answer comes as a recipe of making cake which is algorithm.
As a programmer, if you have been asked: “how do you make software?” you will be
1. Problem definition
3. Design a solution/program
1|Page
It is impossible to do six things correctly at the same time, We will therefore
concentrate on each step, one at a time; - it is an algorithm for making software !!!
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. In your design, you will use a
method called top down design. By using this method, you simply write down the
steps in the correct order, that you are required to perform, to solve the problem. When
all these steps are put together you have what is called an Algorithm, which is a
The instructions for each step are exact and precise and can be carried out by a
computer.
1. Pseudocode: English language statements with some defined rules of structure and
different box types connected by lines with arrowheads indicating the flow. It is
common practice only to show arrowheads where the flow is counter to that stated
above.
2|Page
3. Programming languages are essentially a way of expressing algorithms.
An algorithm trace is a method for hand simulating the execution of your code
in order to manually verify that it works correctly before you compile it.
(such as time and storage) necessary to execute them. Most algorithms are
theory, which provides theoretical estimates for the resources needed by any
in the asymptotic sense, i.e., to estimate the complexity function for arbitrarily
large input. Big O notation, Big-omega notation and Big-theta notation are
used to this end. For instance, binary search is said to run in a number of steps
3|Page
O(log(n)), colloquially "in logarithmic time". Usually asymptotic estimates are
a hidden constant.
machine, and/or by postulating that certain operations are executed in unit time.
For example, if the sorted list to which we apply binary search has n elements,
and we can guarantee that each lookup of an element in the list can be done in
unit time, then at most log2 n + 1 time units are needed to return an answer.
a. STEPPING OUT
calledSTEPPING. This is where all the instructions needed to solve our problem are
4|Page
EXAMPLES:
Algorithm: 1 subtract 32
Both of these examples list the algorithm in an ordered set of steps. Now it's your turn
to try a few.
Problem: Find the volume of a cone given its diameter and height. The formula is:
5|Page
EXERCISE: write down an algorithm for each of the following problems:-
3) Find the product of two numbers (this means to multiply the two numbers).
5) Change a volume in pints to litres (there are 2.2 pints in every litre).
6) Find the average speed of a car given the journey time and the distance
travelled.
8) Find the volume of a pyramid, given the length and width of its base and its
height.
So far we have come across the method of stepping commands. Now we will look at
and uses the commands IF, THEN and sometimes (but not always) ELSE .
THEN command. Here are a couple of examples which should make things a little
clearer: -
6|Page
EXAMPLES:
2 THENgo to university
3 ELSEstay at home
In this example a connectivity operation, AND, was used. This connectivity operation is
used to allow for two or more conditions that have to be satisfied. You will notice
that we have also used the ELSE command in this example where we are stating an
EXERCISE:
In your jotters write down an algorithm for each of the following problems (All of
7|Page
c. GOING LOOPY! [LOOPING]
So far, all of the problems we have solved used an algorithm with the set of instructions
in a set order that we refer to as stepping. What if you have to repeat an instruction
or a set of instructions a number of times to find your solution? In this case you can
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
• WHILE LOOP
In this type of loop the condition is given along with the WHILE command and the
Problem: To ask for a number more than 10, then stop when such a number is given
3 END WHILE
8|Page
EXERCISE:
In your jotters write down an algorithm for each of the following problems
1) To keep asking for a number less than 5 and stop when a number less
than 5 is given.
2) To keep asking for a password, and to give the message "accepted" when
3) To 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
• 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
2. Print number
3. END FOR
NOTE: In this loop, the value of number starts at 10 and goes up to 20. Its value
increases by one each time it goes through the loop until it reaches 21. Since
9|Page
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 z x 13
3. END FOR
In this loop our variable is z, whose 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
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
10 | P a g e
Problem: To find the biggest number in a large set of numbers.
Algorithm:
1. largest = 0
3. Read number
6. ENDWHILE
7. Print largest
Problem: To count the number of red boxes in a set of red, white and blue boxes
Algorithm:
1. number = 0
3. Read color
4. IFcolor is red
6. ENDWHILE
7. Print number
Now it’s your turn to try out all this new found knowledge in the writing of
CHOOSING
11 | P a g e
VI. Top Down Design
If you are asked to find a solution to a major problem, it can sometimes be very difficult
to deal with the complete problem all at the same time. For example building a car is a
major problem and no-one knows how to make every single part of a car. A number of
different people are involved in building a car, each responsible for their own bit of the
car’s manufacture.The problem of making the car is thus broken down into smaller
manageable tasks. Each task can then be further broken down until we are left with a
Top Down Design uses the same method to break a programming problem down into
manageable steps. First of all we break the problem down into smaller steps and then
produce a Top Down design for each step. In this way sub-problems are produced which
Problem: Ask for the length, breadth and height of a room. Then calculate and display
Algorithm:
12 | P a g e
Refinement:
VII. EXERCISE:
3) To print a table giving the price of petrol from 1 litre to 25 litres, given
5) To print a table showing the area of circles with radii from 10cm to 20cm (ask if
8) To check a list of job applicants and reject all those who either smoke or drink.
9) To ask for a number, less than 50, and then keep adding 1 to it until 50 is reached.
10) To ask for the length of all the walls in a room, ask for the height of the room,
11) To calculate an employee’s wages using these conditions: Ask for the employee’s
13 | P a g e
name and how many hours they worked. Up to 40 hours are paid at £4.00 per hour,
all hours over 40 hours are paid at “time and a half” Display the employee’s name,
their basic rate pay, their overtime pay and their total pay.
VIII. Recursion
until certain condition is achieved. Repetition can be implemented using loop: while, for
Recursive is a repetitive process in which an algorithm calls itself. Recursion can be used
to replace loops. Recursively defined data structures, like lists, are very well-suited to
mathematically more elegant than one using loops. Sometimes procedures that would be
Note that not all problems can be solved using recursive. Problem that can be solved
using recursive is a problem that can be solved by breaking the problem into smaller
Drawback of recursive is that the execution running time for recursive function is not
efficient compared to loop, since every time a recursive function calls itself, it requires
There must be at least one case (the base case), for a small value of n, that can be solved
directly. A problem of a given size n can be split into one or more smaller versions of the
14 | P a g e
same problem (recursive case). Recognize the base case and provide a solution to it.
Devise a strategy to split the problem into smaller versions of itself while making
progress toward the base case. Combine the solutions of the smaller problems in such a
IX. Examples
achieved by using addition method. For example, to multiply 8 x 3, the result can
int result=0;
result += M;
return result;
15 | P a g e
Recursive function will call Multiply() repeatedly by reducing N by 1 for each
respective call.
Terminal case is achieved when the value of N is 1 and recursive call will stop. At
this moment, the solution for the terminal case will be computed and the result is
The simple solution for this example is represented by variable M. In this example,
the value of M is 8.
int result;
if (N==1)
result=M;
else
result= M + Multiply(M,N-1);
return result;
}//end Multiply()
16 | P a g e
17 | P a g e
2: String Length Algorithm (Example)
18 | P a g e
3: Factorial Method
Suppose we call the method to solve fact(4). This will result in four calls of method fact.
Step by step
fact(4): This is not the base case (n==1) so we return the result of 4 * fact(3). This
multiplication will not be carried out until an answer is found for fact(3). This leads to
fact(3): Again, this is not the base case and we return 3 * fact (2).
fact(1): Finally we reach the base case, which returns the value 1.
19 | P a g e
4. Fibonacci Problem
Starting from 0 and have features that every Fibonacci series is the result of adding 2
Fibonacci ( 0) = 0
Fibonacci ( 1) = 1
Fibonacci ( 2) = 1
Fibonacci ( 3) = 2
20 | P a g e
Solving Fibonacci Recursively
The simple solution for this example is represented by the Fibonacci value equal to 1.
N represents the series in the Fibonacci number. The recursive process will integrate the
call of two Fibonacci () function. Terminal case for Fibonacci problem is when N equal
Fibonacci() function
{ /* start Fibonacci*/
if (N<=0)
return 0;
else if (N==1)
return 1;
else
Infinite Recursive
There’s a condition where the function will stop calling itself. (if this condition is not
fulfilled, infinite loop will occur). Each recursive function call, must return to the called
function. Variable used as condition to stop the recursive call must change towards
terminal case. Avoiding infinite recursion to avoid infinite recursion: must have at least 1
base case (to terminate the recursive sequence) each recursive call must get closer to a
base case
21 | P a g e
Infinite Recursive : Example
#include <stdio.h>
#include <conio.h>
main()
{ int number;
printIntegers(number);
{ if (nom >= 1)
printIntegers (nom-2);
22 | P a g e
4: Recursive Algorithm for Calculating xn
5: Towers of Hanoi
Rules:
Move one disk at a time. Each disk you move must be a topmost disk.
You can store disks on the second pole temporarily, as long as you observe the previous
two rules.
23 | P a g e
To solve for n disks …
Etc.
Each one lets previous friend know when their simpler task is finished
identity (10) 40
24 | P a g e
(b) Consider the following relation table:
negative(9) 73
negative(5) 83
negative(1) 85
negative(-3) 79
25 | P a g e
(c) Consider the following relation table:
product(64) -1 intproduct(intnum)
{
product(-32) 32 if (num > 20)
return -1;
product(16) 512
else
product(-8) -4096 return num * product(-2 *
num);
product(4) -16384 }
product(-2) 32768
product(1) 32768
To find the largest element in list[a]...list[b], First find the largest element in
list[a]...list[b] stands for the array elements list[a], list[a + 1], ..., list[b].
list[0]...list[5] represents the array elements list[0], list[1], list[2], list[3], list[4], and
list[5].
If list is of length 1, then list has only one element, which is the largest element.
26 | P a g e
Suppose the length of list is greater than 1.
To find the largest element in list[a]...list[b], we first find the largest element in
list[a + 1]...list[b] and then compare this largest element with list[a].
1]...list[b]))
27 | P a g e
28 | P a g e