L05extraSteps in Program Development - Revision

Embed Size (px)

Citation preview

  • 7/31/2019 L05extraSteps in Program Development - Revision

    1/45

    Steps in Program Development

    Define the problem

    Outline the solution

    Develop the outline into an algorithm

    Test the algorithm for correctness

    Code the algorithm into a programming language

    Run the program on computer Document and maintain the program

  • 7/31/2019 L05extraSteps in Program Development - Revision

    2/45

    Structured programming

    Top-down development

    Modular design

    Structure theorem

    Algorithms

    Pseudocode

  • 7/31/2019 L05extraSteps in Program Development - Revision

    3/45

    Top-down development

    Consider the problem as a whole and break

    it down into component parts.

    Each part becomes a problem to be solved

    in its own right.

    Using stepwise refinement, the problem is

    repeatedly broken down into smaller partsuntil it becomes manageable.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    4/45

    Modular design

    A module is

    a step or sub-task resulting from stepwise

    refinement.

    a problem solution which has a well-defined set

    of inputs, processing and outputs.

    Algorithms are written at module level.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    5/45

    The Structure Theorem

    Only three constructs should ever be used in

    programming:-

    1 Sequence

    2 Selection

    3 Iteration

    i.e. no unstructured GOTOs or STOPs.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    6/45

    Algorithm Definition

    Lists the steps involved in accomplishing a taskwhich must

    be lucid, precise and unambiguous

    give the correct solution in all cases

    eventually end

    Algorithms can be written in pseudocode, or can

    be expressed by means of a flowchart Other specification techniques include JSD or

    Nassi-Schneiderman diagram.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    7/45

    Pseudocode

    Statements are written in simple English.

    Each instruction is written on a separate line.

    Keywords and indentation are used to signifyparticular control structures.

    Each set of instructions is written from top to

    bottom with only 1 entry and 1 exit. Groups of statements may be formed into modules

    and that group can be given a name.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    8/45

    Stored program concept

    Data area

    Procedural area

    Input

    Output input Output

    Data

    Instructions

    Program

  • 7/31/2019 L05extraSteps in Program Development - Revision

    9/45

    Stored program concept

    Data area is specific and rigorously defined

    Procedural area can: -

    Take data into the data area from outside

    Put information out from the data area

    Manipulate the data in the data area

  • 7/31/2019 L05extraSteps in Program Development - Revision

    10/45

    Writing pseudocode

    6 basic operations:-

    1. Receive a piece of data

    2. Put out data

    3. Perform arithmetic

    4. Assign a value to a piece of data

    5. Select a group of actions

    6. Repeat a group of actions

  • 7/31/2019 L05extraSteps in Program Development - Revision

    11/45

    Receive data

    Takes data in from outside the algorithm

    Sample informal keywords used

    READ student name

    GET system date ...

    INPUT number1, number2

  • 7/31/2019 L05extraSteps in Program Development - Revision

    12/45

    Put out data

    Sends data out of the algorithm

    PRINT student number

    WRITE 'message

    OUTPUT name, address, staff number

  • 7/31/2019 L05extraSteps in Program Development - Revision

    13/45

    Perform arithmetic

    with specific pieces of data, where the

    operands and destination are explicitly

    specified.

    An operator

    An operand

    Source and destination

  • 7/31/2019 L05extraSteps in Program Development - Revision

    14/45

    Operations in arithmetic

    + Add

    - Subtract

    * Multiply

    / Divide

    () Precedence ** To the power of

  • 7/31/2019 L05extraSteps in Program Development - Revision

    15/45

    Assign a value to a piece of data

    Initialise TOTAL to 0

    Set COUNT to 0

    COUNT = COUNT + 1 ...

  • 7/31/2019 L05extraSteps in Program Development - Revision

    16/45

    Consider a problem

    Take three numbers into the program, add

    them together and output the result

  • 7/31/2019 L05extraSteps in Program Development - Revision

    17/45

    Defining the problem

    Determine INPUT

    Determine OUTPUT

    Determine processing

    This can be done using an IPO chart(Input, Processing, Output):

    INPUTNumber1Number2Number3

    PROCESSINGRead 3 numbers

    Add numbers togetherPrint total

    OUTPUTTotal

  • 7/31/2019 L05extraSteps in Program Development - Revision

    18/45

    Designing a solution algorithm

    Draw a rough sketch of the steps required tosolve the problem.

    Add-Three-NumbersRead Number1, Number2, Number3Total = Number1 + Number2 + Number3Print Total

    ENDThe algorithm is suitably named.

    The end of the algorithm is marked as END.

    Statements between the start and end are indented.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    19/45

    Checking the algorithm Choose 2 or 3 simple input test cases which are valid.

    Establish what the expected results should be.

    Make a table of the relevant variable names within thealgorithm

    Walk the first test case through the algorithm, keeping arecord of the contents of each variable in the table as thedata passes through the logic.

    Repeat the walkthrough process, using the other test data

    cases. Check the expected results against the actual results.

    This is NOT a program proof.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    20/45

    Selection - making choices

    How do you decide what to do?

    Look at the condition(s)

    Specify the alternatives very clearly

    Specify the conditions under which an

    alternative option will be taken very clearly

    Any instructions that are not optional shouldnot be subject to the condition.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    21/45

    Selection control structure

    Simple selection

    Null ELSE statement

    Combined IF statement

    Nested IF statement

    CASE statement

  • 7/31/2019 L05extraSteps in Program Development - Revision

    22/45

    Simple selectionif (accountbalance < 300) - condition

    servicecharge = 5.00- executed if

    condition true

    else

    servicecharge = 2.00- executed if

    condition false

    Endif

    Null ELSE statementIf StudentAttendance = ParttimeAdd 1 to Parttime-Count

    Endif

  • 7/31/2019 L05extraSteps in Program Development - Revision

    23/45

    Nested IF statement:

    If oper = *

    c = a * b

    elseif oper = +

    c = a + b

    else

    if oper = -

    c= a - b

    else

    display Illegal operator

    endif

    endif

    endif

    Display c, =, a, oper, b

  • 7/31/2019 L05extraSteps in Program Development - Revision

    24/45

    Case statement:

    Case oper of:

    *: c = a * b

    + : c= a + b

    -: c = a - bOther: display illegal operator

    End Case

    display c, =,a,operator,b

  • 7/31/2019 L05extraSteps in Program Development - Revision

    25/45

    switch (oper)

    {

    case '*':

    c = a * b; break;

    case '+':

    c = a + b; break;

    case '-':

    c = a - b; break;

    default:

    printf("Illegal operator\n");

    c = 0;}

    printf("%f = %d%c%d\n",c,a,oper,b);

    return 0;

    }

    Part B - case statement The block is enclosed

    in braces

    Once an appropriate

    option has been

    found, the code is

    executed

    If the break is

    omitted, following

    statements may also

    be carried out.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    26/45

    Structure theorem revisited

    6 basic operations:-

    1. Receive a piece of data

    2. Put out data

    3. Perform arithmetic

    4. Assign a value to a piece of data

    5. Select a group of actions6. Repeat a group of actions

  • 7/31/2019 L05extraSteps in Program Development - Revision

    27/45

    Repetition Control Structure

    Coding which needs to be repeated for a

    defined set of circumstances.

    Define the coding which needs to berepeated.

    Define the conditions under which the

    coding should be repeated.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    28/45

    1. Leading edge tested loop

    DOWHILE conditioncoding

    ENDDO

    Condition is tested before the coding is executed - if thecondition is false, no coding is executed.

    The coding is done WHILE the condition is TRUE. Thecondition is tested every time execution loops around.

    When the condition is found to be false, execution continues atthe next statement after the ENDDO

  • 7/31/2019 L05extraSteps in Program Development - Revision

    29/45

    Flowchart notation of WHILE

    Condition

    true?

    code

    True False

  • 7/31/2019 L05extraSteps in Program Development - Revision

    30/45

    Sample leading edge loop

    e.g. A temperature data entry consists of 15 entries,

    each containing a temperature in degrees

    Fahrenheit. A program is to be written which willread and print the temperatures in two columns on

    a report, one column in degrees Fahrenheit and the

    other in degrees Celsius. Column headings which

    read Degrees F and Degrees C are to be printed atthe top of the page.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    31/45

    IPO chart

    INPUT

    15 entries

    F-temp

    PROCESSING

    Print column headings

    For each entry

    Read F-tempConvert F-temp to C-temp

    Print F-temp, C-temp

    OUTPUT

    Headings

    F-temp

    C-temp

  • 7/31/2019 L05extraSteps in Program Development - Revision

    32/45

    Fahrenheit-Celsius conversion

    Print 'Degrees F' and 'Degrees C'

    Entry-counter = 0

    DOWHILE Entry-counter < 15

    Read F-tempC-temp = (F-temp - 32) * 5/9

    Print F-temp, C-temp

    Entry-counter = Entry-counter + 1

    ENDDOEND

    Don't forget to desk-check.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    33/45

    2. Trailing edge tested loop

    REPEATcoding

    UNTIL condition

    The coding is executed The condition is tested

    If the condition is false, the process is repeated

    If the condition is true, execution continues at the next line.

    e.g.Display "Do you wish to continue [Y/N]?"Repeat

    Input ANSWERuntil ANSWER = 'Y' or 'N

  • 7/31/2019 L05extraSteps in Program Development - Revision

    34/45

    Flowchart notation of Repeat

    Condition

    true?

    code

    True

    False

  • 7/31/2019 L05extraSteps in Program Development - Revision

    35/45

    Example

    A program is required to read a series of inventory entries

    which contain item number and stock figure. The last

    entry in the file has an item number of zero. The program

    is to produce a 'Low Stock Items' report, by printing onlythose entries which have a stock figure of less than 20

    items. A heading is to print at the top of the report and a

    total low stock item count to print at the end.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    36/45

    IPO chart

    INPUT

    Inventory entry with:

    Item number

    Stock figure

    PROCESSING

    Print heading

    For each entry:-

    Read EntryPrint Entry

    Increment Total-

    low-stock-items

    Print Total-low-stock-

    items

    OUTPUT

    Heading

    'Low stock items'

    Inventory list with:-Item number

    stock-figure

    Total-low-stock- items

  • 7/31/2019 L05extraSteps in Program Development - Revision

    37/45

    Process-inventory-entries (pseudocode)

    Set Total-Low-Stock-Items to zeroPrint 'Low-Stock Items' HeadingREPEAT

    Read inventory Entry

    IF item number > 0 THENIF stock figure < 20 THEN

    print item number,stock figure

    increment Total-Low-Stock-ItemsENDIF

    ENDIF

    UNTIL item number = 0Print Total-Low-Stock-Items

    END

  • 7/31/2019 L05extraSteps in Program Development - Revision

    38/45

    3. Auto-increment loop

    If the exact number of loop iterations is known in advance,

    then the execution of the loop can be controlled by a loop

    index or counter, which is initialised for the first pass through

    the loop, auto-increments on each pass through the loop andstops at a predestined point:-

    DO loop-index = initial-value to final-value

    statement blockENDDO

  • 7/31/2019 L05extraSteps in Program Development - Revision

    39/45

    How the DO loop works

    The DO loop does the following:-

    Initialise the loop-index to the required initial-value

    increment the loop-index by 1 for each pass through the

    loop

    test the value of loop-index at the beginning of each

    loop to ensure that it is within the stated range of valuesterminate the loop when the loop-index has

    exceeded the specified final-value

  • 7/31/2019 L05extraSteps in Program Development - Revision

    40/45

    Example

    A program is required to read a file of 350 student entries

    containing student number, sex and age and to produce a

    STUDENT LIST. If a student is under 18 years of age,

    then an asterisk '*' is to be printed beside that student'sdetails. A total of the number of students under 18 years

    of age is also to be printed at the end of the report.

  • 7/31/2019 L05extraSteps in Program Development - Revision

    41/45

    IPO chart

    INPUT

    350 student

    entries

    studnt-no

    sex

    age

    PROCESSING

    Print heading

    For each student:

    read Entry

    print details

    print '*

    increment total-students-

    under-18

    Print total-students-under-18

    OUTPUT

    Heading'STUDENT LIST'

    Student details containing:studnt-no

    sex

    age

    Total-students-under-18

  • 7/31/2019 L05extraSteps in Program Development - Revision

    42/45

    Print-student-list1 Print 'STUDENT LIST' Heading

    2 Set Total-Students-Under-18 to zero

    3 DO loop-index = 1 to 350

    4 read student Entry

    5 print student details

    IF age

  • 7/31/2019 L05extraSteps in Program Development - Revision

    43/45

    Desk checking

    For two valid entries (change loop size to 2 from 350): -

    Number Sex Age

    Record 1 14872 M 25

    Record 2 15543 F 17

  • 7/31/2019 L05extraSteps in Program Development - Revision

    44/45

    Desk checkingstatemnt Tot-u-18 Loop

    idx

    Studnt-

    no

    Sex Age PrintStud? Print * Final

    1 - - - - - - -2 0 - - - - - -

    3 0 1 - - - - -4 0 1 14872 M 25 - - -

    5 0 1 14872 M 25 Yes - -6 / / / / / / /

    7 / / / / / / / 3 0 2 14872 M 25 - - -

    4 0 2 15543 F 17 - - -

    5 0 2 15543 F 17 Yes - -6 0 2 15543 F 17 - Yes -

    7 1 2 15543 F 17 - - -

    8 Yes

  • 7/31/2019 L05extraSteps in Program Development - Revision

    45/45

    Comparison between WHILE and

    REPEATWHILE

    Add-numbers

    Sum = 0Read number

    While number not = 999

    sum = sum + number

    read number

    end-while

    Print sum

    END

    REPEAT

    Add-Numbers

    Sum = 0Repeat

    read number

    if number not = 999

    sum = sum + number

    end-if

    until number = 999

    Print sum

    END