You are on page 1of 12

WEEK2

ALGORITHM

Before a computer can be put to any meaningful use, the user must be able to
come out with or define a unit sequence of operations or activities (logically
ordered) which gives an unambiguous method of solving a problem or finding out
that no solution exists. Such a set of operations is known as an ALGORITHM.

Definition: An algorithm, named after the ninth century scholar Abu Jafar
Muhammad Ibn Musu Al-Khowarizmi , is defined as follows: Roughly speaking:
· An algorithm is a set of rules for carrying out calculation either by hand or a
machine.
· An algorithm is a finite step-by-step procedure to achieve a required result.
· An algorithm is a sequence of computational steps that transform the input into
the output.
· An algorithm is a sequence of operations performed on data that have to be
organized in data structures.
· An algorithm is an abstraction of a program to be executed on a physical machine
(model of computation)
The most famous algorithm in history dates well before the time of the ancient
Greeks:
this is Euclids algorithm for calculating the greatest common divisor of two integers.
Before we go into some otherwise complex algorithms, let us consider one of the
simplest but common algorithms that we encounter everyday.
The classic multiplication algorithm
For example to multiply 981 by 1234, this can be done using two methods
(algorithms)
viz:
a. Multiplication the American way:
Multiply the multiplicand one after another by each digit of the multiplier taken from
right to left.

b. Multiplication , the British way:


Multiply the multiplicand one after another by each digit of the multiplier taken from
left to right.

An algorithm therefore can be characterized by the following:


(i) A finite set or sequence of actions
(ii) This sequence of actions has a unique initial action
(iii) Each action in the sequence has unique successor
(iv) The sequence terminates with either a solution or a statement that the problem
is unresolved.
An algorithm can therefore be seen as a step-by-step method of solving a problem.

Concept of Algorithm
An algorithm is a set of instructions to obtain the solution for a given problem.
Computer needs precise and well-defined instructions for finding solution of
problems. If there is any ambiguity, the computer will not yield the right results. It
is essential that all the stages of solution of a given problem be specified in details,
correctly and clearly moreover, the steps must also be organized rightly so that a
unique solution is obtained.

Programming Task
A typical programming task can be divided into two (2) phases:
(a) Problem solving phase
In this stage an ordered sequence of steps that describe solution of the problem is
produced.
Their sequence of steps can be called anti-Algorithm
(b) Implementation Phase
In this phase, the program is implemented in some programming languages.
Algorithm may be set up for any type of problems, mathematical/scientific or
business. Normally algorithms for mathematical and scientific problems involve
mathematical formulas. Algorithms for business problems are generally descriptive
and have little use of formula.

Features of an Algorithm
1. It should be simple
2. It should be clear with no ambiguity
3. It should head to unique solution of the problem
4. It should involve a finite number of steps to arrive at a solution
5. It should have the capability to handle unexpected situation.

Methods of Representing Algorithm


Algorithms are statements of steps involved in solving a particular problem. The
steps to the solutions are broken into series of logical steps in English related form.
Programs are written to solve real life problems.
There can’t be a solution if there is no recognized problem and once a problem
exist, one must take certain step in order to get a desired solution. The following
methods could be used to represent an algorithm.
a) _ Methods of English like form
b) _ Methods of Flowchart
c) _ Methods of Pseudo code
d) _ Methods of Decision table
e) _ Methods of Data flow Diagram (DFD)

a. English-like form
The English form of representing as algorithm entails breaking down the solution
steps of the problem into single and sequential English words. The steps are
represented in English to say what action should be taken in such a step.
Example 1: Develop an algorithm to obtain a book on computer from your school
library located on the fourth floor of the building. You are to proceed to the library
from your ground floor classroom.
1. Start from the classroom
2. Climb the stairs to the 4th floor and reach the library
3. Search a book on computer
4. Have the book issued
5. Return to your classroom
6. Stop.
Note: The above algorithm solution of example 1, has been written in simple and
clear English way.
Example 2: Develop an algorithm to find the average of four numbers stored in
variables A, B, C, D
Solution
1. Start
2. Read values in variables A,B,C,D
3. Calculate the average as (A+B+C+D)/4 and store the result in P.
4. Write the value stored in P
5. Stop.
Example 3:
Develop and algorithm to find the average of four numbers stored in variables A, B,
C, D. When the value of variable A is zero, no averaging is to be done.
Solution
1. Start
2. Read values stored in variable A,B,C,D
3. If the value of A is Zero, then jump to step 6
4. Calculate the average of A, B, C, D and store the result in variable P.
5. Write the value of P
6. Stop.

