1
ALGORITHM AND FLOWCHART
I. ALGORITHM
II.1. Definition
Algorithms are one of the most basic tools that are used to develop the problem-solving logic. An algorithm
is defined as a finite sequence of explicit instructions that when provided with a set of input values,
produces an output and then [Link] be an algorithm, the steps must be unambiguous and after a finite
number of steps, the solution of the problem is achieved.
Different algorithms may accomplish the same task, with a different set of instructions, in more or less the
same time, space, and efforts. For example, two different recipes for preparing tea, one 'add sugar' while
'boiling water' and the other 'after boiling water' produce the same result.
II.2. Properties of an algorithm
Note that algorithms are not computer programs, as they cannot be executed by a computer. Some properties
of algorithm are as follows:
• Non ambiguous: There must be no ambiguity in any instruction.
• Coherent: There should not be any uncertainty about which instruction is to be executed next.
• The description of the algorithm must be finite. An algorithm cannot be open-ended.
• Finiteness: It should terminate after a finite number of steps.
• It must be general enough to deal with any contingency.
II.3. Example of algorithm
We use algorithms in our daily life. For example, to determine the largest number out of three numbers, A,
B, and C, the following algorithm may be used.
Algorithm: To determine largest of three numbers
1. Start
2. Read three numbers A, B, C
3. Find the larger number between A and B and store it in MAX_AB
4. Find the larger number between MAX_AB and C and store it in MAX
5. Display MAX
6. Stop
The above-mentioned algorithm terminates after six steps. This explains the feature of finiteness. Every
action of the algorithm is precisely defined; hence, there is no scope for ambiguity.
II. REPRESENTATION OF AN ALGORITHM
Algorithms can be represented in different ways:
- By Flowchart
- By Pseudocode
III.1. Flowchart
A flowchart helps to clarify how things are currently working and how they could be improved. It also
assists in finding the key elements of a process by drawing clear lines between where one process ends and
the next one [Link] help in revealing redundant or misplaced steps. In addition, they help in
establishing important areas for monitoring or data collection and to identify areas for improvement or
increase in efficiency.
a) Flowchart Symbols
A flowchart is drawn according to defined rules and using standard flowchart symbols prescribed by
American National Standard institute (ANSI). Some standard symbols that are frequently required for
flowcharts are shown:
Symbol Symbol name Description
Flow lines are used to connect symbols. These lines indicate the
Flow Lines
sequence of steps and the direction of flow.
2
The symbol is used to represent the beginning (start), the
Terminal
termination (end) or halt (pause) in the program logic.
Process symbol is used for representing arithmetic and data
movement instructions. It can represent a single step (‘add two
Processing
cups of flour), or an entire sub- process (‘make bread’) within a
larger process.
Process symbol is used for representing arithmetic and data
movement instructions. It can represent a single step (‘add two
Decision
cups of flour), or an entire sub- process (‘make bread’) within a
larger process.
This symbol represents information entering or leaving
Input/output
the system such as customer order (input) and servicing (output)
It is used to provide additional information about another
Annotation flowchart symbol. The content may be in the form of
descriptive comments, remarks or explanatory notes.
Connector Connector symbol is used to join different flow lines.
Off- page This symbol is used to indicate the flowchart continues on the
Connecter next page
Document is used to represent a paper document produced
Document
during the flowchart process.
Manual input symbol represents input to be given by a
Manual Input
developer / programmer.
Manual Manual operation symbol shows that the process has to be done
Operation by a developer/ programmer.
This is used to represent data input or output from and to a
Magnetic Disk
magnetic disk.
Predefined This symbol represents the operation or process that has been
process previously specified elsewhere.
Multipage This symbol is used to represent a document with multiple
document pages.
This symbol is used to represent data input or output from and
Magnetic tape
to a magnetic tape.
b) Guidelines for Preparing Flowcharts
The following guidelines should be used for creating a flowchart:
• The flowchart should be clear, neat and easy to follow.
• It must have a logical start and finish.
• In drawing a proper flowchart, all necessary requirements should be listed in logical order.
• Only one flow line should come out from a process symbol.
• Only one flow line should enter a decision symbol. However, two or three flow lines (one for each
possible answer) may leave the decision symbol.
3
• Only one flow line is used with a terminal symbol.
• Within standard symbols, write briefly. If necessary, use the annotation symbol to describe data or
process more clearly.
• In case of complex flowcharts, connector symbols are used to reduce the number of flow lines.
•
• Intersection of flow lines should be avoided to make it a more effective and better way of
representing communication.
• It is useful to test the validity of the flowchart by passing through it with normal/unusual test data.
c) Advantages of flowchart
The flowchart shows how the program works before you begin actually coding it. Some advantages of
flowcharting are the following.
- Communication: Flowcharts are helpful in explaining the program to other people.
- Effective analysis: With the help of flowchart, the problem can be analysed more effectively
- Proper documentation: Program flowchart serves as a good program documentation, which is
needed for various purposes.
- Efficient coding: Once the flowchart is drawn, it becomes easy to write the program in any high
level language
- Proper debugging: The flowchart help in the debugging process
- Efficient program maintenance: The maintenance of operating program become easy with the help
of flowchart
d) Limitations of Flowcharts
Flowchart can be used for designing the basic concept of the program in pictorial form, but cannot be used
for programming purposes. Some of the limitations of the flowchart are given below:
• Complex: The major disadvantage in using flowcharts is that when a program is very large, the
flowcharts may continue for many pages, making them hard to follow.
• Costly: If the flowchart is to be drawn for a huge program, the time and cost factor of program
development may get out of proportion, making it a costly affair.
• Difficult to Modify: Due to its symbolic nature, any change or modification to a flowchart usually
requires redrawing the entire logic again, and redrawing a complex flowchart is not a simple task.
• No Update: Usually, programs are updated regularly. However, the corresponding update of
flowcharts may not take place, especially in the case of large programs.
III.2. Pseudocode
Pseudocode (pronounced Soo-Doh-Kohd) is made up of two words: Pseudo and code. 'pseudo' means
imitation and 'Code' refers to instructions, written in a programming language. As the name suggests,
4
pseudocode is not a real programming code, but it models and may even look like a programming code. It is
a generic way of describing an algorithm without using any specific programming language notations. It
uses plain English statements rather than symbols, to represent the processes in a computer program. It is
also known as program design language (PDL)
a) Pseudocode Structures
Pseudocode allows the designer to focus on the logic of the algorithm without being distracted by details of
language [Link] 'structured' part of pseudocode is a notation for representing three general
programming constructs: sequence, selection and [Link] of these constructs can be embedded
inside any other [Link] constructs represent the logic or flow of control in an algorithm. It has been
proven that three basic constructs for flow of control are sufficient to implement any 'proper' algorithm.
Before going ahead with pseudocode, let us discuss some keywords, which are often used to indicate input,
output and processing operations.
• Input: READ, OBTAIN, GET and PROMPT
• Output: PRINT, DISPLAY and SHOW
• Compute: COMPUTE, CALCULATE and DETERMINE
• Initialize: SET and INITIALIZE
• Add One: INCREMENT
b) Example of pseudocode
The pseudocode given below calculates the area of a rectangle.
Pseudocode: To calculate the area of a rectangle
PROMPT the user to enter the length of the rectangle
PROMPT the user to enter the width of the rectangle
COMPUTE the area by multiplying the length with width
DISPLAY the area
STOP
Note that the actions are flowing in a logical manner, that is, from top to bottom. There is no branching and
no process is repeated again. Each process is contributing something to the next process.
c) Pseudocode Guidelines
Writing pseudocode is not a difficult task. Even if you do not know anything about the computers or
computer languages, you can still develop effective and efficient pseudocodes, if you are writing in an
organized manner.
ALGORITHM STRUCTURES
• The 'structured' part of pseudocode and flowchart is a notation for representing three general
programming constructs: sequence, selection and repetition.
• Sequence, where information can flow in a straight line.
• Selection (branched), where the decisions are made according to some predefined condition.
• Repetition, where the logic can repeat in a loop, that is, where a sequence of steps is repeated until
the desired output is obtained.
IV.1 Sequence
Sequence construct is a linear progression where one task is performed sequentially after another.
Sequential control is indicated by writing one action after another, each action on a line by itself, and all
actions aligned with the logical indent.
IV.2 Selection (Decision)
Selection is a process of deciding which choice is made between two or more alternative courses of action.
Selection logic is depicted as an IF-THEN-ELSE-ENDIF or CASE-ENDCASE structure.
IV.3 Repetition (Looping)
5
Looping construct is used when some particular task(s) is to be repeated for a number of times according to
the specified condition
WHILE-ENDDO
REPEAT-UNTIL
FOR(LOOP)