37
ALGORITHMS AND FLOWCHARTS

ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Embed Size (px)

DESCRIPTION

Why Algorithm is needed? 3 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, similarly for writing a large or good program you need algorithm.

Citation preview

Page 1: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

ALGORITHMS AND FLOWCHARTS

Page 2: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Why Algorithm is needed?

2

Computer Program ?Set of instructions to perform some specific taskIs Program itself a Software ?NO, Program is small part of software.Software merely comprises of Group of Programs

(Source code),Code: refers to statements that are written in

any programming language as machine code or C code.• Code is some time interchangeably used with

program.

Page 3: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Why Algorithm is needed?

3

• 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, similarly for writing a large or good program you need algorithm.

Page 4: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Program design

4

Program Design Process has 2 phases:Problem Solving Phase

Creates an algorithm that solves the problemImplementation (Coding) Phase

Translates the algorithm into a programming language

AlgorithmProblem Program

Page 5: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Algorithms

5

An algorithm is a finite set of steps defining the solution of a particular problem.

Need not to belong one particular languageSequence of English statements can also be

algorithmIt is not a computer programAn algorithm can be expressed in English like

language,called pseudocode, in a programming language or in the form of flowchart.

Page 6: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Algorithm Vs Program

6

What is the difference between an algorithm and a program?

a program is an implementation of an algorithm to be run on a specific computer and operating system.

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.

Page 7: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Algorithm Specification

7

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.

Page 8: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Pseudo code

8

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 Pseudo Code –READ, PRINT, SET, INITIALISE, INCREMENT, IF – THEN – ENDIF, IF –

THEN – ELSE – ENDIF,REPEAT – UNTIL, DO – WHILE etc.

Page 9: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

What is the difference between Algorithm and Pseudocode?

9

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.

Page 10: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

10

Informal definition of an algorithm

Page 11: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

11

Finding the largest integer among five integers

Page 12: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

12

Defining actions in Find Largest algorithm

Page 13: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

13

ALGORITHMALGORITHMREPRESENTATIONREPRESENTATION

Page 14: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

14

Example 1Example 1

Write an algorithm that finds the average of two numbers

Solution:AverageOfTwoInput: Two numbers

1. Add the two numbers2. Divide the result by 23. Return the result by step 2 2

End

Page 15: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

15

Example 2Example 2

Write an algorithm to change a numeric grade to a pass/fail grade.Solution:

Pass/FailGradeInput: One number

1. if (the number is greater than or equal to 40)then 1.1 Set the grade to “pass”else 1.2 Set the grade to “fail”End if

2. Return the gradeEnd

Page 16: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

16

Example 3Example 3

Write an algorithm for grading System of JUET.

Marks range Grade>=80 A>=70 & <80 B>=60 & <70 C>=50 & <60 D<50 F

Page 17: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

17

AlgoForGradeInput: One number

1. if (the number is between 80 and 100, inclusive)then 1.1 Set the grade to “A”End if

2. if (the number is between 70 and 79, inclusive)then 2.1 Set the grade to “B”End if

Algorithm for Grading Algorithm for Grading

Continues on the next slide

SolutionSolution

Page 18: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Dr. Vishnu Sharma18

3. if (the number is between 60 and 69, inclusive)then 3.1 Set the grade to “C”End if

4. if (the number is between 50 and 59, inclusive)then 4.1 Set the grade to “D”End if

5. If (the number is less than 50)then 5.1 Set the grade to “F”End if

6. Return the gradeEnd

Page 19: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

19

Example 5Example 5Write an algorithm to find the largest of 1000 numbers.

FindLargestInput: 1000 positive integers

1. Set Largest to 02. Set Counter to 03. while (Counter less than 1000)

3.1 if (the integer is greater than Largest) then 3.1.1 Set Largest to the value of the integer

End if 3.2 Increment CounterEnd while

4. Return LargestEnd

SolutionSolution

Page 20: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Flowchart

20

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 flowMake our logic more clearHelp during writing of programMake testing and debugging easy

Page 21: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

21

Page 22: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

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.

22

Page 23: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Flowchart Constructs

23

Page 24: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Flowchart Constructs (cont..)

24

Page 25: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Flowchart Constructs (cont..)

25

Page 26: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example-1Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area.

AlgorithmStep 1: Take Width and Length as

inputStep 2: Calculate Area by Width*

Length Step 3: Print Area.

Page 27: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example-1Pseudocode Step 1: Input W,LStep 2: A L x W Step 3: Print A

START

InputW, L

A L x W

STOP

PrintA

Page 28: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example-2Write an Pseudocode and draw a

flowchart that will take marks of four subjects and calculate the average.Then if average marks are greater than 50 then print PASS otherwise print FAIL.

28

Page 29: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example-2Step 1: Input M1,M2,M3,M4Step 2: AVG (M1+M2+M3+M4)/4 Step 3: if (AVG <50) then

Print “FAIL” else

Print “PASS” endif

START

InputM1,M2,M3,M4

AVG(M1+M2+M3+M4)/4

ISAVG<50

STOP

YN

Print“PASS”

Print“FAIL”

Page 30: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example 3Write an algorithm and draw a flowchart to

convert the length in feet to centimeter.Algorithm: Input the length in feet (Lft)Calculate the length in cm (Lcm) by

multiplying LFT with 30Print length in cm (LCM)

Page 31: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example 3Pseudocode Step 1: Input LftStep 2: Lcm Lft x 30 Step 3: Print Lcm

START

InputLft

Lcm Lft x 30

PrintLcm

STOP

Flowchart

Page 32: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example 4 Write an algorithm and draw a flowchart

that will calculate the roots of a quadratic equation

Hint: d = sqrt ( ), and the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a

2 0ax bx c 2 4b ac

Page 33: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example 4Algorithm:Input the coefficients (a, b, c) of the

quadratic equationCalculate dCalculate x1Calculate x2Print x1 and x2

Page 34: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example 4Pseudocode:

Step 1: Input a, b, c Step 2: d sqrt

( ) Step 3: x1 (–b + d) / (2 x a) Step 4: x2 (–b – d) / (2 x a) Step 5: Print x1, x2

START

Inputa, b, c

d sqrt(b x b – 4 x a x c)

Printx1 ,x2

STOP

x1 (–b + d) / (2 x a)

X2 (–b – d) / (2 x a)

4b b a c

Page 35: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example 5Write an algorithm that reads three

numbers and prints the value of the largest number.

Page 36: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Example 5Step 1: Input N1, N2, N3Step 2: if (N1>N2) then

if (N1>N3) then MAX N1 //[N1>N2, N1>N3] else MAX N3 //[N3>N1>N2] endifelse

if (N2>N3) then MAX N2 //[N2>N1, N2>N3] else MAX N3 //[N3>N2>N1] endifendif

Step 3: Print “The largest number is”, MAX

Page 37: ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software

Flow Charts Limitation

For very large program, flow chart goes for many pages

Costly to draw flow charts for large program Difficult to modify

37