slide 02 - algorithm

Upload: effendyfooad

Post on 02-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Slide 02 - Algorithm

    1/33

    Computer Science Department FTSM

    Algorithm

    Knowledge:

    Understand algorithm representation using flowchart andpseudocode

    Skill:

    Map problem to solution in flowchart and pseudocode forms

  • 8/10/2019 Slide 02 - Algorithm

    2/33

    TK1913-C Programming 2

    Consider the following .

    Problem:Baking a Cake

    How to solve:

    1. Start

    2. Preheat the oven at 180oC

    3. Prepare a baking pan

    4. Beat butter with sugar

    5. Mix them with flour, eggs and essence vanilla

    6. Pour the dough into the baking pan

    7. Put the pan into the oven

    8. End

    Algorithm in Real Life

  • 8/10/2019 Slide 02 - Algorithm

    3/33

    TK1913-C Programming 3

    Divide and Conquer Strategy

    in Algorithm

    Problem:Prepare a Breakfast

    1. Start

    2. Prepare a Breakfast

    3. End

  • 8/10/2019 Slide 02 - Algorithm

    4/33

    TK1913-C Programming 4

    1. Start

    2. Prepare a Breakfast2.1 Prepare a tuna sandwich

    2.2 Prepare some chips

    2.3 Make a cup of coffee3. End

    Divide and Conquer Strategy

    in Algorithm

  • 8/10/2019 Slide 02 - Algorithm

    5/33

    TK1913-C Programming 5

    1. Start

    2. Prepare a Breakfast

    2.1 Prepare a tuna sandwich2.1.1 Take 2 slices of bread

    2.1.2 Prepare tuna paste

    2.2 Prepare some chips2.3 Make a cup of coffee

    3. End

    Divide and Conquer Strategy

    in Algorithm

  • 8/10/2019 Slide 02 - Algorithm

    6/33

    TK1913-C Programming 6

    1. Start

    2. Prepare a Breakfast

    2.1 Prepare a tuna sandwich

    2.1.1 Take 2 slices of bread

    2.1.2 Prepare tuna paste

    2.2 Prepare some chips

    2.2.1 Cut potatoes into slices2.2.2 Fry the potatoes

    2.3 Make a cup of coffee

    3. End

    Divide and Conquer Strategy

    in Algorithm

  • 8/10/2019 Slide 02 - Algorithm

    7/33

    TK1913-C Programming 7

    1. Start

    2. Prepare a Breakfast

    2.1. Prepare a tuna sandwich

    2.1.1 Take 2 slices of bread2.1.2 Prepare tuna paste

    2.2. Prepare some chips

    2.2.1 Cut potatoes into slices

    2.2.2 Fry the potatoes

    2.3. Make a cup of coffee

    2.3.1 Boil water

    2.3.2 Add water with sugar and coffee

    3. End

    Divide and Conquer Strategy

    in Algorithm

  • 8/10/2019 Slide 02 - Algorithm

    8/33

    TK1913-C Programming 8

    What is the connection

    between these real life

    processes and

    algorithm?

    Something to ponder

  • 8/10/2019 Slide 02 - Algorithm

    9/33

    TK1913-C Programming 9

    A specific and step-by-step set of instructions

    for carrying out a procedure or solving a

    problem, usually with the requirement that the

    procedure terminate at some point 2 types of algorithm representation will be

    explained:

    Flowchart

    Pseudocode

    Structured Method (will be explained later)

    A method of designing problemsolution

    Algorithm

  • 8/10/2019 Slide 02 - Algorithm

    10/33

    TK1913-C Programming 10

    Flowchart represents

    algorithm graphically.

    It is intended forcommunication and

    documentation

    Flowchart

  • 8/10/2019 Slide 02 - Algorithm

    11/33

    TK1913-C Programming 11

    FlowchartBasic Syntax

    Start/End

    Symbol Semantic

    Process

    Input/Output

    Test

    Connector

    Flow of activities

  • 8/10/2019 Slide 02 - Algorithm

    12/33

    TK1913-C Programming 12

    FlowchartOther Syntax

    Function call

    Symbol Semantic

    Magnetic Disc

    Stored Data

    Document/File

    Multiple Document/File

  • 8/10/2019 Slide 02 - Algorithm

    13/33

    TK1913-C Programming 13

    Are the steps in the

    algorithm discussedearlier specific

    enough to be

    executed by

    computer?

    Something to ponder

  • 8/10/2019 Slide 02 - Algorithm

    14/33

    TK1913-C Programming 14

    Problem Solving Process

    Input Process Output

  • 8/10/2019 Slide 02 - Algorithm

    15/33

    TK1913-C Programming 15

    Example 1Calculate and display the price of a

    number of apples if the quantity in kg and

    price per kg are given.

    QuantityPrice_per_kg

    PricePrice = Quantity * Price_per_kg

    Input Process Output

  • 8/10/2019 Slide 02 - Algorithm

    16/33

    TK1913-C Programming 16

    Flowchart: Calculate Price of

    ApplesInput

    Quantity

    Start

    Price Quantity * Price_per_kg

    InputPrice_per_kg

    Output

    Price

    End

  • 8/10/2019 Slide 02 - Algorithm

    17/33

    TK1913-C Programming 17

    void main() {

    scanf(%d,&quantity);

    scanf(%d,&price_per_kg);

    price = quantity*price_per_kg;

    printf(%d, price);

    }

    Input

    Quantity

    Start

    Price Quantity * Price_per_kg

    InputPrice_per_kg

    Output

    Price

    End

    C Program: Calculate Price of Apples

  • 8/10/2019 Slide 02 - Algorithm

    18/33

    TK1913-C Programming 18

    void main() {

    scanf(%d,&quantity);

    scanf(%d,&price_per_kg);

    price = quantity*price_per_kg;

    printf(%d, price);

    }

    C Program: Calculate Price of Apples

    Its not complete!

    Declare the variables

  • 8/10/2019 Slide 02 - Algorithm

    19/33

    TK1913-C Programming 19

    void main() {

    int quantity, price_per_kg, price;

    scanf(%d,&quantity);

    scanf(%d,&price_per_kg);

    price = quantity*price_per_kg;

    printf(%d, price);

    }

    Well done ! Butwhat

    are they?

    C Program: Calculate Price of Apples

  • 8/10/2019 Slide 02 - Algorithm

    20/33

    TK1913-C Programming 20

    void main() {

    int quantity, price_per_kg, price;

    scanf(%d,&quantity);scanf(%d,&price_per_kg);

    price = quantity*price_per_kg;

    printf(%d, price);

    }

    Start Declaration

    } InputProcess

    OutputEnd

    C Program: Calculate Price of Apples

  • 8/10/2019 Slide 02 - Algorithm

    21/33

    TK1913-C Programming 21

    Chapter 4

    } Chapter 5Chapter 6

    Chapter 5

    C Program: Calculate Price of Apples

    void main() {

    int quantity, price_per_kg, price;

    scanf(%d,&quantity);scanf(%d,&price_per_kg);

    price = quantity*price_per_kg;

    printf(%d, price);

    }

  • 8/10/2019 Slide 02 - Algorithm

    22/33

    TK1913-C Programming 22

    Example 2A car park has the following charges:

    The 1sthour costs RM2.00. The subsequent hours

    cost RM1.00 per hour. Write an algorithm based on

    a vehicles entry and exit time.

    Entry_time

    Exit_time

    Charge????

    Input Process Output

  • 8/10/2019 Slide 02 - Algorithm

    23/33

    TK1913-C Programming 23

    Flowchart: Calculate Car Park

    Charge Input Entry_timeInput Exit_time

    Start

    Output

    Charge

    End

    Period Exit_time

    Entry_time

    Period > 1?Yes

    Charge 2 + (Period * 1)Charge 2No

  • 8/10/2019 Slide 02 - Algorithm

    24/33

    TK1913-C Programming 24

    scanf(%d%d,&entry_time,&exit_time);

    period = exit_timeentry_time;

    Input Entry_time

    Input Exit_time

    Start

    Output

    Charge

    End

    Period Exit_time

    Entry_time

    Period > 1?Yes

    Charge 2 + (Period * 1)Charge 2No

    if (period > 1)charge = 2 + ( period *1);

    else

    charge = 2;

    printf(%d,charge);

    Flowchart: Calculate Car Park

    Charge

  • 8/10/2019 Slide 02 - Algorithm

    25/33

    TK1913-C Programming 25

    void main() {

    int entry_time, exit_time, period, charge;

    scanf(%d%d,&entry_time,&exit_time);

    period = exit_timeentry_time;

    if (period > 1)charge = 2 + (period * 1);

    elsecharge = 2;

    printf(%d,charge);

    }

    }Chapter 7

    C Program: Calculate Car Park

    Charge

  • 8/10/2019 Slide 02 - Algorithm

    26/33

    TK1913-C Programming 26

    Write a program to calculate the average

    mark of three TK1913s students.

    Example 3

    Mark A

    Mark B

    Mark C

    Average_mark

    ????

    Input Process Output

    THINK!!

  • 8/10/2019 Slide 02 - Algorithm

    27/33

    TK1913-C Programming 27

    Identify the input and output of the problem.

    If necessary, use Divide & Conquer strategy

    to decompose the problem into smaller andmanageable sub problems. Decompose the

    problem until all the sub problems can be

    solved to get the required results

    For each sub problem, identify and list out the

    steps involved in solving it

    Algorithm Development

    Guidelines

  • 8/10/2019 Slide 02 - Algorithm

    28/33

    TK1913-C Programming 28

    What might be the

    disadvantage of

    flowchart?

    Something to ponder

  • 8/10/2019 Slide 02 - Algorithm

    29/33

    TK1913-C Programming 29

    An outline of a program, written in a form thatcan easily be converted into real programmingstatements. It resembles the actual program thatwill be implemented later. However, it cannot

    be compiled nor executed.

    Pseudocode normally codes the followingactions:

    Initialisation of variables Assignment of values to the variables Arithmetic operations Relational operations

    Pseudocode

  • 8/10/2019 Slide 02 - Algorithm

    30/33

    TK1913-C Programming 30

    1. Start

    2. Read quantity

    3. Read price_per_kg

    4.pricequantity* price_per_kg

    5. Print price

    6. End

    Example of Pseudocode

  • 8/10/2019 Slide 02 - Algorithm

    31/33

    TK1913-C Programming 31

    Guidelines on Writing

    Pseudocode

    Refer to the handoutsin your manual . OK??

  • 8/10/2019 Slide 02 - Algorithm

    32/33

    TK1913-C Programming 32

    CFlow Demonstration

  • 8/10/2019 Slide 02 - Algorithm

    33/33

    TK1913-C Programming 33

    End of Lecture 2

    Yes !! Thats all?Whats next???

    INTRODUCTION TO Con the way