Steps in Program Development Define the problem Outline the solution Develop the outline into an...

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
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    0

Transcript of Steps in Program Development Define the problem Outline the solution Develop the outline into an...

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

Structured programming

• Top-down development

• Modular design

• Structure theorem

• Algorithms

• Pseudocode

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 parts until it becomes manageable.

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.

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.

Algorithm Definition

• Lists the steps involved in accomplishing a task which 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.

Pseudocode

• Statements are written in simple English.• Each instruction is written on a separate line.• Keywords and indentation are used to signify

particular 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.

Stored program concept

• Data area• Procedural area• Input• Output input Output

Data

Instructions

Program

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

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

Receive data

• Takes data in from outside the algorithm

• Sample informal keywords used– READ student name …– GET system date ... – INPUT number1, number2

Put out data

• Sends data out of the algorithm – PRINT student number – WRITE 'message’– OUTPUT name, address, staff number

Perform arithmetic

• with specific pieces of data, where the operands and destination are explicitly specified.

• An operator

• An operand

• Source and destination

Operations in arithmetic

• + Add

• - Subtract

• * Multiply

• / Divide

• () Precedence

• ** To the power of

Assign a value to a piece of data

• Initialise TOTAL to 0 …

• Set COUNT to 0

• COUNT = COUNT + 1 ...

Consider a problem

• Take three numbers into the program, add them together and output the result

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

Designing a solution algorithm

Draw a rough sketch of the steps required to solve the problem.

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

END

The algorithm is suitably named.

The end of the algorithm is marked as END.

Statements between the start and end are indented.

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 the

algorithm• Walk the first test case through the algorithm, keeping a

record of the contents of each variable in the table as the data 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.

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 should

not be subject to the condition.

Selection control structure

• Simple selection

• Null ELSE statement

• Combined IF statement

• Nested IF statement

• CASE statement

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 = Parttime

Add 1 to Parttime-Count

Endif

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

Case statement:

Case oper of:

‘*’: c = a * b

‘+’ : c= a + b

‘-’: c = a - b

Other: display ‘illegal operator’

End Case

display c, ‘=‘,a,operator,b

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.

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 actions

6. Repeat a group of actions

Repetition Control Structure

• Coding which needs to be repeated for a defined set of circumstances.

• Define the coding which needs to be repeated.

• Define the conditions under which the coding should be repeated.

1. Leading edge tested loop

DOWHILE conditioncoding

ENDDO

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

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

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

Flowchart notation of WHILE

Conditiontrue?

code

True False

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 will read 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 at the top of the page.

IPO chart

INPUT15 entries F-temp

PROCESSING

Print column headings

For each entry Read F-temp Convert F-temp to C-temp

Print F-temp, C-temp

OUTPUTHeadingsF-tempC-temp

Fahrenheit-Celsius conversion

Print 'Degrees F' and 'Degrees C'Entry-counter = 0DOWHILE Entry-counter < 15

Read F-tempC-temp = (F-temp - 32) * 5/9Print F-temp, C-tempEntry-counter = Entry-counter + 1

ENDDOEND

Don't forget to desk-check.

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’

Flowchart notation of Repeat

Conditiontrue?

code

True

False

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 only those 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.

IPO chart

INPUTInventory entry with: Item number Stock figure

PROCESSING

Print heading

For each entry:- Read Entry Print 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

Process-inventory-entries (pseudocode)

Set Total-Low-Stock-Items to zeroPrint 'Low-Stock Items' HeadingREPEAT Read inventory Entry IF item number > 0 THEN

IF stock figure < 20 THEN print item number,stock figure increment Total-Low-Stock-ItemsENDIF

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

END

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 and stops at a predestined point:-

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

ENDDO

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 values

– terminate the loop when the loop-index has

exceeded the specified final-value

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's details. A total of the number of students under 18 years of age is also to be printed at the end of the report.

IPO chart

INPUT350 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

Print-student-list1 Print 'STUDENT LIST' Heading2 Set Total-Students-Under-18 to zero3 DO loop-index = 1 to 3504 read student Entry5 print student details

IF age <18 THEN6 print '*’7 increment Total-Students-Under-18

ENDIF ENDDO8 Print Total-Student-Under-18END

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

Desk checkingstatemnt Tot-u-18 Loop

idxStudnt-

noSex 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

Comparison between WHILE and REPEAT

WHILE

Add-numbersSum = 0Read numberWhile number not = 999

sum = sum + number

read numberend-whilePrint sum

END

REPEAT

Add-Numbers

Sum = 0

Repeat

read number if number not = 999

sum = sum + number

end-if

until number = 999

Print sumEND