Anda di halaman 1dari 6

WEEK 2

Algorithms: Algorithm is a well-defined computational procedure that takes a single value or set
of values as input and produces a value or set of values as output. An algorithm is thus a
sequence of computational steps that transform the input into the output. We can also view an
algorithm as a tool for solving a well specified computational problem.
An algorithm is a finite set of instructions, which accomplish a particular task. It must satisfy the
following criteria.
Input: The algorithm must have input values from the specified set.
Output: Each algorithm is written to produce the output.
Definiteness: Each instruction must be clear and unambiguous.
Finiteness: It means that algorithm must terminate in the finite number of steps.
Effectiveness: Every algorithm must be effective which performs some specific action on the
given data (i.e. to produce a meaningful result).
Efficient and Elegant: Algorithm should be efficient. They should neither use memory locations
unnecessarily nor should they require an excessive number of logical operations. With time
efficiency the faster algorithm is regarded as best. If the algorithm is clear and simple to
understand then it is considered as elegant algorithm.
Concise and Compact: Algorithm should be concise and compact to facilitate verification of
their correction.

Algorithmic Notations: The algorithm is a base of not only effective data structure but also the
base of good programming. Therefore, it is necessary that each algorithm should be written
clearly. A complete algorithm notation is given below:
Name of Algorithm: Every algorithm is given an identifying name written in capital letters.
Introductory Comments:The algorithm name is followed by a brief description of the tasks that
algorithm performs and any assumption that has been made. The description gives the names and
types of variables used in algorithm.

Steps: The algorithm is made of sequence of numbered steps. Each step begins with a phrase
enclosed in square brackets with an abbreviated description of that step following this phrase in
an ordered sequence of statements which describe action to be executed or tasks to be performed.
Comments: An algorithm step may terminate with a comment enclosed in round parenthesis
intended to help the reader understand that step better. Comments specify no action and are
enclosed only for explanation.
The algorithm procedures are explained with some examples by which you can be familiar with
algorithms. In the given examples all the algorithmic rules are followed which is defined by the
al-Khuwarizmi.

Examples of Algorithms:
ALGORITHM: AVERAGE
This algorithm is used to calculate the average of four numbers. Read four elements/number at
runtime and store in a, b, c, d variable respectively. Then calculate the average and store it in
Average variable. All the elements are of real type.
Step No. 1:

[Read data]
Read (a, b, c, d)

Step No. 2:

[Calculating Average]
Average (a + b + c + d) / 4

Step No. 3:

[Display the result]


Write (Average is = , Average)

Step No. 4:

[Finish]
Exit

In the above algorithm name of the algorithm is AVERAGE and then there is an introductory
comments in which all the variables used in this algorithm are explained. Every instruction in the
algorithm is numbered which shows the flow of execution of instruction in an algorithm. Each
step has its own comments for easy understandability of algorithm for readers.

In the above algorithm user enter data at runtime in a, b, c and d variables consecutively. In next
step calculate the average and store in an average variable. In step 3 the result is displayed on the
screen.

Implementation of above algorithm in C++ is as under:


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a, b, c, d, average;
cout<< enter four integer values ;
cin>>a>>b>>c>>d;
average = (a + b + c + d)/4;
cout<< Average = <<average;
getch();
}

There is another example of algorithm.


ALGORITHM: ADD
This algorithm is used to calculate the addition of two numbers A and B, and the result is stored
in variable C. All variable used are of integer type.
Step No. 1:

[Read A, B]
Read (A, B)

Step No. 2:

[Calculations or Processing]
C = A+ B

Step No. 3:

[Print output]
Write (Sum = , C)

Step No. 4:

[Finish]
Exit

Implementation of the above algorithm in C++ language is an under:


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, c;
cout<< enter two integer values ;
cin>>a>>b;
c = a + b;
cout<< sum = <<c;
getch();
}

Execution Flow: There is a flow in the execution of algorithm instructions and their equivalent
computer program. There are common control structures used in any computer programming
language. These are the following.
i. Sequential flow
ii. Conditional/Selection flow
iii. Iterative or Repetitive flow

i. Sequential Flow: In sequential flow the instructions are executed in sequential form means
that from top to bottom in a sequence.

Example:

a = 2;
b = 3;
sum = a + b;

Sequential flow flowchart is given the following figure

Step
1
Step
2
Step
3
Step
4

Conditional/Selection Flow:It works on the basis of condition.Selection flow employs a number


of conditions which lead to a selection of one or more out of several alternative instructions or
modules.
Example:

a=3
b=2
If a = b
displayequal
else
displaynot equal

Conditional flowchart is given below.


no

Conditi
on
yes

Modul
e

Iteration or Repetitive Flow: The third kind of flow refers to the structures involving loops. It
begins with a repeat statement and is followed by a module, called the body of the loop.
Example:

inta[5];
for (i=0;i<5;i++)
cin>>a[i];

no

Conditi
on

yes
Modul
eA

Anda mungkin juga menyukai