b. Flowchart
A flowchart is a pictorial representation of an Algorithm or of the plan of solution of
a problem. It indicates the process of solution, the relevant operations and
computations, point of decision and other information that are part of the solution.
Flowcharts are of particular importance for documenting a program. Special
geometrical symbols are used to construct flowcharts. Each symbol represents an
activity. The activity could be input/out of data, computation/processing of data,
taking a decision, terminating the solution, etc. The symbols are joined by arrows
to obtain a complete flowchart.
Example 1
Draw a flowchart to find the average of four numbers stored in variables A,B,C,D

Example 3
Write an algorithm and draw a flowchart that will read the two sides of a rectangle
and calculate its area.
Solution
Pseudo code
· Input the Width(w) and Length(L) of a rectangle
· Calculate the area (A) by multiplying L with W
· Print A.
Algorithm
Step 1: Input W, L
Step 2: A← L * W
Step 3: Print A.

Example 4
Write an algorithm and draw a flowchart that will calculate the roots of a quadratic
equation.
ax2 + bx + c = 0
Hint: d = SQrt(b2 – 4ac), and the roots are:
X1 = (-b + d)/2a and X2 = (b – d)/ 2a
Solution
Pseudo code
· Input the coefficients (a, b, c) of the quadratic equation
· Calculate d
· Calculate X1
· Calculate X2
· Print X1 and X2
Algorithm
Stop
Input
W,L
A←L*W
Print A
Stop
Step 1: Input a, b, c
Step 2: d ← Sqrt (b * b – 4 * a * c)
Step 3: X1 ← ( - b + d)/ (2 * a)
Step 4: X2 ← (- b – d)/ (2 * a)
Step 5: Print X1, X2

Flowchart

Example 5
Write an algorithm and draw a flowchart to convert the length in feet to centimeter.
Solution
(Pseudocode)
· Input the length in feet (Lft)
· Calculate the length in an (LCM) by multiplying LFT with 30
· Print Length in Cm (LCM)
Step 1: Input Lft
Step 2: Lcm ← Lft * 30
Step 3: Print Lcm
Step 4: Stop
Examples 6
1. Write an algorithm to read values for three variables. U, V, and W and find a
value for RESULT from the formula: RESULT = U + V2/W. Draw the flowchart.
Solution:
Algorithm
(i) Input values for U, V, and W
(ii) Computer value for result
(iii) Print value of result
(iv) Stop
Example 7: Suppose you are given 20 numbers. Prepare the algorithm that adds
up these numbers and find the average. Draw the flowchart.
Solution:
Algorithm
(i) Set up a Counter (1) which counts the number of times the loop is executed.
Initialize Counter (1) to 1.
(ii) Initialize sum to Zero
(iii) Input value and add to sum
(iv) Increment the Counter (1) by 1
(v) Check how many times you have added up the number (if it is not up to the
required number of times, to step (iii).
(vi) Computer the average of the numbers
(vii) Print the average
(viii) Stop.
Example 8: Prepare an algorithm that indicates the logic for printing the name and
telephone number for each female in a file (Code field is 2 for female). Draw the
flowchart.
Solution:
Algorithm
(i) Read a record
(ii) Determine if the record pertains to a female (that is, determine if the code field
is equal to 2).
(iii) If the code field is not equal to 2, then do not process this record any further,
since it contains data for a male. Instead, read the next record; that is, go back to
step (i).
(iv) If the record contains data for a female (that is, code is equal to 2), then print
out the following fields: first name, last name, telephone number
(v) Go back to step (i) to read the next record.
Example 9: Prepare an algorithm that prints name and weekly wages for each
employee out
of 10 where name, hours worked, and hourly rate are read in. Draw the flowchart.
Solution:
Algorithm
(i) Initialize Counter (A) to 1
(ii) Read name, hours and rate and number of workers
(iii) Let the wage be assigned the product of hours and rate
(iv) Print name and wages
(v) Increment the counter (A) by 1
(vi) Make a decision (Check how many times you have calculated the wages)
(vii) Stop processing, if you have done it the required number of times.

You might also like