Top Banner
ALGORITHM
40
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: algorithm

ALGORITHM

Page 2: algorithm

Definition:

• Algorithm is a finite sequence of instructions required for producing the desired result.

• It is also defined as “any problem whose solution can be expressed in a list of executable instruction.”

Page 3: algorithm

Characteristics:

• The steps in the algorithm must be unambiguous .

• It should be written in sequence.• Ensure that the algorithm will terminate.• It should conclude after a finite number of

steps.

Page 4: algorithm

Quality of good algorithm:

• Time• Memory• Accuracy• Sequence • Generality

Page 5: algorithm

Representation of algorithm:

• Normal English -The algorithm is represented in step by step sequential order in normal English.

• Flow chart- It is a pictorial representation of an algorithm.

• Pseudo code –it is a formal design tool and utilized very well with the rules of structure design and programming.

Page 6: algorithm

Contd..

• Decision table- it is used to helps a lot in designing a specific segment of a design.

• Program - it is represented as a program using any high level language.

Page 7: algorithm

Example

• Addition of two numbersStep1: StartStep2: Read a, bStep3: Add the value of a with b and

store the result in c.Step4: Display the value of cStep5: Stop

Page 8: algorithm

PSEUDO CODE

Page 9: algorithm

Definition:

• Pseudo means imitates and code means instruction.

• It is formal design tool.• It is also called Program Design Language.

Page 10: algorithm

Keywords

• READ,GET• PRINT,DISPLAY• COMPUTE,CALCULATE

Page 11: algorithm

Guidelines for writing Pseudo code

• Steps should be understandable• Capitalize the keyword.• Indent to show hierarchy.• End multiple line structure etc,.

Page 12: algorithm

Example

READ a,bC=a+bWRITE Cstop

Page 13: algorithm

Example

READ a,bIF a>b

PRINT a is greaterELSE

PRINT b is greaterENDIFstop

Page 14: algorithm

Advantage & Disadvantage

• It can be easily modified• It can be understood easily• Compare to flowchart it is difficult to

understand the program logic.

Page 15: algorithm

Flowcharts

• It is the pictorial representation of the algorithm.

• A flow chart, or flow diagram, is a graphical representation of a process or system that details the sequencing of steps required to create output.

• A flowchart is a picture of the separate steps of a process in sequential order.

Page 16: algorithm

Flowchart Symbols

• Terminal symbol– It is used to represent the start, end of the

program logic.

• Input/Output– It is used for input or output.

• Process Symbol– It is used to represent the calculations, data

movements, initialization operations etc,.

Page 17: algorithm

• Decision Symbol

– It is used to denote a decision to be made at that point

• Flow lines– It is used to connect the symbols

• Connectors– It is used to connect the flow lines.

Page 18: algorithm

Rules for preparing flowcharts

• It should be simple.• Standard symbols should be used.• The flow lines should not intersect each

others.• In case of complex flowcharts use the

connectors symbols.

Page 19: algorithm

• Only one flow line should enter the process symbol and only one flow line should come out from a process symbol.

• Only one flow line used with the terminal symbol.

STARTSTOP

Page 20: algorithm

• Only one flow line should enter the decision symbol and two or three flowlines may leave from the decision symbol.

Page 21: algorithm

Benefits of Flowcharts

• Makes Logic Clear• Communication• Effective Analysis• Useful in coding • Useful in Testing etc,.

Page 22: algorithm

Limits of Flowcharts

• It is difficult to use flowcharts for large program

• Difficult to modify• Cost etc,.

Page 23: algorithm

Program control structures:

• Program control structure are defined as the program statements that specifies the order in which statements are executed.

• Types:– Sequence control structure– Selection control structure– Repetition control structure

Page 24: algorithm

• The instructions are computed in sequence i.e. it performs instruction one after another.

• It uses top-down approach.

Sequence control structure

Page 25: algorithm

Sequence control structure

Flow chart Pseudo code

Process 1Process 2

Process nProcess 2

Process n

Process 1

Page 26: algorithm

Example

START

C=a+b

Print c

Read a,b

STOP

Page 27: algorithm

SELECTION CONTROL STRUCTURE

• It is used for making decisions.• It allows the program to make a choice

from alternative paths. • IF …THEN• IF …THEN… ELSE• CASE etc.,

Page 28: algorithm

IF…THEN

Pseudo code Flow chartIF condition THEN

process 1..

END IF..

If condition

NO

YES

Process 1

Page 29: algorithm

ExampleStart

Read a

If a>0

Print a is Positive

Stop

no

yes

Page 30: algorithm

IF…THEN…ELSE

Pseudo code Flowchart

IF condition THENprocess 1

.

.ELSE

process 2..

END IF..

If condition

YES NO

Process 1 Process 2

Page 31: algorithm

Example

Start

Read a,b

If a>b

Print a is GreaterPrint b is Greater

Stop

no

yes

Page 32: algorithm

CASE structurePseudo code Flow chart

.

.CASE TypeCase Type-1:

Process 1Case Type-2:

Process 2..

Case Type-n:Process n..

END CASE

Type 1

Type 2

Type 3

Process 1

Process 2

Process 3

no

no

no

yes

yes

yes

Page 33: algorithm

start

stop

Read m1,m2,m3

Avg=(m1+m2+m3)/3

If Avg>=60

If Avg>=50

If Avg>=35

Fail

Print First Class

Print Second Class

Print Third Class

Example: Finding the Grade

Page 34: algorithm

Repetition control structure(looping)

• It is used to execute some instructions several time based on some condition.

• WHILE loop• Do…WHILE loop etc.,

Page 35: algorithm

WHILE Loop

Pseudo code Flow chart

WHILE condition..

Body of the loop..

END WHILEBody of The loop

conditionno

yes

Page 36: algorithm

ExampleStart

Num=0

Num=Num+1

Print Num

whileNum<5

stop

no

yes

Page 37: algorithm

DO…WHILE Loop

Pseudocode Flow chartDO

.

.Body of the loop

.

.WHILE condition

.

.

END WHILE

Body of The loop

condition

no

yes

Page 38: algorithm

ExampleStart

Num=0

Num=Num+1

Print Num

whileNum<5

stop

no

yes

Page 39: algorithm

Example: Finding the area of a circle

Algorithm Step1: StartStep2: Read the value of rStep3: Calculate area = 3.14*r*rStep4: Print areaStep5: Stop

Page 40: algorithm