1 chapter 8 designing small programs. 2 a ‘procedure’ v a set of instructions which describe the...

22
Chapter 8 Designing Small Programs

Post on 21-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

1

Chapter 8

Designing Small Programs

Page 2: 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

2

A ‘Procedure’

A set of instructions which describe the steps to be followed in order to carry out an activity.

This can be for any activity:– eg recipe, assembly instructions etc

If it is written in a computer language it is called a program.

Page 3: 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

3

To solve a problem by writing a computer program!

Make sure you understand the problem!– be clear in your mind what you are trying to achieve.

Plan the solution!– work out how the task should be done on the computer.

Implement the solution!– write the computer program.

Test the solution!– make sure the program does what it is supposed to do.

Page 4: 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

4

Planning the Solution The solution must be prepared before any

programming starts.– on paper– using computerised tools– in your head

Divide problem into separate tasks if necessary.– called ‘modules’ or ‘components’

These tasks may also be divided. Tasks must be precisely described.

– the ‘procedure’ must be described

Page 5: 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

5

Testing the Program

Unit testing– testing modules separately

Integration testing– do the modules work together?

System testing– testing on the computer it will be used on.

User acceptance testing– are they happy with the program?

Page 6: 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

6

Describing Procedures Precisely 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.

Pseudocode– Words that describe the individual tasks

and special ‘keywords’ that describe the decisions to be made in regard to carrying out these tasks.

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

7

Flowcharts

Flowchart describing the procedure involved in preparing a set of envelopes for posting.

Write address on envelope.

Isletter

urgent?

Begin

Stick first class stamp on envelope.

Stick 2ndclass stampon envelope

Fold letter

Lick gum

Seal envelope

Anymore letters?

End

Yes No

Yes

No

Page 8: 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

8

Flowcharts Rectangle represents actions.

– can only have one entry point.– can only have one exit point.

Diamond represents a decision.– phrased like a question.– can only have one entry point.– must have one exit point for each possible

answer. Arrows show which action is

executed after the previous one.

Opendoor

Isdoor open?

Yes No

Page 9: 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

9

Flowchart Exercises

Design flowcharts for the following tasks!– Leaving the classroom.

imagine you are writing instructions for a robot!

– Knocking a set of nails into a block of wood. see French 8.20 You don’t know how long each nail will take.

– Making a sandwidge (choice of ham or cheese) look at answer in Appendix II

– Making sandwidges from a loaf. Appendix II

– Keep to the Rules!

Page 10: 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

10

Top Down Programming with Stepwise Refinement

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.

Top-Down Design is used in all areas of design!

Page 11: 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

11

Procedure for leaving room

Start

Walkto

door

Opendoor

Movethrough

door

End

Top down design/stepwise refinement

Place handon door handle

Turn handle

Swingdooropen

Are we inside or

outside theroom?

Pushdoor

Pulldoor

Outside Inside

Details of ‘Open door’Details of ‘swing door open’

Unlock door

Page 12: 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

12

Data Identify Data Items

– data items are given names (identifiers)– classified as either:

constant– value does not change during the operation of the program.

variable– value may change during operation of program.

– data type numeric (real, fixed, floating, integer) text (alphanumeric) other (date, etc)

Page 13: 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

13

Data

Constant– a piece of data that does not change

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.

Identifier– the name by which the piece of data is known ie. what the variable or

constant is called.

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”

Page 14: 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

14

Structured Programming Modularise your program.

– use top-down design methods.– write the program as separate units (subroutines)

Use standard pre-built modules if available.– don’t ‘re-invent the wheel’– eg ‘Windows’ programming libraries.

Use the 3 basic programming ‘control structures’.

Use Pseudocode (not flowcharts)

Page 15: 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

15

The Three Programming Control Structures. All programs can be described by combining

the following 3 control structures:

Simple Sequence

Simple Selection(IF-THEN-ELSE) Simple Repitition

(Loop)

Page 16: 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

16

Pseudocode

Words that describe the individual tasks and special ‘keywords’ that describe the decisions to be made in regard to carrying out these tasks.

Keywords for the 3 basic control structures. Forces the programmer to stick to these

structures. Looks like a programming language but is

NOT. It is a strict form of english.

Page 17: 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

17

Simple Sequence

BEGINWalk to doorOpen doorMove through door

END

Start

Walkto

door

Opendoor

Movethrough

door

End

Note the ‘indentation’

Page 18: 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

18

Simple Selection

IF inside room THENPull door

ELSEPush door

ENDIF

Are we inside or

outside theroom?

Pushdoor

Pulldoor

Outside Inside

Note the ‘indentation’

Page 19: 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

19

Simple Repitition

WHILE nail is sticking outhit nail

ENDWHILE

Hitnail

Isnail

stickingout?

yes

noNote the ‘indentation’

Page 20: 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

20

Hints for writing pseudocode(1) Identify the appropriate structure for the task to being

described.– sequence, selection or repitition

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.

Page 21: 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

21

Hints for writing pseudocode(2)

Write down all the keywords for that structure and fill in the gaps.

The gaps can be filled with ordinary english statements.

The statements can represent complicated procedures which can be described in detail later.

BEGIN....................................................................

END

IF .............. THEN...............

ELSE.........................

ENDIF

WHILE................................................

ENDWHILE

Page 22: 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

22

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

<40 is a fail <60 is a pass <70 is a merit >=70 is a distinction

– Keep to the Rules!