MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

Embed Size (px)

Citation preview

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    1/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 1

    Introduction To

    Flowcharting

    MELJUN CORTES

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    2/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 2

    Todays Topics Flowchart Symbols

    Control Structures

    Some examples

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    3/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 3

    Flowchart:

    Represents an algorithm in graphical symbols

    Example: Algorithm for multiplying two

    numbersS t a r t

    S t o p

    G e t A

    G e t B

    D i s p l a y t h e

    R e s u lt C

    C a l c u l a te R e s u t

    C = A * B

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    4/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 4

    Terminal: Used to indicates the start and end of aflowchart. 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. Oneflow line enters and one flow line exits.

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

    exits.

    Decision: Used to represent operations in which there aretwo 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 flowchartportion on the same page. One flow line enters and one

    flow line exits.

    Off-page Connector: Used to connect remote flowchartportion 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 ofcontrol.

    Flowchart Symbols

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    5/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 5

    Example:

    S t r t

    S t

    d

    d

    is l t

    s l t

    lc l t s t*

    Start Terminal.Program starts

    here

    Stop TerminalProgram endshere

    Input.Enter values forA and B

    Process

    Output

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    6/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 6

    Comments or description

    St

    rt

    d N,

    M

    N

    Ys

    St

    N T n mb r f st d nts

    M

    T

    nmb

    r

    f s

    bj

    cts

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    7/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 7

    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

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    8/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 8

    Connectors on a different page

    Page 1 Page 2

    St

    rt

    N

    Y

    s

    St

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    9/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 9

    Function

    Page 1

    AVRG (result, 1, 2, 3)

    Start

    Stop

    Rea

    1, 2 , 3

    Pri t

    result

    Page 2

    AVRG ( result, 1, 2, 3)

    Retur

    su = 1+ 2+ 3

    result = su /3

    End terminal

    must be a Return

    Start terminal for aFunction is different.

    Do not use Start

    The detail of how the function worksis 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 isthe same withnormal flowchart

    This flowchart calculates the average of three numbers

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    10/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 10

    Related terms and concepts

    Page 1

    AVRG (result, 1, 2, 3)

    Start

    Stop

    Rea

    1, 2 , 3

    Pri t

    result

    Page 2

    AVRG is the function name

    Objects enclosed by ( ) result,n1, n2, n3 - are called parameters

    Parameters used in a function-call

    are called actual parameters

    result, n1, n2, n3 are actual parameters

    Parameters used in a function-definitionare called formal parameters

    R, a, b, c are formal parameters

    Each formal parameter representsan actual parameter accordingto its order:

    R represents result,a represents n1,b represents n2,

    c represents n3

    The name of an actual parameter may bedifferent from its formal parameter

    This flowchart calculates the average of three numbers

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    11/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 11

    Related terms and concepts (cont.)

    Page 1 Page 2

    In a function-definition, you should onlyuse formal parameters R, a, b, c

    You shouldnt use actual parameters

    This is wrong!n1, n2, n3 are actual parameters.Should use a, b, c instead.

    This is wrong!R is an formal parameters.

    Should use result instead.

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    12/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 12

    Page 1

    Related terms and concepts (cont.)

    Page 2

    A function may becalled more than once

    At this time:Rrepresents average1,a represents n1,b represents n2,c represents n3

    When comes to this:

    Rrepresents average2,a represents n4,b represents n5,c represents n6

    This flowchart calculates the average of three numbers

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    13/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 13

    Related terms and concepts (cont.)

    A function parameter may act as:

    InputData of the function

    Output

    The result of the function

    Both

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    14/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 14

    Page 1

    AVRG (result, 1, 2, 3)

    Start

    Stop

    Rea

    1, 2 , 3

    Pri t

    result

    Page 2

    Function call:

    result is the output

    parameter.n1, n2, n3 are the inputparameters.

    Function definition:

    R is the output parametera, b, c are input parameters

    Related terms and concepts (cont.)

    This flowchart calculates the average of three numbers

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    15/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 15

    Related terms and concepts (cont.)

    Page 1 Page 2

    Function call:

    p and qact as both input and outputparameters.

    Function definition:

    x and y act as both input and outputparameters

    This flowchart exchanges or swaps the valueof x and y each other

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    16/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 16

    Page 2

    Related terms and concepts (cont.)

    If there is only one output parameter, the flowchart may RETURN the result

    Example: let take a look again at the function that calculates the average of three numbers.

    Original function flowchart:

    Page 2

    Since it has only one output, the output is RETURN

    The output parameter (R)is removed from the formalparameter list

    and the result

    is return

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    17/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 17

    Related terms and concepts (cont.)

    Since the function flowchart has been modified, the way of the function to be called will also be changed

    Original main flowchart: Modified main flowchart:

    Page 1

    AVRG (result,1,

    2,

    3)

    Start

    Stop

    Rea

    1, 2 , 3

    Pri

    t

    result

    Page 1

    Now, result is not

    anymore a parameterof the function-call

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    18/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 18

    Control Structure

    Describe the flow of execution.

    In flowcharts, flow of execution is represented bythe arrow line.

    Types of control structure:Sequential

    SelectionRepetition

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    19/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 19

    Sequential Structure

    statement

    statement

    statement

    Multiple statements considered as one statement

    |

    A statement means

    a command or an instruction

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    20/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 20

    Selection Structure

    If

    (one-choice)

    condition

    statement

    TRUE

    FALSE |statement

    If set condition is true, execute the statement, else do

    nothing

    do it or dont

    condition

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    21/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 21

    Selection Structure (cont..)

    If-else(two-choices)

    condition

    Statement 2Statement 1

    |statement

    If set condition is true, execute the first statement, elseexecute second statement

    TRUE FALSE

    do this one orthe other one

    condition

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    22/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 22

    Considered asone statement

    Nested if(if within if)

    Selection Structure (cont..)

    test1

    test2

    statement

    FALSE

    FALSE

    TRUE

    TRUE

    |

    test1

    FALSE

    TRUE

    it is an one-choice if

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    23/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 23

    Complex if-else & if Statements

    x

    condition

    statement

    condition

    statement

    statement TRUE

    TRUE

    FALSE

    FALSE

    Considered as one statement

    Selection Structure (cont..)

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    24/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 24

    Repetition Structure

    while Loop

    It is a pre-test loop

    statement

    condition

    body of loop|

    While a set condition is true, repeat statement (body of loop)

    TRUE

    FALSE

    condition

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    25/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 25

    Repetition Structure (cont)

    do-while Loop

    It is a post-test loop

    | statement

    FALSE

    TRUE

    Do the statement (body of loop) while a condition is true

    statement

    condition

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    26/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 26

    for Loop

    It is a pre-test loop

    x

    initialization

    condition

    bodyofloop

    increment

    y

    FALSE

    TRUE

    Repetition Control Structure (cont)

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    27/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 27

    Example:

    Start

    Stop

    Rea

    Le gth,

    Wi th

    Pri t

    Area,

    Peri eter

    Calculate Area

    Area=Le gth * Wi th

    Calculate Peri eter

    Peri eter=

    2 * (Wi th+Le gth)

    Input:Length

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    28/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 28

    St rt

    St

    d N m

    rint

    " t r "

    Y s

    N m>0? N

    rint

    " t r "

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

    Num = 10

    10 > 0 ? => YES

    Input:Num > 10

    Output:

    Category A

    Category A

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    29/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 29

    St rt

    St

    d N m

    rint

    " t r "

    Y s

    N m>0? N

    rint

    " t r "

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

    Num = 0

    0 > 0 ? => NOOutput:Category B

    Input:Num >0

    Category B

    Output:

    Category A

    Category A

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    30/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 30

    Start

    Stop

    Pri t

    Result

    Result=Result + Cou t

    Cout=Cou

    t - 1

    Iitialize

    Result=

    Cout=

    u

    Cout

    !

    Rea"

    u

    o

    Pri

    t Cou

    t

    Yes

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

    Input:Num 4

    Variables (in memory):

    Num [ ]

    Result [ ]

    Count [ ]

    Variables (in memory):

    Num [4 ]

    Result [ ]

    Count [ ]

    Count = 4

    4 > 0 ? => YES

    Variables (in memory):

    Num [4 ]

    Result [ 0 ]

    Count [ 4 ]

    Count: 4

    Variables (in memory):

    Num [4 ]

    Result [ 4 ] 0 + 4

    Count [ 3 ] 4 - 1

    Count: 3

    Count = 3

    3 > 0 ? => YES

    Variables (in memory):

    Num [4 ]

    Result [ 7 ] 4 + 3

    Count [ 2 ] 3 - 1

    Count: 2

    Count = 2

    2 > 0 ? => YES

    Variables (in memory):

    Num [4 ]

    Result [ 9 ] 7 + 2

    Count [ 1 ] 2 - 1

    Count: 1

    Count = 1

    1 > 0 ? => YES

    Variables (in memory):

    Num [4 ]

    Result [ 10] 9 + 1

    Count [ 0 ] 1 - 1

    C

    oun

    t: 0

    Count = 0

    0 > 0 ? => NO

    Result: 10

  • 8/8/2019 MELJUN CORTES--T102_FLOWCHARTING_MELJUN_LECTURE

    31/31

    Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 31

    Page 1

    AVRG (average, 1 , 5, )

    Start

    Stop

    Rea

    Pri t

    average

    Page 2

    AVRG ( result, 1, 2, 3)

    Retur

    su = 1+ 2+ 3

    result = su /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