Flow Chart

60
Algorithms

Transcript of Flow Chart

Page 1: Flow Chart

Algorithms

Page 2: Flow Chart

CONCEPTCONCEPTCONCEPTCONCEPT

Page 3: Flow Chart

Figure 8-1Informal definition of an algorithm

used in a computer

Page 4: Flow Chart

Figure 8-2Finding the largest integer

among five integers

Page 5: Flow Chart

Figure 8-3

Defining actions in FindLargest algorithm

Page 6: Flow Chart

Figure 8-4FindLargest refined

Page 7: Flow Chart

Figure 8-5Generalization of FindLargest

Page 8: Flow Chart

THREE CONSTRUCTSTHREE CONSTRUCTSTHREE CONSTRUCTSTHREE CONSTRUCTS

8.28.2

Page 9: Flow Chart

Figure 8-6Three constructs

Page 10: Flow Chart

ALGORITHMALGORITHMREPRESENTATIONREPRESENTATION

ALGORITHMALGORITHMREPRESENTATIONREPRESENTATION

8.38.3

Page 11: Flow Chart

Figure 8-7Flowcharts for three constructs

Page 12: Flow Chart

Figure 8-8Pseudocode for three constructs

Page 13: Flow Chart

Example 1Example 1

Write an algorithm in pseudocode that finds the average of two numbers

SolutionSolution

See Algorithm 8.1 on the next slide.

Page 14: Flow Chart

AverageOfTwoInput: Two numbers

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

End

Algorithm 8.1:Algorithm 8.1:Average of twoAverage of two

Page 15: Flow Chart

Example 4Example 4

Write an algorithm to find the largest of a set of numbers. You do not know the number of numbers.

SolutionSolution

See Algorithm 8.4 on the next slide.

Page 16: Flow Chart

FindLargestInput: A list of positive integers

1. Set Largest to 02. while (more integers)

2.1 if (the integer is greater than Largest) then 2.1.1 Set largest to the value of the

integer End ifEnd while

3. Return LargestEnd

Algorithm 8.4:Algorithm 8.4: Find largestFind largest

Page 17: Flow Chart

Example 5Example 5

Write an algorithm to find the largest of 1000 numbers.

SolutionSolution

See Algorithm 8.5 on the next slide.

Page 18: Flow Chart

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

Algorithm 8.5:Algorithm 8.5:Find largest of 1000 numbersFind largest of 1000 numbers

Page 19: Flow Chart

SUBALGORITHMSSUBALGORITHMSSUBALGORITHMSSUBALGORITHMS

8.58.5

Page 20: Flow Chart

Figure 8-9Concept of a subalgorithm

Page 21: Flow Chart

FindLargestInput: A list of positive integers

1. Set Largest to 02. while (more integers)

2.1 FindLargerEnd while

3. Return LargestEnd

Algorithm 8.6:Algorithm 8.6: Find largestFind largest

Page 22: Flow Chart

FindLargerInput: Largest and current integer

1. if (the integer is greater than Largest)then 1.1 Set Largest to the value of the integerEnd ifEnd

Subalgorithm:Subalgorithm: Find largerFind larger

Page 23: Flow Chart

BASICBASICALGORITHMSALGORITHMS

BASICBASICALGORITHMSALGORITHMS

8.68.6

Page 24: Flow Chart

Figure 8-10Summation

Page 25: Flow Chart

Figure 8-11Product

Page 26: Flow Chart

Figure 8-12Selection sort

Page 27: Flow Chart

Figure 8-13: part IExample of selection sort

Page 28: Flow Chart

Figure 8-13: part IIExample of selection sort

Page 29: Flow Chart

Figure 8-14Selection sort algorithm

Page 30: Flow Chart

Flowchart:

What is a Flowchart? The flowchart is a means of visually presenting the flow

of control through an information processing systems, the operations performed within the system and the sequence in which they are performed.

It is a graphic representation of how a process works, showing, at a minimum, the sequence of steps.

Flowcharts are generally drawn in the early stages of formulating computer solutions.

Page 31: Flow Chart

Flowchart (Contd…):

Guideline for drawing a flowchart: Flowcharts are usually drawn using some standard

symbols; Some standard symbols, which are frequently required for flowcharting many computer programs are shown below

Page 32: Flow Chart

Principles of Programming - NI July 2005 32

Flowchart Symbols

Terminal symbol - indicates the beginning and end points of an algorithm.

Process symbol - shows an instruction other thaninput, output or selection.

Input-output symbol - shows an input or an output operation.

Disk storage I/O symbol - indicates input from or output to disk storage.

Printer output symbol - shows hardcopy printeroutput.

Page 33: Flow Chart

Principles of Programming - NI July 2005 33

Flowchart Symbols cont…

Selection symbol - shows a selection processfor two-way selection.

Off-page connector - provides continuation of a logical path on another page.

On-page connector - provides continuationof logical path at another point in the samepage.

Flow lines - indicate the logical sequence ofexecution steps in the algorithm.

Page 34: Flow Chart

Flowchart (Contd…):

A set of useful standard Flowchart symbols: Rounded box use it to represent an event which occurs automatically. Rectangle or box use it to represent an event which is controlled within

the process. Typically this will be a step or action which is taken.

Diamond use it to represent a decision point in the process. Circle use it to represent a point at which the flowchart

connects with another process.

Page 35: Flow Chart

ADVANTAGES OF USING FLOWCHARTS: Communication: Flowcharts are better way of

