introduction of flowchart 3664210

35
Introduction To Flowcharting

Upload: zaile-balaba

Post on 29-Oct-2014

40 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction of Flowchart 3664210

Introduction To Flowcharting

Page 2: Introduction of Flowchart 3664210

Today’s Topics

• Flowchart Symbols• Structures•Sequence•Selection•Repetition

Page 3: Introduction of Flowchart 3664210

Flowchart:

Represents an algorithm in graphical symbols

Page 4: Introduction of Flowchart 3664210

Terminal: Used to indicates the start and end of a flowchart. Single flow line. Only one “Start” and “Stop” terminal for each program. The end terminal for function/subroutine must use “Return” instead of “Stop”.

Process: Used whenever data is being manipulated. One flow line enters and one flow line exits.

Input/Output: Used whenever data is entered (input) or displayed (output). One flow line enters and one flow line exits.

Flowchart Symbols

Page 5: Introduction of Flowchart 3664210

Decision: Used to represent operations in which there are two possible selections. One flow line enters and two flow lines (labeled as “Yes” and “No”) exit.

Function / Subroutine: Used to identify an operation in a separate flowchart segment (module). One flow line enters and one flow line exits.

On-page Connector: Used to connect remote flowchart portion on the same page. One flow line enters and one flow line exits.

Off-page Connector: Used to connect remote flowchart portion on different pages. One flow line enters and one flow line exits.

Comment: Used to add descriptions or clarification.

Flow line: Used to indicate the direction of flow of control.

Flowchart Symbols

Page 6: Introduction of Flowchart 3664210

Comments or descriptionStart

Read N, M

No

Yes

Stop

N = The number of studentsM = The number of subjects

Page 7: Introduction of Flowchart 3664210

Connectors on the same page

Start

2

1

1 2

Stop

1- connection on the same flowchart portion

2- connection on the different flowchart portion

Page 8: Introduction of Flowchart 3664210

Connectors on a different page Page 1 Page 2

Start

No

1

Yes 1

2

Stop

2

Page 9: Introduction of Flowchart 3664210

FunctionPage 1

AVRG (result, n1, n2,n3)

Start

Stop

Readn1, n2 , n3

Printresult

Page 2

AVRG ( result,n1, n2,n3)

Return

sum = n1+ n2+n3

result = sum/3

End terminal must be a “Return”

Start terminal for aFunction is different.Do not use “Start”

The detail of how the function works is put in another flowchart.

This is known as Function-Definition

At this point, we only focus on whatto do. How to do it, it comes later.

This part is known asFunction-Call

Body of a function is the same with normal flowchart

This flowchart calculates the average of three numbers

Page 10: Introduction of Flowchart 3664210

The main flowcharting structures

1.Sequence2.Selection3.Repetition

A flowchart expressing the solution to an involved problem may have:

1.the main program flowchart on one page

2.with subprograms continuing the problem solution on subsequent pages.

Page 11: Introduction of Flowchart 3664210

Each of the five acceptable structures can be built from the basic elements

as shown below.

Page 12: Introduction of Flowchart 3664210

Each of the five acceptable structures can be built from the basic elements

as shown below.

Page 13: Introduction of Flowchart 3664210

Each of the five acceptable structures can be built from the basic elements

as shown below.

Page 14: Introduction of Flowchart 3664210

SequenceIn a computer program or an algorithm,

sequence involves simple steps which are to be executed one after the other.

The steps are executed in the same order in which they are written.

In pseudocode, sequence is expressed as:

process 1process 2……process n

In a flowchart, sequence is expressed as:

Page 15: Introduction of Flowchart 3664210

SequenceAn Example Using Sequence

Problem: Write a set of instructions that describe how to make a pot of tea.

Pseudocode

BEGINfill a kettle with waterboil the water in the kettleput the tea leaves in the potpour boiling water in the pot

END

Flowchart

Page 16: Introduction of Flowchart 3664210

Binary Selection

In pseudocode, binary selection is expressed in the following ways:

1. IF condition THENprocess 1

ENDIF

2. IF condition THENprocess 1

ELSEprocess 2

ENDIF

