ProblemSolving Part 1 Algorithms and Pseudo Coding

Embed Size (px)

Citation preview

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    1/54

    Problem SolvingPart 1

    Algorithms

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    2/54

    Topic & Structure of the lesson

    In this chapter you will learn about:

    Problem Solving

    Algorithm Pseudocodes

    Flowcharts

    Slide 2 of 40

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    3/54

    Key Terms you must be able to use

    If you have mastered this topic, you should beable to use the following terms correctly in your assignments and exams :

    program

    pseudocode

    flowchart

    algorithm5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    4/54

    Problem Solving Techniques

    In this chapter you will learn about:

    What problem solving is

    The software development method of problem solving using computersBasic algorithm control structures

    The sequence structure

    The selection structureThe repetition structure

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    5/54

    Problem Solving Techniques

    By the time you have completed this chapter,you willhave acquired the ability to:

    Apply the software development method to solveproblems

    Difference between the Algorithm & theFlowchart

    Knowing about the control structures

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    6/54

    Problem Solving

    A great discovery solves a great problem but there is a grain of discovery in the solution of any problem. Your problem may bemodest; but if it challenges your curiosity and brings into play

    your inventive faculties, and if you solve it by your own means,you may experience the tension and enjoy the triumph of discovery. Such experiences at a susceptible age may create ataste for mental work and leave their imprint on mind and character for a lifetime.

    George Polya

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    7/54

    Problem Solving

    First:

    You have tounderstand theproblem.

    UNDERSTANDING THE PROBLEM

    What is the unknown? What are the data?Whatis the condition?Is it possible to satisfy the condition?Is thecondition sufficient to determine the unknown?Or is it sufficient?Or Redundant? Or Contradictory?Draw a figure.Introduce suitablenotation.Separate the various parts of thecondition.Can you write them down?

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    8/54

    Problem Solving

    Second:Find the connectionbetween the data andthe unknown.

    Auxiliary problemsmay be devised if needed.

    You should obtaineventually a plan of the solution.

    DEVISING A PLANHave you seen it before? Or have youseen the same problem in slightly differentform?Do you know a related problem?Look at the unknown! Try to think of afamiliar problem having the same or similar unknown. Split the problem into smaller,

    simple sub-problems. If you cannot solvethe proposed problem try to solve firstsome related problem. Or solve moregeneral problem. Or special case of theproblem. Or solve the part of the problem.

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    9/54

    Problem Solving

    Third:

    Carry out your

    plan.

    CARRYING OUT THE PLAN

    Carrying out your plan of the solution,check

    each step. Can you see clearly that step iscorrect? Can you prove that it is correct?

    Fourth:

    Examine thesolutionobtained.

    LOOKING BACK

    Can you check the result? Can you derivethe result differently? Can you use theresult, or the method, for some other problem?

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    10/54

    Algorithmic Problem Solving

    Algorithmic problem:

    Any problem whose solution can be expressed as a set of executable instructions.

    Algorithm:

    A well defined computational procedure consisting of aset of instructions , that takes some value or set of values,as input , and produces some value or set of values, asoutput.

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    11/54

    Algorithmic Problem Solving

    Derived from the name of Mohammed al-khowarizmi, aPersian mathematician in the ninth century.

    Al-khowarizmi--Algorismus(in Latin)--Algorithm

    An algorithm is like a recipe, that converts theingredients into some culinary dish.

    The formal written version is a program.Algorithms/programs are the software .The machinethat runs the programs is the hardware .

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    12/54

    Algorithmic Problem Solving

    Ingredient

    Recipe(software)

    Cooking utensils(hardware)

    Al-gongBah-kut-the

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    13/54

    Characteristics of an Algorithm

    Each step of an algorithm must be exact,preciously and ambiguously described.

    It must terminate, i.e. it contains a finitenumber of steps.

    It must be effective, i.e.., produce the correctoutput.

    It must be general, i.e.. to solve everyinstance of the problem.

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    14/54

    Characteristics of an Algorithm

    An Algorithm is implemented in some p r o g r a m m i n g language.

    prog ram = Algo r i thm + Data St ruc tu res .

    Data Struc tures refer to the types of data used andhow the data are organized in the program.

    An algorithm is usually presented in the form of somepseudo-code , which is a mixture of Englishstatement,some mathematical notations,and selectedkeywords from a programming language.

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    15/54

    Characteristics of an Algorithm

    PROBLEM:

    You are required to design a completesystem which will enable the sum of twovalues to be calculated.

    An Algorithm should emphasize theWHAT s and not the HOWs . Consider theproblem below:

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    16/54

    Problem Solving

    To grapple with this problem, we have tounderstand the problem from the human

    perspective. A question to ask yourself is this,

    How Would You Calculate the Sum of TwoValues?

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    17/54

    Problem Solving

    As the computer is also a device similar to the wayin which the human brain functions, the process of calculating the sum of two values can also be easily

    performed by the computer.

    =5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    18/54

    Problem Solving

    Input

    Processing(Brains)

    Output

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    19/54

    Problem Solving

    Input Device

    Output Device

    CPU

    (Brains)

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    20/54

    Problem Solving

    5 10

    15

    5 + 10 = 15

    Input

    Processing

    OutputLet us assume we are interested in calculating the sum of 5

    and 10.5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    21/54

    Problem Solving

    As shown previously, the example values (5 and10) have been specified explicitly .

    As the brain is flexible enough in calculating awide range of numbers , the two input valueshave to be generalised .

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    22/54

    Problem Solving

    Value1Value2

    Sum

    Sum = Value1 + Value2

    Notice that instead of using specific numbers ,

    variables are used to represent these values.5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    23/54

    What Are Variables?

    Variables are memory locations within the computer whichallows pieces of data to be stored.

    The word variable comes from the word vary , which means

    that whatever you place within a variable can be changed .

    A variable can be viewed as a container used to storethings.

    Data (for example, name,age, salary) can be stored in

    these containers.5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    24/54

    What Are Variables?

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    25/54

    Problem Solving

    Now that we have an exact idea about how theproblem is solved, let us represent this in a clearer manner, using the defining diagram .

    Input Processing Output

    Value1

    Value2

    Sum

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    26/54

    Problem Solving

    The next step is to identify the actual processing steps required to convert the input to become theoutput .

    Input Processing Output

    Value1

    Value2

    Sum1) Read Value1, Value2

    2) Calculate Sum

    3) Display Sum

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    27/54

    Algorithm Development

    Once the defining diagram has beendeveloped, the next logical step is to developthe algorithm (which is much more detailed).

    Input Processing Output

    Value1

    Value2

    Sum1) Read Value1, Value2

    2) Calculate Sum

    3) Display Sum

    The developed processing steps have to be more

    detailed in the algorithm.5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    28/54

    Algorithm Development

    Example of an algorithm (using pseudocodes) whichcan be used to carry out the tasks outlined in the

    defining diagram is as follows:-

    1) Read Value1, Value2

    2) CalculateSum = Value1 + Value2

    3) Display Sum5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    29/54

    Pseudo Coding

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    30/54

    Pseudocoding

    A Pseudocode language is semiformal, English-like language with a limited vocabulary that can beused to design and describe algorithms.

    The pseudocode language can be used for:

    Designing algorithms

    Communicating algorithms as programs

    Implementing algorithms as programs

    Debugging logic errors in program

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    31/54

    Pseudocode for the Control Structures

    The Sequence Control Structure:

    The sequence co n t ro l s t ruc tu re is a series of stepsor statements that are executed in the order in whichthey are written in an algorithm.For Example:

    read taxable inc om e

    read f i l ing s ta tus com pute incom e tax

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    32/54

    Contd

    The Selection Control Structure:The se lec t ion co nt ro l s t ruc ture defines two courses of action, depending on the outcome of a condition. Acondition is an expression that, when evaluated, computesto either true or false.

    Syntax is: i f cond i t ion

    then-par t

    else

    else-part

    end-if

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    33/54

    Decision Making

    Being able to mimic the way the human brainworks, the computer also has the ability to makedecisions .

    Decision making can be represented inpseudocodes using the IF...THEN construct.

    IF ( expression ) THEN::

    ENDIF5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    34/54

    Decision Making

    IF ( expression ) THEN:

    :ENDIF

    The expression is a comparison betweentwo values which evaluates to either true of false .

    Statements are

    placed here.5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    35/54

    Decision Making

    Example:-

    We are looking for a job which pays more thanRM4000.

    IF (Salary>4000 ) THENSay "I Will Take The Job!!"

    ENDIF

    Example of anExpression

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    36/54

    Decision Making

    Commonly used relational operators in expressions :-

    > Greater Than

    < Less Than

    = Equals To

    < > Not Equals To

    >= Greater Than or Equals To

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    37/54

    Decision Making

    Since all expressions works out to be either true or false ,what the IF..THEN statement represents is a two-state condition.

    For example,

    A potential employer is waiting for you to give a reply (on thespot) about the job offer with a salary of RM2000. Your decision would be to only take a job worth more thanRM4000. What would you say?

    IF (Salary>4000 ) THENSay YES!

    ELSESay NO!

    ENDIF5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    38/54

    Decision Making

    Certain conditions may give rise to more thanone expression being evaluated. These areknown as compound expressions .

    Example:-

    You are interested in taking up a job which paysmore than RM4000 and that the company must

    also provide a credit card .IF (Salary>4000) And (CreditCard=YES) THEN

    Take Job!!ENDIF

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    39/54

    Decision Making

    Compound expressions can be representedusing the following operators :-

    AND Every expression must evaluate to be

    true in order for the whole expression tobe true.

    OR As long as any one of the expression

    can be true, the entire IF statement willbe true.

    NOT The inverse (opposite ) of the entireexpression.

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    40/54

    Decision Making

    IF statements can be nested , that is, placedwithin another IF statement.

    This is used in situations when the expressionis more complex than the simple decisions (asseen earlier).

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    41/54

    Decision Making

    IF (Salary>4000) And (CreditCard=YES) THENSay Yes I Will Take The Job!!

    ENDIF

    For example, this statement.........

    can be represented like this.........

    IF (Salary>4000) THENIF (CreditCard=YES) THENSay Yes I Will Take The Job!!

    ELSESay No Credit Card? Say Sorry!!

    ENDIFELSE

    Say Not Enough Pay!! ENDIF

    ........ whereby more possibilities can be represented.5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    42/54

    Decision Making

    For good practice ...........

    IF (Salary>4000) THENIF (CreditCard=YES) THEN

    Say Yes I Will Take The Job!! ELSE

    Say No Credit Card? Say Sorry!!

    ENDIFELSE

    Say Not Enough Pay!! ENDIF

    ........ ensure that statements are properlyindented to indicate block of statements

    which belong together.5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    43/54

    Contd

    For Example:

    i f a i s g reater than b then

    p r in t A is greater

    e lse

    p r in t B is greater

    end i f

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    44/54

    Contd

    Repetition Control Structure:

    The repet i t ion co nt ro l s t ruc tu re specifies a block of one or more statements that are repeatedly executeduntil a condition is satisfied.

    Syntax is:

    wh i le cond i t ion

    loop-body

    end-while

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    45/54

    Looping Constructs

    Looping constructs (also known as repetition or iteration constructs) are a kind of construct foundin pseudocodes which allows statements (or a

    group of statements) to be repeated .The main reason why looping constructs areprovided is because most of the problems which

    we encounter everyday requires some degree of repetition .

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    46/54

    Looping Constructs

    An example of a process which is iterative:-

    Payroll processing is very much an iterative process as the person processing thepayroll applies the same calculations for each employee to produce the pay slip .

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    47/54

    Looping Constructs

    The looping constructs available in pseudocodesare as follows:-

    DOWHILE...ENDDO

    FOR NEXT

    REPEAT...UNTIL

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    48/54

    Looping Constructs

    The format of theDOWHILE...ENDDO construct isshown below:-

    DOWHILE ( expression )::

    :ENDDO

    Group of statements

    An expression which determineswhether the loop will continue.

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    49/54

    Looping Constructs

    The format of the FOR...NEXTconstruct is shown below:-

    FOR (initialze TO expression) STEP increment::

    :ENDDO

    Group of statements

    An expression which determineswhether the loop will continue.

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    50/54

    Looping Constructs

    The format of theREPEAT...UNTIL construct isshown below:-

    REPEAT::

    :UNTIL ( expression )

    Group of statements

    An expression which determineswhether the loop will continue.

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    51/54

    Looping Constructs

    Take a look at the following example:-

    You are required to develop a complete system

    which will allow the total payroll to becalculated.

    The system is required to read in the amount to

    be paid for each employee .The moment the system receives an inputvalue of -99, the system is required to stop anddisplay the total payroll .

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    52/54

    Looping Constructs

    Input Processing Output

    Salary Total1) Read Salary

    2) Calculate Total

    3) Display Total

    The Defining Diagram

    5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    53/54

    Looping Constructs

    Algorithm (Using Pseudocodes)

    1) Display "Enter Salary"

    2) Read Salary

    3) Total = 0

    4) DOWHILE (Salary-99)

    Total = Total + Salary

    Display "Enter Salary"Read Salary

    ENDDO

    5) Display "Total Payroll = ", Total5/9/2013

  • 7/30/2019 ProblemSolving Part 1 Algorithms and Pseudo Coding

    54/54

    Contd

    Example:

    Dowhile (income is less than 50000) print Enter taxable income;should begreater than or equal to 50000 read income

    Enddo