1 / 22

Chapter 8

Chapter 8. Designing Small Programs. 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.

kaylee
Download Presentation

Chapter 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8 Designing Small Programs

  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.

  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.

  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

  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?

  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.

  7. Begin Flowcharts Write address on envelope. Is letter urgent? Yes No • Flowchart describing the procedure involved in preparing a set of envelopes for posting. Stick first class stamp on envelope. Stick 2nd class stamp on envelope Fold letter Lick gum Seal envelope Any more letters? Yes No End

  8. Flowcharts Open door • 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. Yes No Is door open?

  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!

  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!

  11. Procedure for leaving room • Top down design/stepwise refinement Start Unlock door Walk to door Outside Are we inside or outside the room? Inside Place hand on door handle Open door Push door Pull door Turn handle Move through door Swing door open Details of ‘swing door open’ End Details of ‘Open door’

  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)

  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”

  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)

  15. The Three Programming Control Structures. • All programs can be described by combining the following 3 control structures: Simple Selection (IF-THEN-ELSE) Simple Repitition (Loop) Simple Sequence

  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.

  17. Simple Sequence Start BEGIN Walk to door Open door Move through door END Walk to door Open door Move through door Note the ‘indentation’ End

  18. Simple Selection IF inside room THEN Pull door ELSE Push door ENDIF Outside Are we inside or outside the room? Inside Push door Pull door 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) • 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.

  21. Hints for writing pseudocode(2) • IF .............. THEN • ............... • ELSE • ........... • .............. • ENDIF • WHILE................. • ...... • ............. • .... • ........ • ENDWHILE • BEGIN • ................ • ................ • ........... • ......................... • END • 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.

  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!

More Related