Binary Selection

In flowcharts, binary selection is expressed in the following ways:

Selection is used in a computer program or algorithm

to determine which particular step or set of steps is to be executed

Page 17: Introduction of Flowchart 3664210

SelectionBinary (structure)

Binary Selection

In pseudocode, binary selection is expressed in the following ways:

1. IF condition THENprocess 1

ENDIF

2. IF condition THENprocess 1

ELSEprocess 2

ENDIF

Binary Selection

In flowcharts, binary selection is expressed in the following ways:

Page 18: Introduction of Flowchart 3664210

SelectionBinary (flowchart structure)

Note: In a flowchart it is most important to indicate 1. which path is to be followed when the condition is true, and 2. which path to follow when the condition is false.

Without these indications the flowchart is open to more than one interpretation.

Note: There are two acceptable ways to represent a decision in all of the structures.Either method is acceptable. For consistency, the method 1 is used throughout this document.

1. The condition is expressed as a statement and the two possible outcomes are indicated by

• True• False

2. The condition is expressed as a question and the two possible outcomes are indicated by

• Yes• No

Page 19: Introduction of Flowchart 3664210

SelectionBinary (examples)

Examples Using Binary SelectionProblem 1: Write a set of instructions to describe when to answer the phone.

Binary SelectionPseudocode

IF the telephone is ringing THEN

answer the telephone

ENDIF

Binary SelectionFlowchart

Page 20: Introduction of Flowchart 3664210

SelectionBinary (examples)

Examples Using Binary SelectionProblem 2: Write a set of instructions to follow when approaching a set of traffic control lights.

Binary SelectionPseudocode

IF the signal is green THEN

proceed through the intersection

ELSEstop the vehicle

ENDIF

Binary SelectionFlowchart

Page 21: Introduction of Flowchart 3664210

SelectionMulti-way (structure)

Multi-way Selection

In pseudocode, multiple selection is expressed as:

CASEWHERE expression evaluates to

choice a : process achoice b : process b . . . . . .

OTHERWISE : default processENDCASE

Note: As the flowchart version of the multi-way selection indicates, only one process on each pass is executed as a result of the implementation of the multi-way selection.

Multi-way Selection

In flowcharts, multi-way selection is expressed as:

Page 22: Introduction of Flowchart 3664210

SelectionMulti-way (examples)

Example Using Multi-way SelectionProblem: Write a set of instructions that describes how to:

respond to all possible signals at a set of traffic control lights.

Multi-way SelectionPseudocode

CASEWHERE signal isred : stop the vehicleamber : stop the vehiclegreen : proceed through the intersection

OTHERWISE : proceed with cautionENDCASE

Multi-way SelectionFlowchart

Page 23: Introduction of Flowchart 3664210

RepetitionRepetition allows for a portion of an algorithm or

computer programto be done any number of times

dependent on some condition being met. An occurrence of repetition is usually known as a loop.

An essential feature of repetition is that each loop has a termination condition

to stop the repetition, or the obvious outcome is that

the loop never completes execution (an infinite loop).

The termination condition can be checked or tested 1. at the beginning and is known as a pre-test

loop or2. at the end of the loop and is known as a post-

test loop.

Page 24: Introduction of Flowchart 3664210

Repetition Pre-test (structure)

Repetition: Pre-TestA pre-tested loop is so named because the condition has to be met at

the very beginning of the loop or the body of the loop is not executed.

This construct is often called a guarded loop. The body of the loop is executed repeatedly while the termination condition is true.Repetition

In pseudocode, pre-test repetition is expressed as:

WHILE condition is trueprocess(es)

ENDWHILE

Repetition

In flowcharting pre-test repetition is expressed as:

Page 25: Introduction of Flowchart 3664210

Repetition Post-test (structure)

Repetition: Post-Test• A post-tested loop executes the body of the loop before testing the

termination condition. • This construct is often referred to as an unguarded loop. • The body of the loop is repeatedly executed until the termination condition is

true.An important difference between a pre-test and post-test loop is that the statements of a post-test loop are executed at least once even if the condition is originally true, whereas the body of the pre-test loop may never be executed if the termination condition is originally true. A close look at the representations of the two loop types makes this point apparent.Repetition