communicating the logic of a system Effective analysis: Problem can be analyzed in more

effective way. Proper documentation: Flowcharts serve as a good

program documentation Efficient Coding: Flowcharts act as a guide or blueprint

during the systems analysis and program development phase.

Page 36: Flow Chart

ADVANTAGES OF USING FLOWCHARTS (Contd…): Proper Debugging: Flowchart helps in debugging

process.

Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart.

Page 37: Flow Chart

Flow chart of the while loop :

Page 38: Flow Chart

Flow chart of the for loop:

Page 39: Flow Chart

The flow chart of the if statement:

Page 40: Flow Chart

The flow chart of the if…else statement:

Page 41: Flow Chart

The flow chart of the switch statement:

Page 42: Flow Chart

Flowchart for finding the sum of first five natural numbers ( i.e. 1,2,3,4,5):

Page 43: Flow Chart

Flowchart (Example):Flowchart to find the sum of first 50 natural numbers.

Page 44: Flow Chart

Start

Read A, B

Is A > B

Print A Print B

End

Yes No

Flow Chart to find largest of two numbers:

Page 45: Flow Chart

Flowchart to find the largest of three numbers A,B, and C:

NO

Page 46: Flow Chart

LIMITATIONS OF USING FLOWCHARTS: Complex logic: Sometimes, the program logic is quite

complicated. In that case, flowchart becomes complex and clumsy.

Alterations and Modifications: If alterations are required the flowchart may require re-drawing completely.

Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem.

Page 47: Flow Chart

Pseudocode Pseudocode is a shorthand notation for programming which uses a

combination of informal programming structures and verbal descriptions of code.

In general, pseudocode is used to outline a program before translating it into proper syntax. This helps in the initial planning of a program, by creating the logical framework and sequence of the code. An additional benefit is that because pseudocode does not need to use a specific syntax, it can be translated into different programming languages and is therefore somewhat universal. It captures the logic and flow of a solution without the bulk of strict syntax rules.

Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is very similar to everyday English.

Page 48: Flow Chart

Pseudocode & Algorithm

Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.

Page 49: Flow Chart

Pseudocode & Algorithm

Pseudocode: Input a set of 4 marks Calculate their average by summing and dividing

by 4 if average is below 50

Print “FAIL”else

Print “PASS”

Page 50: Flow Chart

Pseudocode & Algorithm

Detailed Algorithm Step 1: Input M1,M2,M3,M4

Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then

Print “FAIL” else

Print “PASS”endif

Page 51: Flow Chart

example

Page 52: Flow Chart

Design withStructure Charts

Page 53: Flow Chart

Design Process

Problem solving and design should be done independent of programming.

Code significantly clouds the design process.. It is very difficult to see the design and address design issues when coding

Design needs to take place in a mode which minimizes code influence

Page 54: Flow Chart

4 Steps

Create a structure chart based on the main

item,NOT programming

requirements

1

Modify structure chart based on the programming requirements.

Top-down design.

2

Write code toimplement the

top-down design.As you mature thiswill be skipped by going to step 4.

3

Improve readabililtyof the code by incorporating

functions.

4

Page 55: Flow Chart

National Parcel ExampleExample 2 : Parcel Service Company.

Emphasizes IF notation for top down design.

National Parcel Service (NPS) specializes in nationwide delivery of small packages. NPS will not accept

any packages whose largest dimension is greater than 3 feet or whose weight exceeds 50 pounds.

The charge for shipping a parcel is $0.75 plus an amount based on package weight as follows:

Weight (lb.) Rate-----------------------------20 or less $0.08 per lb.40 or less $0.10 per lb.Over 40 $0.15 per lb.-----------------------------

There is an additional $1.00 charge if the volume of the package exceeds 18 cubic feet. Write a program

that will read the dimensions of a parcel (in feet) and its weight (in pounds) and then compute and print

the postage due. If the package is rejected, an appropriate message should be printed.

Example Test Data Length Width Depth Weight 1.5 1.2 0.8 6.0

Page 56: Flow Chart

Parcel Delivery

National Parcel Service

Parcel *

Acceptable Not Acceptable

Shipping Status

Charge

Weight Charge Volume Charge 0.75

Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18

WeightSize

Length Width Depth

Page 57: Flow Chart

Change in Perspective

Our natural view of the problem domain may not match the requirements.

There are multiple views of anything. Other views are not wrong, but do provide

insight into the structure we expect and perceive.

In this problem we need to adjust for a single parcel.

Page 58: Flow Chart

Parcel Delivery(adjust to one package)

Parcel

Acceptable Not Acceptable

Shipping Status

Charge

Weight Charge Volume Charge 0.75

Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18

WeightSize

Length Width Depth

Page 59: Flow Chart

Step 2

Adapt the structure chart

to accommodate the

program specifications:

DO A TOP-DOWN DESIGN

Page 60: Flow Chart

Parcel DeliveryAdjusting Structure Chart for Program (Top-Down) Design

Compute Postage

Acceptable Not Acceptable

Determine Acceptability

Compute Charge

ComputeWeight Charge

ComputeVolume Charge

ComputeTotal Charge

Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18

WeightSize

Length Width Depth

Input Package Info

Output ChargeOutput Rejection

Message

Wt_chrg=Wt*0.08

Wt_chrg=Wt*0.1

Wt_chrg=Wt*0.15

Vol_chrg=0.0

Vol_chrg=1.0

Charge=Wt_Chrg+Vol_Chrg+

0.75