1 Chapter 8 Designing Small Programs
2 A ‘Procedure’ v A set of instructions which describe the steps to be followed in order to carry out an activity. v This can be for any activity: –eg recipe, assembly instructions etc v If it is written in a computer language it is called a program.
3 To solve a problem by writing a computer program! v Make sure you understand the problem! –be clear in your mind what you are trying to achieve. v Plan the solution! –work out how the task should be done on the computer. v Implement the solution! –write the computer program. v Test the solution! –make sure the program does what it is supposed to do.
4 Planning the Solution v The solution must be prepared before any programming starts. –on paper –using computerised tools –in your head v Divide problem into separate tasks if necessary. –called ‘modules’ or ‘components’ v These tasks may also be divided. v Tasks must be precisely described. –the ‘ procedure ’ must be described
5 Testing the Program v Unit testing –testing modules separately v Integration testing –do the modules work together? v System testing –testing on the computer it will be used on. v User acceptance testing –are they happy with the program?
6 Describing Procedures Precisely v Flowcharts –Diagrams that include words which describe the individual tasks to be carried out and the decisions to be made in regard to carrying out tasks. v Pseudocode –Words that describe the individual tasks and special ‘keywords’ that describe the decisions to be made in regard to carrying out these tasks.
7 Flowcharts v Flowchart describing the procedure involved in preparing a set of envelopes for posting. Write address on envelope. Is letter urgent? Begin Stick first class stamp on envelope. Stick 2nd class stamp on envelope Fold letter Lick gum Seal envelope Any more letters? End Yes No Yes No
8 Flowcharts v Rectangle represents actions. –can only have one entry point. –can only have one exit point. v Diamond represents a decision. –phrased like a question. –can only have one entry point. –must have one exit point for each possible answer. v Arrows show which action is executed after the previous one. Open door Is door open? YesNo
9 Flowchart Exercises v Design flowcharts for the following tasks! –Leaving the classroom. u imagine you are writing instructions for a robot! –Knocking a set of nails into a block of wood. u see French 8.20 u You don’t know how long each nail will take. –Making a sandwidge (choice of ham or cheese) u look at answer in Appendix II –Making sandwidges from a loaf. u Appendix II –Keep to the Rules!
10 Top Down Programming with Stepwise Refinement v Designing a computer program by specifying as little of the details of how the job will be done at the early stages, but gradually defining tasks in more detail at later stages. –Leave details as long as possible! –Divide a job into it’s major components. –Take each component and specify the major tasks involved. –Break each task down into further tasks. –This ‘refinement’ can be done as often as required. v Top-Down Design is used in all areas of design!
11 Procedure for leaving room Start Walk to door Open door Move through door End v Top down design/stepwise refinement Place hand on door handle Turn handle Swing door open Are we inside or outside the room? Push door Pull door OutsideInside Details of ‘Open door’ Details of ‘swing door open’ Unlock door
12 Data v Identify Data Items –data items are given names (identifiers) –classified as either: u constant –value does not change during the operation of the program. u variable –value may change during operation of program. –data type u numeric (real, fixed, floating, integer) u text (alphanumeric) u other (date, etc)
13 Data v Constant –a piece of data that does not change v Variable –a piece of data that may change –in BASIC or JavaScript programs variables may be used to store constants in which case it is up to the programmer to make sure it does not change during the operation of the program. v Identifier –the name by which the piece of data is known ie. what the variable or constant is called. v Literal –a piece of text that is fixed in a program. –not necessarily given a name. –usually has ““ around it to differentiate it from an identifier (variable name) eg cost “cost”
14 Structured Programming v Modularise your program. –use top-down design methods. –write the program as separate units (subroutines) v Use standard pre-built modules if available. –don’t ‘re-invent the wheel’ –eg ‘Windows’ programming libraries. v Use the 3 basic programming ‘control structures’. v Use Pseudocode (not flowcharts)
15 The Three Programming Control Structures. v All programs can be described by combining the following 3 control structures: Simple Sequence Simple Selection (IF-THEN-ELSE) Simple Repitition (Loop)
16 Pseudocode v Words that describe the individual tasks and special ‘keywords’ that describe the decisions to be made in regard to carrying out these tasks. v Keywords for the 3 basic control structures. v Forces the programmer to stick to these structures. v Looks like a programming language but is NOT. It is a strict form of english.
17 Simple Sequence BEGIN Walk to door Open door Move through door END Start Walk to door Open door Move through door End Note the ‘indentation’
18 Simple Selection IF inside room THEN Pull door ELSE Push door ENDIF Are we inside or outside the room? Push door Pull door OutsideInside Note the ‘indentation’
19 Simple Repitition WHILE nail is sticking out hit nail ENDWHILE Hit nail Is nail sticking out? yes no Note the ‘indentation’
20 Hints for writing pseudocode(1) v Identify the appropriate structure for the task to being described. –sequence, selection or repitition v Try these: –Adding up a large set of numbers. –putting ham or cheese onto a slice of bread. –printing out ‘pass’ or ‘fail’ for a student. –addressing and stamping an envelope. – printing out results for a class of students.
21 Hints for writing pseudocode(2) v Write down all the keywords for that structure and fill in the gaps. v The gaps can be filled with ordinary english statements. v The statements can represent complicated procedures which can be described in detail later. BEGIN END IF THEN ELSE ENDIF WHILE ENDWHILE
22 Pseudocode Exercises v Design pseudocode for the following tasks! –Leaving the classroom. –Knocking a set of nails into a block of wood. –Making a sandwidge (choice of ham or cheese) –Making sandwidges from a loaf. –Adding up a set of numbers. –Calculating the average of a set of numbers. –Finding the largest of a set of numbers. –Finding the middle on of 3 numbers. –Printing out results for a class of students. u <40 is a fail u <60 is a pass u <70 is a merit u >=70 is a distinction –Keep to the Rules!