Beruflich Dokumente
Kultur Dokumente
What
How
Do it
Testing
Test
Use
Errors
Submit Program
Success
Correct Errors
Algorithm
An Algorithm is a solution to a problem that is
independent of any programming language.
An Algorithm is
a finite sequence of steps
each step shall be clearly stated and
understandable
for each input, it shall terminate in finite time
with output
How we solve the problem with Computer?
Algorithm Example
Pseudocode
This is a common notation for writing algorithms.
Mixture of English language statement and a
programming language( like C) code.
It is an intermediate step in writing a program.
Not actually executed on computers.
Standard mathematical notations like
exponents,square-root symbols etc. are allowed in
pseudocode.
Examples
Pseudo code to find out the sum of all
odd numbers in the range 1 to 10.
START
INITIALIZE a[10], sum=0,i
for i=1 to 10 do
Read a[i];
if (condition) then
Calculate sum=sum+a[i];
done
Write sum;
STOP
Examples
Pseudo code to find out the maximum number from 20
given numbers.
START
INITIALIZE a[20],max,i;
for i=1 to 20 do
Read a[i];
done
max=a[1];
for i=2 to 20 do
if a[i]>max then
Assign max=a[i];
done
Write max;
STOP
Practice Example
Write an algorithm for the given problem:
10 students of a class appears for a test in three
subjects. Each test is out of 100 marks. The percentage
of each student has to be calculated and depending on
the percentage calculated, grades are given below:
Percentage
Grade
> =80
60-79
<=59
Flowchart
Graphical representation of an algorithm
Components:
Arrows/lines :Flow of control
Parallelogram: Indicates input and output
operations
Rectangle symbol (action symbol): Indicates
any type of action/computational step
Oval symbol:Indicates the beginning or end
of a program or a section of code
Diamond: Decision
Flowchart Notations
Arrows
Diamond
Parallelogram
Rectangle
Oval
READ
A,B
C=A+B
PRINT
C
STOP
Pseudo code
label 1:
START
INITIALIZE FIG, area=0;
READ FIG ;
IF FIG = circle
CALL CIR_ROUTINE ;
ELSE
IF FIG=rectangle
CALL RECT_ROUTINE ;
ELSE
GOTO label 1;
ENDIF
ENDIF
WRITE area of figure, area;
STOP
CIR_ROUTINE
INITIALIZE radius=0;
READ radius;
CALCULATE area = 3.14 *
radius * radius;
RETURN to Calling
procedure
RECT_ROUTINE
INITIALIZE length=0,
width=0;
READ length,width;
CALCULATE area =
length*width;
RETURN to Calling
procedure
Flow Chart
READ
FIG
START
other
FIG?
rectangle
READ
LENGTH
& WIDTH
circle
READ
RADIUS
AREA=LENGTH * WIDTH
AREA=3.14*RADIUS * RADIUS
PRINT
AREA
STOP
Problems
GCD of three numbers
Calculate 2n
Roots of equation ax2+bx+c=0
Largest of N numbers
Generate the fibonacci series