In pseudocode, post-test repetition is expressed as:

REPEATprocess

UNTIL condition is true

Repetition

In a flowchartpost-test repetition is expressed as:

Page 26: Introduction of Flowchart 3664210

Repetition Pre-test (example)

An Example Using Pre-Test RepetitionProblem: Determine a safety procedure for travelling in a carriage on a moving train.

Pre-test RepetitionPseudocode

WHILE the train is moving

keep wholly within the carriage

ENDWHILE

Pre-test RepetitionFlowchart

Page 27: Introduction of Flowchart 3664210

Repetition Post-test (example)

An Example Using Post-Test RepetitionProblem: Determine a procedure to beat egg whites until fluffy.

Post-test RepetitionPseudocode

REPEATbeat the egg whites

UNTIL fluffy

Post-test RepetitionFlowchart

Page 28: Introduction of Flowchart 3664210

Example:

Start

Stop

ReadLength,Width

PrintArea,

Perimeter

Calculate AreaArea=Length * Width

Calculate PerimeterPerimeter=

2 * (Width+Length)

Input:Length <- 5Width <- 3

Process:Area = 5 * 3 = 15

Process:Perimeter = 2* (5+3) = 16

Output

Area: 15Perimeter: 16

Page 29: Introduction of Flowchart 3664210

Start

Stop

Read Num

Print"Category A"

Yes

Num>0? No

Print"Category B"

Example: What is the output of the following flowchart when the input Num= 10

Num = 1010 > 0 ? => YES

Input:Num <- 10

Enter a Number >> 10

Output:“Category A”

Category A

Page 30: Introduction of Flowchart 3664210

Start

Stop

Read Num

Print"Category A"

Yes

Num>0? No

Print"Category B"

Example: What is the output of the following flowchart when the input is Num= 0

Num = 00 > 0 ? => NO

Output:“Category B”

Input:Num <- 0

Enter a Number >> 0

Category B

Output:“Category A”

Category A

Page 31: Introduction of Flowchart 3664210

Start

Stop

PrintResult

Result=Result + Count

Count=Count - 1

Initialize

Result=0Count=Num

Count>0?

Read Num

No

Print Count

Yes

Example: What is the output of the following flowchart when the input is Num= 4

Input:Num <- 4

Enter a Number => 4

Variables (in memory):

Num [ ]Result [ ]Count [ ]

Variables (in memory):

Num [ 4 ]Result [ ]Count [ ]

Count = 44 > 0 ? => YES

Variables (in memory):

Num [ 4 ]Result [ 0 ]Count [ 4 ]

Count: 4

Variables (in memory):

Num [ 4 ]Result [ 4 ] 0 + 4Count [ 3 ] 4 - 1

Count: 3

Count = 33 > 0 ? => YES

Variables (in memory):

Num [ 4 ]Result [ 7 ] 4 + 3Count [ 2 ] 3 - 1

Count: 2

Count = 22 > 0 ? => YES

Variables (in memory):

Num [ 4 ]Result [ 9 ] 7 + 2Count [ 1 ] 2 - 1

Count: 1

Count = 11 > 0 ? => YES

Variables (in memory):

Num [ 4 ]Result [ 10] 9 + 1Count [ 0 ] 1 - 1

Count: 0

Count = 00 > 0 ? => NO

Result: 10

Page 32: Introduction of Flowchart 3664210

Page 1

AVRG (average, 10, 5, N)

Start

Stop

ReadN

Printaverage

Page 2

AVRG ( result,n1, n2,n3)

Return

sum = n1+ n2+n3

result = sum/3

Example: What is the output of the following flowchart when the input is N = 6

average

10

5

N=6

Sum = 10 + 5 + 6

average = 21/3

Output:Average: 7

Page 33: Introduction of Flowchart 3664210

T. O. L

Page 34: Introduction of Flowchart 3664210

Quiz

1. What is a flowchart?2. It is used to connect remote

flowchart portion on the same page. One flow line enters and one flow line exits.

3-5. Control Structures of Flowchart.