pro lect 2

Upload: hanani-idris

Post on 02-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 pro lect 2

    1/25

    Lecture 2PROBLEM SOLVING

    DITP 1113 PROGRAMMING ISEMESTER 1 2014/2015

  • 8/11/2019 pro lect 2

    2/25

    System Development

    Definition: The critical process determines theoverall quality and success of the program.

    If any program is design carefully using good structureddevelopment techniques, the program will be efficient,error-free and easy to maintain.

    Most programming projects are built using SystemDevelopment Life Cycle (SDLC).

    One of the popular development life cycle is known as thewaterfall model.

  • 8/11/2019 pro lect 2

    3/25

    Waterfall model

    Identifyprogramcomponents.

    Looks atdifferentalternatives

    from asystemviewpoint.

    Review program specification(input, output, process).

    Analyst meet with the users. Read and understand the question.

    Algorithm a list of steps to solve theproblem.

    Top-down design divide & conquer. Algorithm refinement improvised the

    algorithm Flowchart graphically shows the

    logic in a solution algorithm. Pseudo code describe a program

    using English-like technique.

    Write programs(coding).

    Compile andexecute.

    Updateprogram/systemfrom time totime.

    Fix errors &enhance thesystem.

    Verify tasks as desired.

    Run the programs repeatedly usingdifferent data sets.

    Errors type: syntax, run-time or logic

  • 8/11/2019 pro lect 2

    4/25

    Development of program

    A multi-step process that requires you understand theproblem, develop a solution, write the program, and test it.

    To determine how to take the inputs given and thenconvert them into the specified output (program design).

    4 steps involve in solving problems:1. Understand the problem2. Develop the solution

    3. Write the program

    4. Test the program

    Example of the problem:

    A user can read a map in kilometers but the map showsdistances in miles.

  • 8/11/2019 pro lect 2

    5/25

    Development of program (coding)

    Problem: Convert distance in miles (m) to kilometers (km).

    4 steps involve in solving problems:

    1. Understand the given problem

    Carefully read the requirements statement.

    Ask few clarifying questions even for the simplest problemstatements.

    Determines I.P.O. (input, process, output) Input the distances in miles (m)

    Process conversion formula

    1 mile = 1.609 kilometers

    kms = miles x 1.609

    Output the distance in kilometers, (km)

    1. Understand the problem2. Develop the solution3. Write the program4. Test the program

  • 8/11/2019 pro lect 2

    6/25

    Development of program (coding)

    Problem: Convert distance in miles (m) to kilometers (km).

    4 steps involve in solving problems:

    2. Develop the solution Tools to solve problems

    Structure chart to design the whole program

    Pseudo-code to design the individual parts of a program(eg. module / function)

    Flowchart to design the individual parts of a program(eg. module / function)

    1. Understand the problem2. Develop the solution3. Write the program4. Test the program

  • 8/11/2019 pro lect 2

    7/25

    Development of program

    Problem: Convert distance in miles (m) to kilometers (km).

    4 steps involve in solving problems: 2. Develop the solution

    Structure chart

    To design the whole program

    - The functional flow through

    your program.- Shows the breakdown of a

    program to its lowestmanageable levels.

    - Shows your program dividedinto logical steps each step asseparate module

    - The interaction between

    modules of your program.

    DistanceConversion

    Get milesCalculateConversion

    Printkilometers

    1. Understand the problem2. Develop the solution3. Write the program4. Test the program

  • 8/11/2019 pro lect 2

    8/25

    Development of program

    Problem: Convert distance in miles (m) to kilometers (km).

    4 steps involve in solving problems: 2. Develop the solution

    Pseudo code

    To design individual parts of theprogram

    (eg. module/function)

    - Precise algorithmic description

    of program logic- Precisely describe the

    algorithm detail, what theprogram being design.

    - Requires to define, in sufficientdetail, the steps to accomplishthe task so that can beconverted into a computer

    program.

    Algorithm Calculate Distance Conversion

    received units in miles, msconvert to kilometers using kms = ms * 1.609

    return kms in kilometers unit

    End Algorithm

    1. Understand the problem2. Develop the solution3. Write the program4. Test the program

  • 8/11/2019 pro lect 2

    9/25

    Development of program

    Problem: Convert distance in miles (m) to kilometers (km).

    4 steps involve in solving problems: 2. Develop the solution

    Flowchart

    To design individual parts of theprogram

    (eg. module/function)

    - A graphical or symbolicrepresentation of a process.

    - Each step in the process isrepresented by a different

    symbol and contains a shortdescription of the processstep.

    - Use standardised symbolsand linked together witharrows showing the processflow direction.

    1. Understand the problem2. Develop the solution3. Write the program4. Test the program

    Start

    kms ms * 1.609

    Receive unitsin miles (ms)

    End

    Return result

    (kms)

  • 8/11/2019 pro lect 2

    10/25

    Development of program

    Problem: Convert distance in miles (m) to kilometers (km).

    4 steps involve in solving problems: 3. Write a program

    Remember:

    Syntax

    ; {} () >> > ms; }

    void printKilometers()

    { cout

  • 8/11/2019 pro lect 2

    11/25

    Development of program

    Problem: Convert distance in miles (m) to kilometers (km).

    4 steps involve in solving problems: 4. Test a program two types of testing:-

    Blackbox Testing

    Whitebox Testing

    Differences?

    Blackbox Whitebox

    Test by the system engineer andthe user.

    Test by the programmer.

    Testing without knowing how doesthe program works.

    Testing with the tester that knowseverything about the program.

    Test by looking only at the

    requirements statement.

    Test to ensure that every single

    instruction and possible situationhave been tested.

    1. Understand the problem2. Develop the solution3. Write the program4. Test the program

  • 8/11/2019 pro lect 2

    12/25

    Examples of problem solving

    Problem Statement #1:

    Compute and display the total cost of apples byreceiving the number of kilograms of applespurchased and the cost per kilogram of apples.

    Step 1: Understand the problem

    Input number of apples purchased (numKiloApples), cost per kilogram

    of apples (costPerkg)

    Process formula total cost = cost per kilogram of apples x number of

    apples kilograms (totalCost= numKiloApples x costPerkg)

    Output Total cost of purchased apples (totalCost)

  • 8/11/2019 pro lect 2

    13/25

    Examples of problem solving

    Problem Statement #1:

    Step 2: Develop the solution

    (pseudo code)

    Algorithm cost of apples purchased

    Get number of kilogram ofapples purchased

    Get cost per kilogram of apples

    Compute the total cost of apples

    The total cost of apples is number of kilogram ofapples purchased multiply/times cost per kilogram

    Display the total cost

    End algorithm

    Step 1: Understand the problem

    Input number of applespurchased (numKiloApples), costper kilogram of apples(costPerkg)

    Process formula total cost = costper kilogram of apples x numberof apples kilograms (totalCost=numKiloApples x costPerkg)

    Output total cost of purchasedapples (totalCost)

  • 8/11/2019 pro lect 2

    14/25

    Examples of problem solving

    Problem Statement #1:

    Step 2: Develop the solution (flowchart)

    Step 3: Write the program(C++ codes)

    Start

    Read kilograms of apples(numKiloApples)

    Compute the total cost of apple purchased,

    totalCost= numKiloApples x costPerkg

    PrintTotal cost of apples

    in RM (totalCost)

    End

    Get cost per kilogram ofapples (costPerkg)

    void main()

    {

    int numKiloApples;int costPerkg;

    int totalCost;

    cout > costPerkg;

    totalCost = numKiloApples * costPerkg;

    cout >> The total cost of apples is >> totalCost;}

  • 8/11/2019 pro lect 2

    15/25

    Examples of problem solving

    Problem Statement #2:

    During the promotion of Ummies Boutique, 30%discount is given to every item purchased. Write aprogram to compute and display the price afterdiscount for every item purchased by the customers.

    Step 1: Understand the problem

    Input Price of the item,priceBefore

    Process

    formula discount = priceBefore x 0.3

    formula priceAfter = priceBefore - discount

    Output Price after discount,priceAfter

  • 8/11/2019 pro lect 2

    16/25

    Examples of problem solving

    Problem Statement #2:

    Step 2: Develop the solution

    (pseudo code)

    Algorithm price after discounted

    get the price of the item (priceBefore)compute the price of the item after discount

    compute discount = priceBefore x 0.3

    compute priceAfter = priceBefore - discount

    display the the price after discount, priceAfter

    End algorithm

    Step 1: Understand the problem

    Input Price of the item,priceBefore

    Processdiscount = priceBefore x 0.3

    priceAfter = priceBefore discount

    Output Price after discount,priceAfter

  • 8/11/2019 pro lect 2

    17/25

    Examples of problem solving

    Problem Statement #2:

    Step 2: Develop the solution (flowchart)

    Step 3: Write the program(C++ codes)

    Start

    Get Items price (priceBefore)

    Compute the price after discount

    priceAfter = priceBefore discPrice

    Display the price afterdiscount, priceAfter

    End

    Compute the discountdiscPrice = priceBefore x 0.3 void main()

    {

    float priceBefore;

    float discPrice;

    float priceAfter;

    cout

  • 8/11/2019 pro lect 2

    18/25

    Flowchart with different structure

    If/else double-selection structure:

    Pseudo code:If students grade is greater than or equal to 60

    Print Passed

    ElsePrint Failed

  • 8/11/2019 pro lect 2

    19/25

    Flowchart with different structure

    If/else double-selection structure:

    Pseudo code:While product is less than or equal to 1000

    Multiply product by two

  • 8/11/2019 pro lect 2

    20/25

    Examples of problem solving

    Problem Statement #3:

    SHN Enterprise is given 10% discount for every itemwith price of above RM100 only. Write a program thatcompute and display the price after discount forevery item purchased by the customers.

    Step 1: Understand the problem

    Input Price of the item,priceBefore

    Process

    formula discount = priceBefore x 0.1

    formula priceAfter = priceBefore discount

    Output Price after discount,priceAfter

  • 8/11/2019 pro lect 2

    21/25

    Examples of problem solving

    Problem Statement #3:

    Step 2: Develop the solution

    (pseudo code)

    Algorithm price after discounted

    get the price of the item, priceBeforecompute the item discount

    if priceBefore > 100

    compute discount = priceBefore x 0.1

    else

    discount = 0

    compute priceAfter = priceBefore discountdisplay the price after discount, priceAfter

    End algorithm

    Step 1: Understand the problem

    Inputs Price of the item,priceBefore

    Processdiscount = priceBefore x 0.1

    priceAfter = priceBefore discount

    Output Price after discount,priceAfter

  • 8/11/2019 pro lect 2

    22/25

    Examples of problem solving

    Problem Statement #3:

    Step 2: Develop the solution (flowchart)

    Start

    Discount = priceBefore x 0.1

    Get Items price, priceBefore

    Display price afterdiscount, priceAfter

    End

    Compute the price after discount

    priceAfter = priceBefore - discount

    priceBefore > 100?

    discount = 0

    YesNo

  • 8/11/2019 pro lect 2

    23/25

    Examples of problem solving

    Problem Statement #3:

    Step 3: Write the program(C++ codes)

    void main()

    {

    float priceBefore;

    float discPrice;

    float priceAfter;

    cout

  • 8/11/2019 pro lect 2

    24/25

    Indent program properly

    Use a good indentation style to enhance the readability of aprogram.

    The layout of a program should make it easy for a reader toidentify the program's modules.

    Use blank lines to offset each function.

    Also, within both functions and the main program, you shouldoffset with blank lines and indent individual blocks of code visibly.

    These blocks are generally - but are not limited to - the actionsperformed within a control structure, such as a while loop or an if

    statement.

  • 8/11/2019 pro lect 2

    25/25

    Any questions..?

    End of Lecture 2..