Module 2 - Algorithm and Features of Algorithms
Module 2 - Algorithm and Features of Algorithms
(Week 1)
Page 1 of 17
2.3 Characteristics of a good Algorithm
Some of the desirable characteristics of a well – written algorithms are:
(a) Integrity/Correctness: Integrity refers to the accuracy of a calculations
in an algorithm and is an absolutely necessity in any algorithm. All
algorithms’ enhancements are meaningless if calculations are not
correctly carried out.
(b) Clarity: Clarity refers to the overall readability of an algorithm with
particular emphasis on its underlying logic. If an algorithm is written
clearly, it should be possible for another programmer to follow the
algorithm logic without undue effort.
(c) Simplicity: clarity and accuracy of an algorithm can be enhanced by
making algorithm logic simple and consistent with the overall algorithm
objectives. This can be achieved by sacrificing certain amount of
computation efficiency in order to maintain a relatively simple and
straightforward algorithmic structure.
(d) Efficiency: Efficiency is concern with algorithm execution and efficient
memory utilization. This can be achieved by avoiding unstructured
statements in the algorithm. Efficiency should not be obtained at the
expense of clarity or simplicity.
(e) Modularity: An algorithm is said to be modular if it is decomposed into
a series of identifiable tasks called subalgorithms or procedures. Each of
these subtasks can be implemented as a separate algorithm module. The
use of a modular programming structure enhances the accuracy and
clarity of an algorithm. It also facilitates future algorithms alterations.
(f) Generality: An algorithm should be made to be general as possible,
within reasonable limit. Variables should be used to read in the values
of certain key parameters rather than placing constant (fixed) values into
the program. Considerable amount of generality can be obtained with
very little additional programming effort.
(g) Robustness: Robustness means that how an algorithm can clearly define
our problem.
Page 2 of 17
(h) Maintainability: Here, maintainability means that the algorithm
should be designed in a very simple structured way so that when we
redefine the algorithm, no major change will be done in the algorithm.
(i) Functionality: It considers various logical steps to solve the real-world
problem.
(j) User-friendly: If the algorithm is not user-friendly, then the designer
will not be able to explain it to the programmer.
(k) Extensibility: If any other algorithm designer or programmer wants to
use your algorithm then it should be extensible
(l) Documentation: The programmer must integrate good documentation
in every algorithm, that is, statements that briefly explain what every
part of the algorithm is doing. Documentation (or Comments) makes
algorithms easy to read, understand and modify when necessary.
Page 4 of 17
(d) Flowchart Development
A flowchart is a visual representation of the algorithm for non-technical
people so that they can easily understand the algorithm. It is expected at
this step to create a high-level representation of the algorithm using
flowcharts. Flowcharts use graphical symbols to represent the steps and
flow of the algorithm.
(e)Implementation
In this step, the algorithm design is translated into a specific computer
programming language. Write the code to implement the algorithm, taking
into consideration the syntax, data types, and control structures of the
programming language.
(e) Testing
Test the algorithm by giving the test data and see if the desired output is
generated. Testing with various inputs to verify the correctness and
effectiveness of the algorithm. Identify and fix any bugs or errors that may
arise during testing. Testing may involve different types of inputs, including
edge cases, to ensure the algorithm performs correctly in different scenarios.
(d) Documentation
Document the algorithm, including its design, implementation details, and
any assumptions or limitations. This documentation helps others understand
and use the algorithm effectively.
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Page 5 of 17
Step 6 − print c
Step 7 − STOP
Algorithms tell the programmers how to code the program. Alternatively, the
algorithm can be written as −
Page 6 of 17
The most common among three ways of expressing an algorithm are
Pseudocode and Flowchart. These tools are not programming languages and
they cannot be processed by a computer. Their purpose is to provide a way
for a programmer to document his idea in a program design. An Algorithm
expressed using any of the two tools mention above must be converted into a
program using an appropriate programming language before it can be
executed by the computer.
Page 7 of 17
✓ - for subtraction,
✓ / for division,
✓ * for multiplication and
✓ ^ or ** - For exponentiation
✓ Mod for Modulus (Remainder after Division)
✓ Div for Integer division
Examples:
X^2 means X2
X^Y means Xy
X + Y means X plus Y
X/Y means x divided by Y
X – Y means X minus Y
X + Y means X plus Y
Page 8 of 17
Statement – 1
ELSE
Statement – 1
ENDIF
e.g,
IF (a > b) Then
c = 20
else
c = 40
Endif
(ii) Goto Statement: The GOTO statement is used to alter the normal
sequence of algorithm (i.e., Pseudocode) execution by transferring
control to some other part of the program. In its general form, the
GOTO statement is written as:
GOTO label
where label is an identifier/step that is used to label the target
statement to which control will be transferred. Control may be
transferred to any other statement within the algorithm. (To be more
Page 9 of 17
precise, control may be transferred anywhere within the current
Algorithm. The target statement must be labeled, and the label must
be followed by a colon. Thus, the target statement will appear as:
label: statement
Each labeled statement within the program (more precisely, within the
current function) must have a unique label; i.e., no two statements can
have the same label. For example
Step 3: Print “ Hello”
.
.
.
GOTO Step 3
(iii) Select Case structure: This statement provides a structured way to
handle multiple branching. Several alternatives are offered but only
one is selected and executed. After the set of instructions are
executed, control transfers to the end of the structure. They are can
be several alternatives and the computer decides which set of
instructions to execute by examining the value of the Case values.
The syntax is:
Select Case (Expression)
When expression-1: Statement-1
When expression-2: Statement-2
.
.
.
When expression-n: Statement-n
Case Else
Statement – else
End Select Case
e.g.
Select Case color
When ‘R’: Print “RED”
When ‘G’: Print “GREEN”
When ‘B’: Print “BLUE”
CASE ELSE
Print “BLACK”
END SELECT
Page 10 of 17
✓ While Structure
✓ Do While Structure
✓ FOR Structure
✓ For-each Structure
✓ Repeat … Until Structure
For the purpose of algorithm, the third kind of logic refers to either of two
types of structures involving loops. Each type begins with a Repeat statement
and is followed by a module/statements/group of statements called the body
of the loop. For clarity, we will indicate the end of the structure by the
statement
[End of loop]
The repeat-for loop uses an index variable, such as K, to control the loop. The
loop will usually have the form:
Here start-value is called the initial value, stop_value is the end value or test value,
and step-value is the increment. Loop_variable is the variable used to control the
loop.
For example,
Repeat for counter = 1 to 5 by 1
Print Counter
End of Loop
The second is the repeat-while loop and it uses a condition to control the loop.
The loop will usually have the form
For example,
Repeat while ( i < 10)
counter = counter + 1
Print Counter
End of Loop
Page 11 of 17
2.9 Algorithm vs Pseudocode
An Algorithm and Pseudocode are used interchangeably but in actual fact
they are different. A Pseudocode is one of the ways to express an algorithm.
'Pseudo' means false or imitation and 'code' means program instructions.
An algorithm for a computer is the set of steps that explains how to begin with
the known information specified in a problem statement and manipulate that
information to arrive at a solution and is typically first written in a format that
is not specific to a particular computer or programming languages. This way,
the software engineer focuses on formulating a correct algorithm rather than
an expressing the algorithm using the commands of a computer programming
language. In the later phase of the software development process, the
algorithm is translated into instructions written in a computer programming
language so that the algorithm can be implemented by a computer.
Page 12 of 17
Some examples of algorithm are given below:
2.10.1 Sequential Execution
Example 1: Design an algorithm to find sum of two numbers and output the
result.
Solution:
Program Specification/Glossary
Fnum = First Number
Snum = Second Number
Sum = Sum of the Numbers
Algorithm
Step 1: START ADD
[Algorithm to Find the Sum of two Numbers]
Step 2: Input Fnum and Snum
Step 3: Sum ← Fnum + Snum
Step 4: Print “Sum = ”, Sum
Step 5: STOP
Solution
Program Specification/Glossary
d – diameter
t – wall thickness
density ρ = rho
Ro – Ro
Ri = Ri
Volume = V
Weight = W
Algorithm
Step 1: START HOLLOWSPHERE
[Algorithm to Compute the Weight of a Hollow Sphere]
Step 2: Read d, t and rho
Step 3: Ro := d/2, Ri = Ro – t
Step 4: V := 4 * 3.142 * (Ri ^3 – Ro ^ 3)/3
Step 5: W:= rho * V
Step 6: Print “Weight = ”, W
Page 13 of 17
Step 7: STOP
Example 3:- Write an Algorithm to convert a temperature reading in degrees
Fahrenheit to degrees Celsius, using the formula C = (5/9 )x (F- 32) .
Solution
Program Specification/Glossary
Temperature in Celsius degrees - Celsius
Temperature in Fahrenheit degrees – Fahrenheit
Algorithm
Step 1: START TEMPERATURECONVERSION
[Algorithm to convert Temperature Reading in degree Fahrenheit
to Celsius]
Step 2: Input Fahrenheit
Step 3: Celsius ← 5/9 * (Fahrenheit – 32)
Step 4: Print “Temperature in degree Celsius = ”, Celsius
Step 5: STOP
Example 4: Write an Algorithm that calculates the Energy (Q) needed to heat
water from an initial temperature to a final temperature. The formula for
computing the Energy is:
Q = M * (final temperature – initial temperature) * 4184
Where M is the weight of water in Kilograms, temperatures are in degrees
Celsius and Energy Q is measured in joules.
Solution
Step 1: START ENERGYNEEDED
[Algorithm to compute in Joules]
Step 2: Read Mass, InitialTemperature, FinalTemperature
Step 3: Energy := Mass * (InitialTemperature – FinalTemperature) * 4184
Step 4: Print “Energy in Joules = ”, Enegry
Step 5: STOP
Solution
Step 1: START TRIANGLE
[Algorithm to compute the Perimeter of a triangle]
Step 2: Read A, B, and C
Step 3: Perimeter ← (A + B + C)/2
Step 4: Print “Perimeter = ”, Perimeter
Page 14 of 17
Step 5: STOP
2.10.2 Selection/Branching Operations
Example 1: Write an Algorithm to input two numbers and determine which
of them is Larger.
Solution
Step 1: START LARGER
[Algorithm to input two numbers and determine Larger]
Step 2: Input Fnum, Snum
Step 3: IF (Fnum > Snum) Then
Step 4: Print Fnum, “ Is Larger”
Step 5: Else
Step 6: Print Snum, “ Is Larger”
Step 7: ENDIF
Step 8: STOP
Solution
Step 1: START RELATIONSHIP
[Algorithm to compute mathematical relationship]
Step 2: Input X
Step 3: IF (X > = 0) then
Step 4: Y← 4 *X ^ 2 - X^ 2 + 1
Step 5: Else
Page 15 of 17
Step 6: Y← X ^2 + X – 1
Step 7: ENDIF
Step 8: Print Y
Step 9: STOP
Algorithm
Step 1: START SUMMATION
[Algorithm to compute the Sum and Average of 50 numbers]
Step 2: Sum: = 0.0.
Step 3: Input N
Step 4: Repeat for counter = 1 to N by 1
Step 5: Input Number
Step 6: Sum := Sum + Number
Step 7: End of Loop
Step 8: Average := Sum/N
Step 9: Print Sum
Step 10: Print , Average
Step 11: STOP
Example 2: Design an algorithm to calculate the sum and average of the first
50 integer numbers and outputs the result.
Solution:
Step 1: START SUMMATION
[Algorithm to compute the Sum and Average of the first 50 Integer
numbers]
Step 1: Sum: = 0
Step 2: Input N
Step 3: Repeat for counter = 1 to N by 1
Step 4: Sum := Sum + Counter
Step 5: End of Loop
Step 6: Average:= Sum/N
Step 7: Print Sum
Step 8: Print Average
Page 16 of 17
Ste9 9: STOP
Solution
Program specification/Algorithm
Let measurement in inches = inches
Let measurement in centimeters = centimeters.
Algorithm
Step 1: START MEASUREMENT
[Algorithm to convert from Inches to Centimeters]
Step 2: Inches := 1
Step 3: Read n (i.e. No of terms)
Step 4: Repeat for inches = 1 to n by 0.5
Step 5: centimeters: = 2.54 * inches
Step 6: Print inches, centimeters.
Step 7: End Loop
Step 8: STOP
Page 17 of 17