Flow Chart

Post on 26-Nov-2014

16 views 2 download

Tags:

transcript

Algorithms

CONCEPTCONCEPTCONCEPTCONCEPT

Figure 8-1Informal definition of an algorithm

used in a computer

Figure 8-2Finding the largest integer

among five integers

Figure 8-3

Defining actions in FindLargest algorithm

Figure 8-4FindLargest refined

Figure 8-5Generalization of FindLargest

THREE CONSTRUCTSTHREE CONSTRUCTSTHREE CONSTRUCTSTHREE CONSTRUCTS

8.28.2

Figure 8-6Three constructs

ALGORITHMALGORITHMREPRESENTATIONREPRESENTATION

ALGORITHMALGORITHMREPRESENTATIONREPRESENTATION

8.38.3

Figure 8-7Flowcharts for three constructs

Figure 8-8Pseudocode for three constructs

Example 1Example 1

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

SolutionSolution

See Algorithm 8.1 on the next slide.

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

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.

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

Example 5Example 5

Write an algorithm to find the largest of 1000 numbers.

SolutionSolution

See Algorithm 8.5 on the next slide.

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

SUBALGORITHMSSUBALGORITHMSSUBALGORITHMSSUBALGORITHMS

8.58.5

Figure 8-9Concept of a subalgorithm

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

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

BASICBASICALGORITHMSALGORITHMS

BASICBASICALGORITHMSALGORITHMS

8.68.6

Figure 8-10Summation

Figure 8-11Product

Figure 8-12Selection sort

Figure 8-13: part IExample of selection sort

Figure 8-13: part IIExample of selection sort

Figure 8-14Selection sort algorithm

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.

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

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.

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.

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.

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.

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.

Flow chart of the while loop :

Flow chart of the for loop:

The flow chart of the if statement:

The flow chart of the if…else statement:

The flow chart of the switch statement:

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

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

Start

Read A, B

Is A > B

Print A Print B

End

Yes No

Flow Chart to find largest of two numbers:

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

NO

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.

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.

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.

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”

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

example

Design withStructure Charts

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

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

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

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

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.

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

Step 2

Adapt the structure chart

to accommodate the

program specifications:

DO A TOP-DOWN DESIGN

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