chapter 2-problem solving methods

Upload: badrul-afif-imran

Post on 03-Jun-2018

253 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Chapter 2-Problem Solving Methods

    1/63

    CHAPTER 2PROBLEM SOLVING METHODS

    FP 101PROGRAMMING PRINCIPLES

    1

  • 8/11/2019 Chapter 2-Problem Solving Methods

    2/63

  • 8/11/2019 Chapter 2-Problem Solving Methods

    3/63

    Specific outcome:2. 1 Understand problem solving concept.2.2 Demonstrate the understanding of

    Programming Life Cycle.2.3 Apply the different types of algorithm to

    solve problem.2.4 Use problem solving tools to solve

    problem

    3

  • 8/11/2019 Chapter 2-Problem Solving Methods

    4/63

    2.1 Understand ProblemSolving Concept

    4

  • 8/11/2019 Chapter 2-Problem Solving Methods

    5/63

    Problem Solving Concept

    Identifying the problem --Which problem should I address? Ifthere are several, how do I choose the most important one?Describing the problem --How do I accurately and completelydescribe the problem?

    Analyzing the problem --What are the different causes of theproblem, and which causes are most important to solve rightaway?Planning the solutions --What are the different alternativesolutions for solving the problem?

    Imp lement ing the so lu t ions --How do I make sure the solutionsare implemented correctly and effectively?Monitoring/evaluating the solutions --How did the solutionswork? What needs to be changed?

    5

  • 8/11/2019 Chapter 2-Problem Solving Methods

    6/63

    Developing the Input Process Output(IPO) Chart

    Extends and organizes the information in the Problem

    Analysis Chart.It shows in more detail what data items are input, whatare the processing or modules on that data, and whatwill be the result or output .

    Input Processing OutputAll input

    data fromPAC

    All processing stepsfrom IPO / IC

    All outputrequirements

    from PAC

    Define Input, ProcessAnd Output

  • 8/11/2019 Chapter 2-Problem Solving Methods

    7/63

    Developing the Input Process Output(IPO) Chart

    ProblemWrite a Input Process Output (IPO) to find an area of acircle where area = pi * radius * radius

    Input Processing Output

    - radius - Area = 3.14 x radius x radius-Display area

    - Area of a circle

    Identify Input, ProcessAnd Output

  • 8/11/2019 Chapter 2-Problem Solving Methods

    8/63

    A program is required to read three (3) numbers,add them and print their total. It is usually helpfulto write down the three (3) components in adefining diagram as shown below:

    Example

  • 8/11/2019 Chapter 2-Problem Solving Methods

    9/63

    Steps Of Program ExecutionInput Refers to the process of entering data, program andinstructions into the computer system using input devices.Process Computer processes raw data into usable information tobe used by user. Data processing is done by the CPU (CentralProcessing Unit).Output Output is raw data that has been processed by thecomputer (result). Output will be converted to an understandableform before being displayed or printed.

    9

  • 8/11/2019 Chapter 2-Problem Solving Methods

    10/63

    Example: Flow Of ATMProgram

    Assume that our transaction is moneywithdrawal. The instructions are:a. Get the card number from the user

    b. Get pin number from the userc. Process the input datad. Get the transaction chosen by the usere. Get the account type from the user

    f. Process the transaction as wanted by the userg. Withdraw amount of money required by the userh. Print receipt for the user

    10

    (Input)

    (Input)

    (Input)

    (Input)

    (Process)

    (Process)(Output)

    (Output)

  • 8/11/2019 Chapter 2-Problem Solving Methods

    11/63

    Input : Example: Card number, ATM pin number, type oftransaction, type of account, amount of money towithdraw.

    Process: Example: Process to identify card number, valid pin

    number, type of transaction, type of account and deductsthe withdrawal from the users account. Output: Example: Receipt will show balance in users account and

    money withdrawn.

    INPUT PROCESS OUTPUT

    11

  • 8/11/2019 Chapter 2-Problem Solving Methods

    12/63

    Executing A ProgramTo execute a program, CPU will examine each programinstruction in memory and send out the required commandsignals to carry out the instruction.During execution, data can be entered into memory and

    manipulated in some specific way (delete and modify) asrequired.Program instructions are used to copy a program's data(program input) into memory.

    After the input data were processed, instructions fordisplaying or printing will be executed.Result displayed by a program is called program output. Example: Cash withdrawal from ATM machine.

    12

  • 8/11/2019 Chapter 2-Problem Solving Methods

    13/63

    First step: Data entered by user are stored in memory (input).

    Second step: CPU will instruct program to process the card number and ATM

    pin number with the data in memory concurrently. Program willexecute the transaction chosen by the user and store the result inmemory.

    Third step:

    The outputs are money withdrawal and receipt (output).

    Machine languageprogram for processingcard number and PIN

    number.

    Data entered duringexecution.

    Computed results.

    Central Processing Unit.

    Output results:

    Receipt and money

    Input data:Card Number,PIN Number,transaction. Program Output

    Step 1

    Step 2

    Step 3

    13

  • 8/11/2019 Chapter 2-Problem Solving Methods

    14/63

    Discuss

    14

    1. Show step of mailing a copy of your SPMtranscript using photocopy machine.

    2. Show step of making call from yourtelephone.

    3. Show step of saving your document in yourcomputer.

    4. Show steps of buying a tin of soft drink for a

    vending machine.5. Show steps of sending an email to your

    friend.

  • 8/11/2019 Chapter 2-Problem Solving Methods

    15/63

    ACTIVITY 1: GRAFFITI

    15

    1. Everyone get a piece of graphic paper

    2. Recall what you have learn in chapter 1 and chapter2 (2.1)

    3. Write down 4 things that you have learned in class(only 3 parts)- 4 minutes

    4. Move to your friends in class and share the answer(1 friend=1 answer)

    5. Move again to other friends and share the answeruntil you fullfill 4 PARTS of PAPER

    6. I will call 4 people to share the answer

  • 8/11/2019 Chapter 2-Problem Solving Methods

    16/63

    2.2 Demonstrate theunderstanding ofProgramming Life Cycle

    16

  • 8/11/2019 Chapter 2-Problem Solving Methods

    17/63

    Programming Life Cycle

    Life Cycle : Refers to the changes made froman old to a new program (the cycle startsagain).Programming Life Cycle : A framework ordiscipline, which uses certain techniquesneeded in computer programming

    development.

    17

  • 8/11/2019 Chapter 2-Problem Solving Methods

    18/63

    Steps involved in ProgrammingLife Cycle:

    1. Specify theproblem

    2. Analyze theproblem

    3. Programdesign(thealgorithm)

    4. Programcoding(Implem

    ent thealgorithm)

    5. Test andverify

    6. Maintain andupdate

    7.Documentation

    18

  • 8/11/2019 Chapter 2-Problem Solving Methods

    19/63

    Step 1: Specify The Problem Describe exactly what you are doing as clear as

    possible the data to be used, and what can be assumed.

    Also, the desired output and its layout. Specification of needs

    what the problem is

    what is needed to solve itwhat the solution should provideif there are constraint s and special conditions.

    19

  • 8/11/2019 Chapter 2-Problem Solving Methods

    20/63

    Example

    20

    Situation:Joe Jambul was borrowing a book form PBU library.He was late returning a book late. Calculate thetotal fine charged to Joe Jambul by library for late-return books. The charge is RM0.20 for 1 day.

    Calculate the total fine charged to Joe Jambul bylibrary for late-return books.Data given (RM 0.20/day)

    Specify the problem:

  • 8/11/2019 Chapter 2-Problem Solving Methods

    21/63

  • 8/11/2019 Chapter 2-Problem Solving Methods

    22/63

    Step 3: Program Design

    Definition:It is a framework or flow that shows the steps in

    problem solving.

    22

  • 8/11/2019 Chapter 2-Problem Solving Methods

    23/63

    Methods to design a program:

    1. Algorithm Algorithm is a sequence of instructions to solve aproblem , written in human language , and it can solve

    any problems when it is used with the correctprocedure. ( Example )2. Flowchart

    A graphical representation of data, information and

    workflow using certain symbols that are connected toflow lines to describe the instructions done in problemsolving.It shows the flow of the process from the start to the

    end of the problem solving. ( Example )

    Step 3: Program Design cont.

    23

  • 8/11/2019 Chapter 2-Problem Solving Methods

    24/63

    25

  • 8/11/2019 Chapter 2-Problem Solving Methods

    25/63

    ACTIVITY 2:DISCUSSION(20 minutes)

    26

    1. Create 8 group (7 people/group)-

    2 minutes2. Get a topic, marker pen, paper3. Discuss in group4. Write the algorithm in paper given5. Choose BEST GROUP as

    WINNER(faster, correct , creative and

    tidiness)

  • 8/11/2019 Chapter 2-Problem Solving Methods

    26/63

    PROBLEM

    27

    You want to calculate the totaland average of four number thatentered by yourself. Then,

    display the total and average.

  • 8/11/2019 Chapter 2-Problem Solving Methods

    27/63

    Methods to design a program:

    Basic flowchart symbol

    Step 3: Program Design

    29

  • 8/11/2019 Chapter 2-Problem Solving Methods

    28/63

    S y m b o l Explanat ion

    Indicate the direction of data flow.Used to connect a block to another

    block.Indicates operations / processinvolved.

    Receive / read valueDisplay value

    Flow Lines

    Process

    Input / Output

    Methods to design a program:

    Basic flowchart symbol

    Step 3: Program Design

    30

  • 8/11/2019 Chapter 2-Problem Solving Methods

    29/63

    S y m b o l Explana t ion

    Execute decision based oncondition.Test is performed and theprogram flow continues, based onthe result

    Indicates the beginning and end ofa flowchart.

    Show the continuing flowchart inthe same page.

    Decision

    Start / End FlowLines

    On-page

    connector FlowLines

    Methods to design a program:

    Basic flowchart symbol

    Step 3: Program Design

    31

  • 8/11/2019 Chapter 2-Problem Solving Methods

    30/63

    START

    Input

    Condition

    False statement

    Output

    END

    False

    True Statement True

    Methods to design a program:

    Flowchart symbol usage

    Step 3: Program Design

    32

  • 8/11/2019 Chapter 2-Problem Solving Methods

    31/63

    Flowchart to calculate the total of fine for late returning of library books.RM0.20 perday

    START

    Inputtotal_of_day

    Fine = total_of_day * 0.20

    Output Fine

    END

    33

    Example Flowchart

    Step 3: Program Design

  • 8/11/2019 Chapter 2-Problem Solving Methods

    32/63

    ACTIVITY 2:DISCUSSION(20 minutes)

    34

    1. Create 8 group (7 people/group)-

    2 minutes2. Get a topic, marker pen, paper3. Discuss in group4. Draw the flowchart in paper given5. Choose BEST GROUP as

    WINNER(faster, correct , creative and

    tidiness)

  • 8/11/2019 Chapter 2-Problem Solving Methods

    33/63

    Methods to design a program:

    3. PseudocodeSteps in problem solving that is written half inprogramming code and half in human language .

    Advantages:Easily understood.Easily maintained.The codes are changeable.

    Disadvantages:Cannot be executed in the computer.

    Step 3: Program Design

    35

  • 8/11/2019 Chapter 2-Problem Solving Methods

    34/63

    Example of Pseudocode

    36

    START Input quiz1, quiz2, quiz3 TotalMark= quiz1+quiz2+quiz3 Print TotalMark

    END

  • 8/11/2019 Chapter 2-Problem Solving Methods

    35/63

  • 8/11/2019 Chapter 2-Problem Solving Methods

    36/63

    Methods to design a program:

    4. Structure Charts An additional method in preparing programs thathas many sub modules .

    It consists rectangular boxes, which represents allthe sub modules in a program and is connectedby arrows .It illustrates the top-down design of a program

    and is also known as hierarchical chart becauseits components are in hierarchical form.The advantage is that it is easy to be drawn andto be changed.

    Step 3: Program Design

    38

  • 8/11/2019 Chapter 2-Problem Solving Methods

    37/63

    Structure Chart:The problem is normally big and complex.Thus, requires big program.Thus, the processing can be divided intosubtasks called modules.Each module accomplishes one function .

    These modules are connected to eachother to show the interaction of processingbetween the modules.

    Methods to design a program:

    Step 3: Program Design

  • 8/11/2019 Chapter 2-Problem Solving Methods

    38/63

    Methods to design a program:

    Structure chart

    Step 3: Program Design

    40

  • 8/11/2019 Chapter 2-Problem Solving Methods

    39/63

    ProblemWrite a Structure Charts to find an area of a circlewhere area = pi * radius * radius

    Area

    radius area = 3.14 xradius x radius Display area

    Example Structure ChartStep 3: Program Design

  • 8/11/2019 Chapter 2-Problem Solving Methods

    40/63

    Problem: To calculate the amount of water bill

    Represent the modules in the program

    Water bill

    Get /read

    data

    Computethe

    charge

    Displayinstructions

    for user

    Determinethe late

    charge

    Displaythe bill

    Printthe bill

    42

    Example Structure ChartStep 3: Program Design

  • 8/11/2019 Chapter 2-Problem Solving Methods

    41/63

    Steps 4: Program CodingDefinition: Writing problem solving into certainprogramming language such as C, COBOL , C++and others.Problem solving : Instructions before it is codedinto programming language.Purpose : To produce a program to develop asystemThe process of implementing an algorithm by

    writing a computer program using a programminglanguage (for example, using C or C++ language)The output of the program must be the solution of

    the intended problem .

    43

  • 8/11/2019 Chapter 2-Problem Solving Methods

    42/63

    Step 5: Testing And Verify

    Testing :Using a set of data to discover errors and toensure accuracy of the program.

    Process of testing: is the process of executing aprogram to demonstrate its correctness

    Program verification is the process of ensuringthat a program meets user-requirement

    44

  • 8/11/2019 Chapter 2-Problem Solving Methods

    43/63

    Two types of error:

    1. Syntax Error (grammatical error)Occurs when the rules of programming language are not applied .Correction is done during the programcoding.The bug can be traced during thecompilation .

    Also known as compile-time errorMust be corrected before executing andtesting the program.

    Step 5: Testing AndDebugging

    45

  • 8/11/2019 Chapter 2-Problem Solving Methods

    44/63

    2. Logic errorCannot be traced by compiler.

    Corrected during the problem solvingprocess.

    Also known as run time error.Example : output for average is 4, but whenit runs, the output is 2.

    Two types of error:

    Step 5: Testing AndDebugging

    46

    S 6 M i i A d

  • 8/11/2019 Chapter 2-Problem Solving Methods

    45/63

    Step 6: Maintain AndUpdateDefinition:

    Activity that verifies whether the operational system isperforming as planned or an activity to modify thesystem to meet the current requirement .The process of changing a system after it has been

    applied to maintain its ability. The changes mayinvolve simple changes such as error correcting.Process of changing a system after it has been

    delivered and is in use. Adaptive Maintenance - modifications to properly

    interface with changing environments -> newhardware, OS, devicesPerfective Maintenance - implementing new system

    requirements after system is successful

    47

  • 8/11/2019 Chapter 2-Problem Solving Methods

    46/63

    How to do maintenance?Testing Test the ability of the system.Measurement Access data time. Example, time tosave, print and others.Replacement Replace the old system to newsystem.

    Adjustment Adding needs to new system.Repair For example: An old system cannot updatethe new dataUpdating Update the database.

    Step 6: Maintenance

    48

  • 8/11/2019 Chapter 2-Problem Solving Methods

    47/63

  • 8/11/2019 Chapter 2-Problem Solving Methods

    48/63

    Content of Documentation:

    Description of the program.Specification of program requirementProgram design such as pseudocode and flowchartList of program and comments (to explain about the

    program).Test results.Users manual book. Program capabilities and limitation.

    Step 7: Documentation

    50

  • 8/11/2019 Chapter 2-Problem Solving Methods

    49/63

    Two Pair Sharing

    51

    In 5 minutes, write downanything that you have learned

    in chapter 2.Choose your pair and share it.(5 minutes)

    Pick 3 students from magic box

  • 8/11/2019 Chapter 2-Problem Solving Methods

    50/63

    Example 1

    Write a program that will get 3 numbers asinput from the users. Find the average anddisplay the three numbers and its average.

    Step 1: Define Problem

    52

    l

  • 8/11/2019 Chapter 2-Problem Solving Methods

    51/63

    Step 2: The Problem Analysis

    Input:numbers1, number2 and number3.

    Process:totalNumbers=number1+number2+number3average=totalNumbers/3

    Output:The three numbers and its average

    Example 1

    INPUT PROCESS OUTPUT

    53

    E l 1

  • 8/11/2019 Chapter 2-Problem Solving Methods

    52/63

    To calculate the average of three numbers.

    1. Set Total=0, Average=0;2. Input 3 numbers3. Total up the 3 numbers

    Total= total of 3 number

    4. Calculate average Average=Total/3

    5. Display 3 numbers and the average

    Step 3: The Program Design

    (using Algorithm)

    Example 1

    54

    E l 1

  • 8/11/2019 Chapter 2-Problem Solving Methods

    53/63

    55

    Step 3: The Program Design

    (using Pseudocode)

    Example 1

    E l 1

  • 8/11/2019 Chapter 2-Problem Solving Methods

    54/63

    56

    Step 4: The Program CodingExample 1

    #includevoid main(){float a,b,c,sum,av;couta>>b>>c;

    sum=a+b+c;av=sum/3;cout

  • 8/11/2019 Chapter 2-Problem Solving Methods

    55/63

    2.3 Apply The DifferentTypes Of Algorithm To SolveProblem

    57

  • 8/11/2019 Chapter 2-Problem Solving Methods

    56/63

  • 8/11/2019 Chapter 2-Problem Solving Methods

    57/63

    Concept Of Algorithm Algorithm:

    Must may have input(s ) and must have output(s )Should not be ambiguous (there should not be differentinterpretations to it)Must be general (can be used for different inputs)Must be correct and it must solve the problem for whichit is designedMust execute and terminate in a finite amount of timeMust be efficient enough so that it can solve theintended problem using the resource currentlyavailable on the computer

    Distinguish Between Flowchart

  • 8/11/2019 Chapter 2-Problem Solving Methods

    58/63

    Distinguish Between FlowchartAnd Pseudocode

    TYPES FLOWCHART PSEUDOCODE

    Layout Graphical structure Structure for thecode of the program

    Benefits For smallerconcepts andproblems

    More efficient forlarger programminglanguages

    Structure Symbols andshapes

    Linear text-basedstructure

    Depth Detail can causeconfusion

    More flexibility withdetail

    Example 1

  • 8/11/2019 Chapter 2-Problem Solving Methods

    59/63

    Example 1

    61

    A flowchart (and equivalent Pseudo code) tocompute the interest on a loan

    Flowchart Pseudocode

    Example 2

  • 8/11/2019 Chapter 2-Problem Solving Methods

    60/63

    Example 2

    62

    A program that reads two numbers and displaysthe numbers read in decreasing order

    Flowchart Pseudocode

    Read A, BIf A is less than B

    BIG = BSMALL = A

    elseBIG = A

    SMALL = BWrite (Display) BIG, SMALL

    TIME FOR ACTIVITY!!

  • 8/11/2019 Chapter 2-Problem Solving Methods

    61/63

    TIME FOR ACTIVITY!!(30 minutes)

    63

    Form a groupLeader in group find the question in class

    Go to your groupDiscuss with your group memberWrite your answer in Majung Paper

    (Algorithm, flowchart, pseudocode)Choose BEST GROUP as WINNER(faster, correct , creative and tidiness)

  • 8/11/2019 Chapter 2-Problem Solving Methods

    62/63

    EXERCISE

    64

    1. Write IPO, pseudo code and flow chart forthe program which can calculate totalsale by multiply quantity and price.

    2. Write an IPO, pseudo code and flow chartfor this program: any students whom gotCGPA more than 2.0 will pass their study,and less than 2.0 will fail.

  • 8/11/2019 Chapter 2-Problem Solving Methods

    63/63