Flowchart in C Language

Embed Size (px)

Citation preview

  • 7/30/2019 Flowchart in C Language

    1/27

    FLOW CHARTING

    Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    2/27

    What is a Flowchart?

    A flowchart is a diagram that depictsthe flow of a program.

    The figure shown here is a flowchartfor the addition of two numbers.

    START

    Display messagePlease entertwo numbers

    Read two

    Numbers

    Calculate theSum

    Display Sum

    END

    2Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    3/27

    Basic Flowchart Symbols

    Notice there are three types of symbols in this flowchart:

    rounded rectangles parallelograms

    a rectangle

    Each symbol represents a different type of operation.

    SymbolSymbol NameName MeaningMeaningovals or roundedrectangles (terminals)

    Start or end of a process

    Arrows Flow of control

    parallelograms Input/output

    Rectangle Processing (indicates a process such as amathematical computation or variableassignment)

    3Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    4/27

    Flow of control in flow chart

    START

    Display messagePlease enter two

    numbers

    Read twoNumbers

    Please enter two

    numbers

    Output operation

    Calculate the

    Sum

    Display Sum

    END

    4Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    5/27

    Flow of control in flow chart

    START

    Display messagePlease enter twonumbers

    Read twoNumbers

    Please enter two

    numbers

    12 13

    Read operation

    Calculate the

    Sum

    Display Sum

    END

    Accept the value and store them into variables

    Fno

    Sno

    5Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    6/27

    Flow of control in flow chart

    START

    Display messagePlease enter twonumbers

    Read twoNumbers

    Please enter two

    numbers

    12 13

    Calculate the

    Sum

    Display Sum

    END

    Processing /calculation

    Processing/calculate

    Sum=Fno+Sno

    6Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    7/27

    Flow of control in flow chart

    START

    Display messagePlease enter twonumbers

    Read twoNumbers

    Please enter two

    numbers

    12 13

    25

    Calculate the

    Sum

    Display Sum

    END

    Output

    7Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    8/27

    Four Flowchart Structures

    SequenceDecision

    RepetitionCase

    8Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    9/27

    Sequence Structure

    A series of actions are performed in sequence

    The addition of two numbers was a sequential activity

    9Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    10/27

    Decision Structure

    One of two possible actions is taken, depending on a condition.

    A new symbol, the diamond, indicates a yes/no question.

    If the answer to the question is yes, the flow follows one path.

    If the answer is no, the flow follows another path

    YESNO

    10Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    11/27

    PROBLEM

    Accept two numbers and find the square of

    Nihar Ranjan Roy 11

  • 7/30/2019 Flowchart in C Language

    12/27

    Decision Structure

    In the flowchart segment below, the question is x < y? is asked. If theanswer is no, then process A is performed. If the answer is yes, then

    process B is performed.

    YESNO

    Square=y*y

    x < y?

    Square=x*x

    12Nihar Ranjan Roy

    If (x

  • 7/30/2019 Flowchart in C Language

    13/27

    Repetition Structure

    A repetition structure represents part of the

    program that repeats. This type of structure iscommonly known as a loop.

    13Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    14/27

    Repetition Structure

    Notice the use of the diamond symbol.

    A loop tests a condition, and if the condition exists, it performs anaction. Then it tests the condition again. If the condition still exists,the action is repeated.

    This continues until the condition no longer exists.

    Condition?Condition?

    14Nihar Ranjan Roy

    YES

    No

  • 7/30/2019 Flowchart in C Language

    15/27

    Repetition Structure

    In the flowchart segment, the question is x < y? is asked. If theanswer is yes, then Process A is performed.

    The question is x < y? is asked again. Process A is repeated as long asx is less than y.

    When x is no longer less than y, the repetition stops and the structureis exited.

    x < y?Do some

    Processing

    YES

    15Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    16/27

    PROBLEM

    Print your name 5 times

    Nihar Ranjan Roy 16

  • 7/30/2019 Flowchart in C Language

    17/27

    Repetition Structure

    The flowchart segment below shows a repetition structureexpressed in C as a while loop.

    Flowchart

    i=0

    i=0;

    while (i < 5)

    {

    puts(nihar Ranjan roy);

    i=i+1;

    }

    i< 5?

    Printname

    i=i+1

    YES

    17Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    18/27

    Caution: Controlling a Repetition Structure

    The action performed by a repetition structure must eventually causethe loop to terminate. Otherwise, an infinite loop is created.

    In this flowchart if i is never changed. Once the loop starts, it willnever end.

    QUESTION: How can this flowchart be modified so it is no longeran infinite loop?

    18Nihar Ranjan Roy

    i< 5?

    Printname

    YES

    i=0;

    while (i < 5)

    {

    puts(nihar Ranjan roy);

    }

    C

    Code

  • 7/30/2019 Flowchart in C Language

    19/27

    Controlling a Repetition Structure

    ANSWER: By adding an action within the repetition that changes thevalue of i.

    YES

    Print namei < 5? i=i+1

    19Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    20/27

    Case Structure

    One of several possible actions is taken, depending on the contents ofa variable.

    20Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    21/27

    Case Structure

    The structure below indicates actions to perform depending on the valuein years_employed.

    CASEyears_employed

    1 2 3 Other

    bonus = 100 bonus = 200 bonus = 400 bonus = 800

    21Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    22/27

    Case Structure

    If years_employed = 2,

    bonus is set to 200If years_employed = 3,

    bonus is set to 400

    CASEyears_employed

    1 2 3 Other

    bonus = 100 bonus = 200 bonus = 400 bonus = 800

    If years_employed = 1,bonus is set to 100

    If years_employed isany other value, bonus

    is set to 800

    22Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    23/27

    Connectors

    Sometimes a flowchart will not fit on one page.

    A connector (represented by a small circle) allows you to connect two

    flowchart segments.

    A

    23Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    24/27

    Connectors

    ASTART

    The A connector

    indicates that the second

    flowchart segment begins

    A

    END

    where the first segmentends.

    24Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    25/27

    Modules

    A program module (such as a function in C) is represented by a specialsymbol.

    25Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    26/27

    Modules

    The position of the module

    symbol indicates the point the

    module is executed.

    START

    Read Input.

    A separate flowchart can beconstructed for the module.

    END

    Call calc_pay

    function.

    Display results.

    26Nihar Ranjan Roy

  • 7/30/2019 Flowchart in C Language

    27/27

    Problem

    Design an algorithm and flow chart for

    converting an integer number to its binaryequivalent.

    Nihar Ranjan Roy 27