Alogorithum
Alogorithum
ALGORITHM
Algorithm can be defined as: “A sequence of activities to be processed for getting
desired output from a given input.”
We defines an algorithm as: “A formula or set of steps for solving a particular
problem. To be an algorithm, a set of rules must be unambiguous and have a clear
stopping point”. There may be more than one way to solve a problem, so there may
be more than one algorithm for a problem.
Now, if we take definition of algorithm as: “A sequence of activities to be
processed for getting desired output from a given input.” Then we can say that:
1. Getting specified output is essential after algorithm is executed.
2. One will get output only if algorithm stops after finite time.
3. Activities in an algorithm to be clearly defined in other words for it to be
unambiguous.
Before writing an algorithm for a problem, one should find out what is/are the
inputs to the algorithm and what is/are expected output after running the algorithm.
Now let us take some exercises to develop an algorithm for some simple problems:
While writing algorithms we will use following symbol for different operations:
‘+’ for Addition
‘-’ for Subtraction
‘*’ for Multiplication
‘/’ for Division and
‘ ’ for assignment. For example A X*3 means A will have a value
of X*3.
1 Example of Algorithm
Problem 1: Find the area of a Circle of radius r.
Inputs to the algorithm:
Radius r of the Circle.
Expected output:
Area of the Circle
Algorithm:
Step1: Read\input the Radius r of the Circle
Step2: Area PI*r*r // calculation of area
Step3: Print Area
Problem2: Write an algorithm to read two numbers and find their sum.
Inputs to the algorithm:
First num1.
Second num2.
Expected output:
Sum of the two numbers.
Algorithm:
Step1: Start
Step2: Read\input the first num1.
Step3: Read\input the second num2.
Step4: Sum num1+num2 // calculation of sum
Step5: Print Sum
Step6: End
Problem 3: Convert temperature Fahrenheit to Celsius
Inputs to the algorithm:
Temperature in Fahrenheit
Expected output:
Temperature in Celsius
Algorithm:
Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C 5/9*(F32)
Step 4: Print Temperature in Celsius: C
Step5: End
Type of Algorithms
The algorithm and flowchart, classification to the three types of control
Structures. They are:
1. Sequence
2. Branching (Selection)
3. Loop (Repetition)
These three control structures are sufficient for all purposes. The sequence is
exemplified by sequence of statements place one after the other – the one above or
before another gets executed first. In flowcharts, sequence of statements is usually
contained in the rectangular process box.
The branch refers to a binary decision based on some condition. If the condition
is true, one of the two branches is explored; if the condition is false, the other
alternative is taken. This is usually represented by the ‘if-then’ construct in pseudo-
codes and programs. In flowcharts, this is represented by the diamond-shaped
decision box. This structure is also known as the selection structure.
Problem 1: write algorithm to find the greater number between two numbers
Step1: Start
Step2: Read/input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End
Problem 2: write algorithm to find the result of equation: ( ) {
Step1: Start
Step2: Read/input x
Step3: If X Less than zero then F=-X
Step4: if X greater than or equal zero then F=X
Step5: Print F
Step6: End
Problem 3: A algorithm to find the largest value of any three numbers.
Step1: Start
Step2: Read/input A,B and C
Step3: If (A>=B) and (A>=C) then Max=A
Step4: If (B>=A) and (B>=C) then Max=B
Step5:If (C>=A) and (C>=B) then Max=C
Step6: Print Max
Step7:End
Properties of algorithm
Donald Ervin Knuth has given a list of five properties for a, algorithm, these
properties are:
1) Finiteness: An algorithm must always terminate after a finite number of steps. It
means after every step one reach closer to solution of the problem and after a finite
number of steps algorithm reaches to an end point.
2) Definiteness: Each step of an algorithm must be precisely defined. It is done by
well thought actions to be performed at each step of the algorithm. Also the actions
are defined unambiguously for each activity in the algorithm.
3) Input: Any operation you perform need some beginning value/quantities
associated with different activities in the operation. So the value/quantities are
given to the algorithm before it begins.
4) Output: One always expects output/result (expected value/quantities) in terms of
output from an algorithm. The result may be obtained at different stages of the
algorithm. If some result is from the intermediate stage of the operation then it is
known as intermediate result and result obtained at the end of algorithm is known
as end result. The output is expected value/quantities always have a specified
relation to the inputs.
5) Effectiveness: Algorithms to be developed/written using basic operations.
Actually operations should be basic, so that even they can in principle be done
exactly and in a finite amount of time by a person, by using paper and pencil only.
FLOWCHART
The flowchart is a diagram which visually presents the flow of data through
processing systems. This means by seeing a flow chart one can know the
operations performed and the sequence of these operations in a system. Algorithms
are nothing but sequence of steps for solving problems. So a flow chart can be used
for representing an algorithm. A flowchart, will describe the operations (and in
what sequence) are required to solve a given problem. You can see a flow chart as
a blueprint of a design you have made for solving a problem.
For example suppose you are going for a picnic with your friends then you plan for
the activities you will do there. If you have a plan of activities then you know
clearly when you will do what activity. Similarly when you have a problem to
solve using computer or in other word you need to write a computer program for a
problem then it will be good to draw a flowchart prior to writing a computer
program. Flowchart is drawn according to defined rules.
Flowchart Symbols
There are 6 basic symbols commonly used in flowcharting of assembly language
Programs: Terminal, Process and input/output, Decision, Connector and Predefined
Process. This is not a complete list of all the possible flowcharting symbols, it is
the ones used most often in the structure of Assembly language programming.
General Rules for flowcharting
1. All boxes of the flowchart are connected with Arrows. (Not lines)
2. Flowchart symbols have an entry point on the top of the symbol with no other
entry points. The exit point for all flowchart symbols is on the bottom except for
the Decision symbol.
3. The Decision symbol has two exit points; these can be on the sides or the bottom
and one side.
4. Generally a flowchart will flow from top to bottom. However, an upward flow
can be shown as long as it does not exceed 3 symbols.
5. Connectors are used to connect breaks in the flowchart. Examples are:
• From one page to another page.
• From the bottom of the page to the top of the same page.
• An upward flow of more than 3 symbols
6. Subroutines and Interrupt programs have their own and independent
Flowcharts.
7. All flow charts start with a Terminal or Predefined Process (for interrupt
Programs or subroutines) symbol.
8. All flowcharts end with a terminal or a contentious loop.
Examples of Flowcharts
Now, we will discuss some examples on flowcharting. These examples will help in
proper understanding of flowcharting technique. This will help you in program
development process in next unit of this block.
Problem1: Find the area of a circle of radius r.
Problem 2: Convert temperature Fahrenheit to Celsius.
Problem3: Flowchart for an algorithm which gets two numbers and prints sum of
their value
Problem4: Algorithm for find the greater number between two numbers.
Problem5: Flowchart for the problem of printing even numbers between 9 and
100:
roblem6: Flowchart for the calculate the average from 25 exam scores.
Advantages of using Flowcharts
As we discussed flow chart is used for representing algorithm in pictorial form.
This pictorial representation of a solution/system is having many advantagrs.These
advantages are as follows:
1) Communication: A Flowchart can be used as a better way of communication of
the logic of a system and steps involve in the solution, to all concerned particularly
to the client of system.
2) Effective analysis: A flowchart of a problem can be used for effective analysis
of the problem.
3) Documentation of Program/System: Program flowcharts are a vital part of a
good program documentation. Program document is used for various purposes like
knowing the components in the program, complexity of the program etc.
4) Efficient Program Maintenance: Once a program is developed and becomes
operational it needs time to time maintenance. With help of flowchart maintenance
become easier.
5) Coding of the Program: Any design of solution of a problem is finally converted
into computer program. Writing code referring the flowchart of the solution
become easy.