0 ratings0% found this document useful (0 votes) 22 views37 pages665780086
Supply an demand elactisity
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
ALGORITHMS AND FLOWCHARTSWhy Algorithm is needed?
Programming is both tedious and exciting,
Tedious because like spoken languages programming languages
also have so many demanding rules.
Exciting because writing program provides the programmer with
the chance to create something new also gives challenges of
solving a problem.
It is very difficult to write direct programs in any language, just
like you can not start constructing building without the design of
building
For constructing a building you need design of building, si
ilarly
for writing a large or good program you need algorithm.Why Algorithm is needed?
Programming is both tedious and exciting,
Tedious because like spoken languages programming languages
also have so many demanding rules.
Exciting because writing program provides the programmer with
the chance to create something new also gives challenges of
solving a problem.
It is very difficult to write direct programs in any language, just
like you can not start constructing building without the design of
building
For constructing a building you need design of building, si
ilarly
for writing a large or good program you need algorithm.Program design
Problem | nN | Program
Igorithm
¢ Program Design Process has 2 phases:
e Problem Solving Phase
Creates an algorithm that solves the problem
¢ Implementation (Coding) Phase
Translates the algorithm into a programming languageAlgorithms
An algorithm is a finite set of steps defining the solution of a
particular problem.
° Need not to belong one particular language
© Sequence of English statements can also be algorithm
© It is not a computer program
© An algorithm can be expressed in English like language, called
pseudocode, in a programming language or in the form of
flowchart.Algorithm Vs Program
© What is the difference between an algorithm and a program?
O a program is an implementation of an algorithm to be run on a
specific computer and operating system.
O an algorithm is more abstract — it does not deal with machine
specific details — think of it as a method to solve a problem.
* What is good algorithm?
Efficient algorithms are good, we generally measure efficiency of an
algorithm on the basis of:
1. Time: algorithm should take minimum time to execute.
2. Space: algorithm should use less memory.Algorithm Specification
Every algorithm must satisfy the
following criteria:
° Input. Zero or more quantities are externally supplied.
© Output. At least one quantity is produced.
© Definiteness. Each instruction must be clear and
unambiguous(Unique meaning).
¢ Finiteness. An algorithm terminates in a finite number of steps.
¢ Effectiveness. Every instruction must be basic enough to be
carried out than, means not so complex.Pseudo code
Pseudocode is one of the methods that could be used to represent an
algorithm.
It is not written in a specific syntax that is used by a programming
language and therefore cannot be executed in a computer.
There are lots of formats used for writing pseudocodes and most of
them borrow some of the structures from popular programming
languages such as C, Lisp, FORTRAN, etc.
Also, natural language is used in Pseudocode when presenting details
that are not important.
Most of the algorithms are presented using pseudocode since they
can be read and understood using programmers who are familiar
with different programming languages.
Some programming constructs used for Pscudo Code —
READ, PRINT, SET, INITIALISE, INCREMENT, IF -THEN — ENDIF, IF —
THEN — ELSE — ENDIF,REPEAT — UNTIL, DO — WHILE ete.What is the difference between
Algorithm and Pseudocode?
An algorithm is a well defined sequence of steps that provides
a solution for a given problem, while a pseudocode is one of
the methods that can be used to represent an algorithm.
© While algorithms can be written in natural language,
pseudocode is written in a format that is closely related to
high level programming language structures.
° But pseudocode does not use specific programming language
syntax and therefore could be understood by programmers
who are familiar with different programming languages.
Additionally, transforming an algorithm presented in
pseudocode to programming code could be much easier than
converting an algorithm written in natural language.Informal definition of an algorithm
Input List
Algorithm
Output ListFinding the largest integer
among five integers
Input List
FindLargest
oe Ce) ooo
Sept - a - .
Step 2
ae Output ResultDefining actions in Find Largest algorithm
—
FindLargest
Step 1
(|
ae ‘Output ResultIKE ees Ny
REPRESENTATIONExample-1
Pseudocode
© Step 1: Input W,L
© Step 2: AM@LxW
© Step 3: Print AExample-2
Input
M1.M2,.M3,M4
AYGm(MI+M24M34M4)/4
1s y
AVGN2) then
if (NI>N3) then
MAX # NI
else
MAX mN3
endif
else
if (N2>N3) then
MAX m N2
else
MAX wm N3
endif
endif
//{N1>N2, NI>N3]
//{N3>NI>N2]
//[N2>N1, N2>N3]
//{N3>N2>N1]
Step 3: Print “The largest number is”, MAXFlowchart
A graphical representation of an algorithm, often used
in the design phase of programming to work out the
logical flow of a program.
° Visual way to represent the information flow
© Make our logic more clear
° Help during writing of program
° Make testing and debugging easyName Symbol
Flow line ———_ >
Paralletogram
Rectangle
‘Use in flowchart
Denotes the beginning or end of a program.
Denotes the direction of logic flow in @
program.
Denotes either an input operation (e.g., INPUT)
‘oF an output operation (e.g, PRINT).
Denotes a process to be carried out
(eg., an addition).
Denotes a decision (or branch) to be made. The
Program should continue along one of two routes
(eg., IFTHEN/ELSE).Flowchart or program constructs
| Sequence: The order of execution, this typically refers to the order
in which the code will execute. Normally code executes line by
line, so line 1 then 2 then 3 and so on.
Selection: Selection, like branching, is a method of controlling the
execution sequence, you can create large control blocks, using if
statements testing a condition, or switch statements
evaluating a variable etc to control and change the execution of the
program depending on this environment and changing variables,
Iteration (Repetition): Iteration is typically used to refer to
collections and arrays of variables and data. Repeating set of
instruction. Counting from 1 to 10, you are iterating over the first
10 numbers, for, while, do-while loops will be implemented for
iteration.Flowchart Constructs
SequenceFlowchart Constructs (cont..)
Repetition
yes No
ey
=Flowchart Constructs (cont..)
Repetition
yes No
ey
=Example-1
Pseudocode
© Step 1: Input W,L
© Step 2: AM@LxW
© Step 3: Print AExample-1
Pseudocode
© Step 1: Input W,L
© Step 2: AM@LxW
© Step 3: Print AExample-2
Input
M1.M2,.M3,M4
AYGm(MI+M24M34M4)/4
1s y
AVGN2) then
if (NI>N3) then
MAX # NI
else
MAX mN3
endif
else
if (N2>N3) then
MAX m N2
else
MAX wm N3
endif
endif
//{N1>N2, NI>N3]
//{N3>NI>N2]
//[N2>N1, N2>N3]
//{N3>N2>N1]
Step 3: Print “The largest number is”, MAXExample 5
Step 1: Input N1,N2,N3
Step 2: if (NI>N2) then
if (NI>N3) then
MAX # NI
else
MAX mN3
endif
else
if (N2>N3) then
MAX m N2
else
MAX wm N3
endif
endif
//{N1>N2, NI>N3]
//{N3>NI>N2]
//[N2>N1, N2>N3]
//{N3>N2>N1]
Step 3: Print “The largest number is”, MAX/
Flow Charts Limitation
© For very large program, flow chart goes for many pages
Costly to draw flow charts for large program
Difficult to modify