02 03 Design

Embed Size (px)

Citation preview

  • 8/12/2019 02 03 Design

    1/35

    Introduction to C Programming

    Program design

  • 8/12/2019 02 03 Design

    2/35

    CT018-3-1 Introduction To C Programming Program Design

    Topic & Structure of the lesson

    In this chapter you will learn about:

    Problem Solving

    Algorithm

    Pseudocodes

    Flowcharts

  • 8/12/2019 02 03 Design

    3/35

    CT018-3-1 Introduction To C Programming Program Design

    Learning Outcomes

    If you have mastered this topic, you should be ableto use the following terms correctly in your

    assignments:

    Algorithm

    Pseudocode

    Flowchart

  • 8/12/2019 02 03 Design

    4/35

    CT018-3-1 Introduction To C Programming Program Design

    Problem Solving

    First:

    ou have tounderstandthe problem!

    U!"#ST$!I% T" P#O'L"(

    "hat is the un#nown$ "hat are the data$"hat is the condition$

    Is it possible to satisfy the condition$Is thecondition sufficient to determine theun#nown$%r is it sufficient$%r &edundant$%r Contradictory$

    'raw a figure!Introduce suitablenotation!Separate the various parts of thecondition!Can you write them down$

  • 8/12/2019 02 03 Design

    5/35

    CT018-3-1 Introduction To C Programming Program Design

    Problem Solving

    Second:

    Find the connectionbetween the data and

    the un#nown!Au(iliary problemsmay be devised ifneeded!

    ou should obtaineventually a plan ofthe solution!

    !")ISI% $ PL$

    )ave you seen it before$ %r have youseen the same problem in slightly differentform$

    'o you #now a related problem$*oo# at the un#nown+ ry to thin# of afamiliar problem having the same or similarun#nown! Split the problem into smaller,simple sub-problems! If you cannot solve

    the proposed problem try to solve firstsome related problem! %r solve moregeneral problem! %r special case of theproblem! %r solve the part of the problem!

  • 8/12/2019 02 03 Design

    6/35

    CT018-3-1 Introduction To C Programming Program Design

    Problem Solving

    hird:

    Carry out yourplan!

    *$##+I% OUT T" PL$

    Carrying out your plan of the solution,chec#each step! Can you see clearly that step iscorrect$ Can you prove that it is correct$

    Fourth:

    .(amine thesolutionobtained!

    LOO,I% '$*,

    Can you chec# the result$ Can you derivethe result differently$ Can you use theresult, or the method, for some otherproblem$

  • 8/12/2019 02 03 Design

    7/35CT018-3-1 Introduction To C Programming Program Design

    $lgorithm

    Algorithmic problem:

    Any problem whose solution can be

    e(pressed as a set of e(ecutable instructions!

    Algorithm:

    A well defined computational procedure

    consisting of a set of instructions, that ta#essome value or set of values, as input, and

    produces some value or set of values, as

    output.

  • 8/12/2019 02 03 Design

    8/35CT018-3-1 Introduction To C Programming Program Design

    *haracteristics of an $lgorithm

    .ach step of an algorithm must be e(act, preciouslyand ambiguously described!

    It must terminate, i!e! it contains a finite number ofsteps!

    It must be effective, i!e!!, produce the correct output!

    It must be general, i!e!! to solve every instance of the

    problem! An Algorithm is implemented in someprogramming language.

    program = Algorithm + Data Structures.

  • 8/12/2019 02 03 Design

    9/35CT018-3-1 Introduction To C Programming Program Design

    Problem Solving

    Slide 26 (of 80)

    /ow that we have an exact idea about how the

    problem is solved, let us represent this in a clearer

    manner, using the defining diagram!

    Input Processing %utput

    0alue1

    0alue2

    Sum

  • 8/12/2019 02 03 Design

    10/35CT018-3-1 Introduction To C Programming Program Design

    Problem Solving

    Slide 2 (of 80)

    he ne(t step is to identify the actual processingsteps re3uired to convert the input to become the

    output!

    Input Processing %utput

    0alue1

    0alue2

    Sum14 &ead 0alue1, 0alue2

    24 Calculate Sum

    54 'isplay Sum

  • 8/12/2019 02 03 Design

    11/35CT018-3-1 Introduction To C Programming Program Design

    $lgorithm !evelopment

    Slide 28 (of 80)

    %nce the defining diagram has been

    developed, the ne(t logical step is to developthe algorithm6which is much more detailed4!

    Input Processing %utput

    0alue1

    0alue2

    Sum14 &ead 0alue1, 0alue2

    24 Calculate Sum

    54 'isplay Sum

    he developed processing steps have to be more

    detailed in the algorithm!

  • 8/12/2019 02 03 Design

    12/35CT018-3-1 Introduction To C Programming Program Design

    Operators

    Slide 2! (of 80)

    he basic mathematical operators used in algorithmsare as follows:-

    7 addition

    - subtraction

    8 multiplication

    9 division

    assignment

    6 4 brac#ets for grouping calculations

  • 8/12/2019 02 03 Design

    13/35CT018-3-1 Introduction To C Programming Program Design

    $lgorithm !evelopment

    Slide 30 (of 80)

    .(ample of an algorithm 6using pseudocodes4 which

    can be used to carry out the tas#s outlined in the

    defining diagram is as follows:-

    14 &ead 0alue1, 0alue2

    24 CalculateSum 0alue1 7 0alue2

    54 'isplay Sum

  • 8/12/2019 02 03 Design

    14/35CT018-3-1 Introduction To C Programming Program Design

    Pseudocoding

    Slide 31 (of 80)

    A Pseudocode language is semiformal, .nglish-li#elanguage with a limited vocabulary that can be usedto design and describe algorithms!

    he pseudocode language can be used for:

    'esigning algorithms

    Communicating algorithms as programs

    Implementing algorithms as programs

    'ebugging logic errors in program

  • 8/12/2019 02 03 Design

    15/35CT018-3-1 Introduction To C Programming Program Design

    Program -lowcharts

    Slide 62 (of 80)

    As humans are more inclined towards

    understanding diagrams and pictures rather

    than words, pseudocodes tends to becometedious to understand if too lengthy!

    Program flowcharts, because they are

    represented graphically, ma#es understandingeasier!

  • 8/12/2019 02 03 Design

    16/35CT018-3-1 Introduction To C Programming Program Design

    -lowcharting

    Slide 66 (of 80)

    Another techni3ue used in designingand representingalgorithms!

    Alternative to pseudocoing

    A pseudocode description is verbal, a flowchart isgraphicalin nature!

    !efinition:

    A flowchart is a graph consisting of geometrical shapesthat are connected by flow lines!

  • 8/12/2019 02 03 Design

    17/35CT018-3-1 Introduction To C Programming Program Design

    Program -lowcharts

    Slide 63 (of 80)

    he following are the commonly used

    symbols for drawing program flowcharts!

    terminator off-page

    connector

    process storage

    decision

    ma#ingdocument

    input9output connector

    arrowheads

  • 8/12/2019 02 03 Design

    18/35CT018-3-1 Introduction To C Programming Program Design

    Pseudocode for the *ontrol Structures

    Slide 32 (of 80)

    The Se.uence *ontrol Structure:

    he sequence control structureis a series of stepsor statements that are e(ecuted in the order in whichthey are written in an algorithm!

    For .(ample:

    read taxable income

    read filing status compute income tax

  • 8/12/2019 02 03 Design

    19/35

    CT018-3-1 Introduction To C Programming Program Design

    !ecision (a/ing

    Slide 33 (of 80)

    The Selection *ontrol Structure:

    he selection control structure defines two courses ofaction, depending on the outcome of a condition! Acondition is an e(pression that, when evaluated, computesto either true or false!

    Synta( is:

    if condition

    thenpart

    else

    elsepart

    endif

  • 8/12/2019 02 03 Design

    20/35

    CT018-3-1 Introduction To C Programming Program Design

    !ecision (a/ing

    Slide 3 (of 80)

    Commonly used relational operators in expressions:-

    ;

  • 8/12/2019 02 03 Design

    21/35

    CT018-3-1 Introduction To C Programming Program Design

    !ecision (a/ing

    Slide "0 (of 80)

    Compound e(pressions can be representedusing the followingoperators:-

    A/' .very e(pression must evaluate to be

    truein order for the whole e(pression tobe true!

    %& As long as any one of the e(pression

    can be true, the entire IF statement willbe true!

    /% he inverse 6opposite4 of the entire

    e(pression!

  • 8/12/2019 02 03 Design

    22/35

    CT018-3-1 Introduction To C Programming Program Design

    !ecision (a/ing 0 "1ample

    Slide 38 (of 80)

    A potential employer is waiting for you to give a reply 6on thespot4 about the ?ob offer with a salary of &@2! our

    decision would be to only ta#e a ?ob worth more than

    &@B! "hat would you say$

    IF 6Salary>40004 )./Say .S+D

    .*S.

    Say /%+D

    ./'IF

  • 8/12/2019 02 03 Design

    23/35

    CT018-3-1 Introduction To C Programming Program Design

    !ecision (a/ing 0 "1ample

    Slide 3! (of 80)

    Certain conditions may give rise to more thanone e(pression being evaluated! hese are

    #nown as compound expressions!

    .!g! ou are interested in ta#ing up a ?ob which

    pays morethan &@B and that the company

    must also provide a credit card!

    IF 6Salary;B4 And 6CreditCard.S4 )./

    a#e Eob++

    ./'IF

  • 8/12/2019 02 03 Design

    24/35

    CT018-3-1 Introduction To C Programming Program Design

    #epetition

    Slide "# (of 80)

    #epetition *ontrol Structure:

    he repetition control structure specifies a bloc# ofone or more statements that are repeatedly e(ecuteduntil a condition is satisfied!

    Synta( is:

    while condition

    loopbod!

    endwhile

  • 8/12/2019 02 03 Design

    25/35

    CT018-3-1 Introduction To C Programming Program Design

    Looping *onstructs

    Slide "6 (of 80)

    *ooping constructs 6also #nown as repetition or

    iterationconstructs4 are a #ind of construct found

    in pseudocodes which allows statements 6or a

    group of statements4 to be repeated!

    he main reason why looping constructs are

    provided is because most of the problems which

    we encounter everyday re3uires some degree ofrepetition!

  • 8/12/2019 02 03 Design

    26/35

    CT018-3-1 Introduction To C Programming Program Design

    Looping *onstructs

    Slide "8 (of 80)

    he looping constructs available in pseudocodes

    are as follows:-

    '%")I*.!!!./''%

    F%&/.G

    &.P.A!!!H/I*

  • 8/12/2019 02 03 Design

    27/35

    CT018-3-1 Introduction To C Programming Program Design

    Looping *onstructs

    Slide "! (of 80)

    he format of the '%")I*.!!!./''% construct is shownbelow:-

    '%")I*. 6expression4

    :

    :

    :

    ./''%

  • 8/12/2019 02 03 Design

    28/35

    CT018-3-1 Introduction To C Programming Program Design

    Looping *onstructs

    Slide #0 (of 80)

    he format of the F%&!!!/.G construct is shown below:-

    F%& 6initiale % e(pression4 S.P increment

    :

    ::

    /.G

  • 8/12/2019 02 03 Design

    29/35

    CT018-3-1 Introduction To C Programming Program Design

    Looping *onstructs

    Slide #1 (of 80)

    he format of the&.P.A!!!H/I* construct is

    shown below:-

    &.P.A:

    :

    :

    H/I* 6expression4

  • 8/12/2019 02 03 Design

    30/35

    CT018-3-1 Introduction To C Programming Program Design

    .(ample

    !owhile2 "nddo

    Slide ## (of 80)

    Dowhile (income is less than 50000)

    print nter taxa!le income"

    should !e greater than or e#ual

    to 50000$

    read income

    nddo

  • 8/12/2019 02 03 Design

    31/35

    CT018-3-1 Introduction To C Programming Program Design

    $rray

    Contiguous, homogeneous collection of data

    values that share a common name!

    lement - one value in an array!

    %ndex 6subscript4 - an integer indicating a

    position in an array! Array inde(es start with !

    3 4 5 6 7 8 9 ;

    1 2 55 BB 1L M 1N OO 2

  • 8/12/2019 02 03 Design

    32/35

    CT018-3-1 Introduction To C Programming Program Design

    $rray 0 "1ample

    &eplace every third element in the following

    array with value 1!

    /ote: /ame: numberQ Sie 6elements4 O

    3 4 5 6 7 8 9

    5 B 2 5 L N 1 N

    3 4 5 6 7 8 9

    1 B 2 1 L N 1 N

    F%& 6inde( % N4 S.P 5

    numberRinde( 1

    /.G inde(

    or

    Set inde( to

    F%& every third element in the number

    replace e(isting value with 1

    increase inde( by 5

    ./'F%&

  • 8/12/2019 02 03 Design

    33/35

    CT018-3-1 Introduction To C Programming Program Design

    !esign

  • 8/12/2019 02 03 Design

    34/35

    CT018-3-1 Introduction To C Programming Program Design

    !esign egin

    &ead Amount

    .nd

    Amount;2!$

    Calculate

    ActualAmount 8 !O

    Calculate

    ActualAmount

    /%.S

  • 8/12/2019 02 03 Design

    35/35

    Summary

    An algorithmis a se3uence of a finite number of stepsarranged in a specific logical order that, when e(ecuted,produce the solution for a problem!

    A pseudocodelanguage is a semiformal,.nglish-li#elanguage with a limited vocabulary that can be used todesign and describe algorithms!

    $ -lowchartis a graphical representation of analgorithm!