 # Steps in Program Development

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

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

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 Data Instructions Program Output input

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

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): INPUT Number1 Number2 Number3 PROCESSING Read 3 numbers Add numbers together Print total OUTPUT Total

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.

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

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

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

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.

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.

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

Flowchart notation of WHILE
Condition true? False True code

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

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.

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’

Flowchart notation of Repeat
code False Condition true? True

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

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

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

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

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

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

Desk checking

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