Presentation is loading. Please wait.

Presentation is loading. Please wait.

Steps in Program Development

Similar presentations

Presentation on theme: "Steps in Program Development"— Presentation transcript:

1 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

2 Structured programming
Top-down development Modular design Structure theorem Algorithms Pseudocode

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

4 Modular design A module is Algorithms are written at module level.
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.

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

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

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

8 Stored program concept
Data area Procedural area Input Output Data Instructions Program Output input

9 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

10 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

11 Receive data Takes data in from outside the algorithm
Sample informal keywords used READ student name … GET system date ... INPUT number1, number2

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

13 Perform arithmetic with specific pieces of data, where the operands and destination are explicitly specified. An operator An operand Source and destination

14 Operations in arithmetic
+ Add - Subtract * Multiply / Divide () Precedence ** To the power of

15 Assign a value to a piece of data
Initialise TOTAL to 0 … Set COUNT to 0 COUNT = COUNT

16 Consider a problem Take three numbers into the program, add them together and output the result

17 Defining the problem Determine INPUT Determine OUTPUT
Determine processing This can be done using an IPO chart (Input, Processing, Output): INPUT Number1 Number2 Number3 PROCESSING Read 3 numbers Add numbers together Print total OUTPUT Total

18 Designing a solution algorithm
Draw a rough sketch of the steps required to solve the problem. Add-Three-Numbers Read Number1, Number2, Number3 Total = Number1 + Number2 + Number3 Print Total END The algorithm is suitably named. The end of the algorithm is marked as END. Statements between the start and end are indented.

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

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

21 Selection control structure
Simple selection Null ELSE statement Combined IF statement Nested IF statement CASE statement

22 Simple selection Null ELSE statement If StudentAttendance = Parttime
if (accountbalance < 300) - condition servicecharge = executed if condition true else servicecharge = executed if condition false Endif Null ELSE statement If StudentAttendance = Parttime Add 1 to Parttime-Count

23 Nested IF statement: If oper = ‘*’ c = a * b else
display “Illegal operator” endif Display c, ‘=‘, a, oper, b

24 Case statement: Case oper of: ‘*’: c = a * b ‘+’ : c= a + b
Other: display ‘illegal operator’ End Case display c, ‘=‘,a,operator,b

25 Part B - case statement The block is enclosed in braces
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; 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.

26 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

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

28 1. Leading edge tested loop
DOWHILE condition coding 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

29 Flowchart notation of WHILE
Condition true? False True code

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

31 IPO chart INPUT 15 entries F-temp PROCESSING Print column headings
For each entry Read F-temp Convert F-temp to C-temp Print F-temp, C-temp OUTPUT Headings F-temp C-temp

32 Fahrenheit-Celsius conversion
Print 'Degrees F' and 'Degrees C' Entry-counter = 0 DOWHILE Entry-counter < 15 Read F-temp C-temp = (F-temp - 32) * 5/9 Print F-temp, C-temp Entry-counter = Entry-counter + 1 ENDDO END Don't forget to desk-check.

33 2. Trailing edge tested loop
REPEAT coding 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 ANSWER until ANSWER = 'Y' or 'N’

34 Flowchart notation of Repeat
code False Condition true? True

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

36 IPO chart INPUT Inventory 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

37 Process-inventory-entries (pseudocode)
Set Total-Low-Stock-Items to zero Print 'Low-Stock Items' Heading REPEAT Read inventory Entry IF item number > 0 THEN IF stock figure < 20 THEN print item number,stock figure increment Total-Low-Stock-Items ENDIF ENDIF UNTIL item number = 0 Print Total-Low-Stock-Items END

38 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-value statement block ENDDO

39 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

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

41 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

42 Print-student-list 1 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 <18 THEN 6 print '*’ 7 increment Total-Students-Under-18 ENDIF ENDDO 8 Print Total-Student-Under-18 END

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

44 Desk checking

45 Comparison between WHILE and REPEAT
Add-numbers Sum = 0 Read number While number not = 999 sum = sum + number read number end-while Print sum END REPEAT Add-Numbers Sum = 0 Repeat read number if number not = 999 sum = sum + number end-if until number = 999 Print sum END

Download ppt "Steps in Program Development"

Similar presentations

Ads by Google