Problem Solving & Flow Charts

download Problem Solving & Flow Charts

of 26

Transcript of Problem Solving & Flow Charts

  • 8/2/2019 Problem Solving & Flow Charts

    1/26

    Working With Algorithm andFlowcharts

    Understanding theirpracticality

  • 8/2/2019 Problem Solving & Flow Charts

    2/26

    What is an Algorithm?

    It is a finite set of rules giving a sequence of

    operations for solving a specific type ofproblem.

    In most common computer applications an

    algorithmic solution to a problem must beconstructed.

  • 8/2/2019 Problem Solving & Flow Charts

    3/26

    Design of an Algorithm

    The design of an algorithm tends to be difficult

    when the task is non-trivial. Computers tend to lack the intuition or

    common sense to realise the full procedure.

    The designer must describe the procedure very

    precisely for it to function properly

    Top-down design approach is often used.

  • 8/2/2019 Problem Solving & Flow Charts

    4/26

    How does the top-down method

    work?

    First try to break the process into a number of

    steps, which are smaller and simpler than thatfor the entire process.

    The sub-algorithms can themselves be brokendown into smaller portions

    We use flow charts to show how the algorithmwill work.

  • 8/2/2019 Problem Solving & Flow Charts

    5/26

    Lets take a look at an algorithm

    Image you had to program a robotto make coffee.

  • 8/2/2019 Problem Solving & Flow Charts

    6/26

    A Simple Algorithm : Making Coffee

    Algorithm

    1 Boil water

    2 Put coffee

  • 8/2/2019 Problem Solving & Flow Charts

    7/26

    A Simple Algorithm : Making Coffee

    Original First Refinement

    1 Boil water

    2 Put coffee

    1.1 fill kettle1.2 switch kettle on

    1.3 wait until boiling

    1.4 switch off kettle

    2.1 open coffee jar

    2.2 Fill spoon withcoffee

    2.3 tip spoonful intocup

    2.4 close coffee jar

  • 8/2/2019 Problem Solving & Flow Charts

    8/26

    A Simple Algorithm : Making Coffee

    Original First Refinement Second Refinement

    1 Boil water

    2 Put coffee

    1.1 fill kettle1.2 switch kettle on

    1.3 wait until boiling

    1.4 switch off kettle

    2.1 open coffee jar

    2.2 Fill spoon withcoffee

    2.3 tip spoonful intocup

    2.4 close coffee jar

    1.1.1 put kettle undertap

    1.1.2 turn on tap

    1.1.3 wait till kettle is full

    1.1.4 turn off tap

    2.1.1 take coffee jarfrom shelf

    2.1.2 remove lid

    2.4.1 put lid on coffeejar

    2.4.2 replace coffee jaron shelf

  • 8/2/2019 Problem Solving & Flow Charts

    9/26

    Selection (Decision Statement)

    An algorithm which is solely a sequence of

    steps is extremely inflexible A What if ? Then Statement allows for this

    flexibility.

    What if the coffee jar is empty?

    Then get a new jar.

  • 8/2/2019 Problem Solving & Flow Charts

    10/26

    Iteration (Repetition or Loop)

    Sometimes it is necessary for the computer not

    just to make a decision, but a continuousaction.

    This processes is called iteration.

    Example : With the robot making the coffee

    Continue filling coffee jar untiljar isfound.

  • 8/2/2019 Problem Solving & Flow Charts

    11/26

    What Is a Flowchart?

    A diagrammatic representation of a program

    Building a flowchart is like putting a puzzletogether; there are standard pieces

    The standard action shapes include terminal,process, in/output, decision and junction.

  • 8/2/2019 Problem Solving & Flow Charts

    12/26

    What Are the Basic Symbols?

    A program should

    always begin with a startsymbol and finishsymbol

    For Pascal it equals thebegin and the end

    Start

    End

  • 8/2/2019 Problem Solving & Flow Charts

    13/26

    What Are the Basic Symbols?

    Whenever a decisionmust be taken a

    diamond symbol is used.

    Can you see thecorrelation to the

    ifthenelsestatements.

    no

    Is mark

    > 50?

    yes

    Decision/ Comparison

    Operation

  • 8/2/2019 Problem Solving & Flow Charts

    14/26

    What Are the Basic Symbols?

    Used to connect differentparts of the programtogether.

    Connector

  • 8/2/2019 Problem Solving & Flow Charts

    15/26

    What Are the Basic Symbols?

    A rectangle represents aprocess.

    When a variable mustbe assigned to a value, aprocess must be used

    When clearing the

    screen, changing coloura rectangle must beused

    mark := 0

    Process to be

    performed

  • 8/2/2019 Problem Solving & Flow Charts

    16/26

    What Are the Basic Symbols?

    Input and Output arerepresented using a

    parallelogram.

    In Pascal we are dealingwith writeln, write, or

    readln.Show resultto user

    Read markfrom user

    Input /Output

    Operation

  • 8/2/2019 Problem Solving & Flow Charts

    17/26

    Lets Put the Pieces Together!

    Example

    You are going to school, how will

    you get there?

  • 8/2/2019 Problem Solving & Flow Charts

    18/26

    Leave the House

  • 8/2/2019 Problem Solving & Flow Charts

    19/26

    Leave the House

    Check the Weather

  • 8/2/2019 Problem Solving & Flow Charts

    20/26

    Leave the House

    Check the Weather

    Weather

    Clear?

  • 8/2/2019 Problem Solving & Flow Charts

    21/26

    Leave the House

    Check the Weather

    Weather

    Clear?

    Yes

    Walk

  • 8/2/2019 Problem Solving & Flow Charts

    22/26

    Leave the House

    Check the Weather

    Weather

    Clear?

    Yes

    Walk

    Arrive at School

  • 8/2/2019 Problem Solving & Flow Charts

    23/26

    Leave the House

    Check the Weather

    Weather

    Clear?

    Yes

    Walk

    Arrive at School

    No, Rain.

  • 8/2/2019 Problem Solving & Flow Charts

    24/26

    Leave the House

    Check the Weather

    Weather

    Clear?

    Yes

    Walk

    Arrive at School

    No, Rain.Put on raincoat

  • 8/2/2019 Problem Solving & Flow Charts

    25/26

    Leave the House

    Check the Weather

    Weather

    Clear?

    Yes

    Walk

    Arrive at School

    No, Rain.Put on raincoat

    Ride Bus

  • 8/2/2019 Problem Solving & Flow Charts

    26/26

    Leave the House

    Check the Weather

    Weather

    Clear?

    Yes

    Walk

    Arrive at School

    No, Rain.Put on raincoat

    Ride Bus