Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flowcharts and Pseudocode

Similar presentations


Presentation on theme: "Flowcharts and Pseudocode"— Presentation transcript:

1 Flowcharts and Pseudocode
Summative Activity Flowcharts and Pseudocode This lecture is about how you go about planning to write a computer program.

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. A computer is simply a sequence of instructions that the computer can understand which it will obey sequentially one after the other. What is meant by sequentially?

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. This slide describes the total process of developing software. This lecture is really about the second point above. However, this slide shows you how planning fits into the overall creation of a computer program.

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 For very simple programs, like most of the ones in an elementary course you would plan the program in your head (possibly copying the style or structure from another program that you have already seen). By rights it should be done on paper using some of the techniques shown in this lecture. If you were doing a very big job, you can get software to help you do this planning. Bigger jobs can be divided into managable tasks. However, at some point we have to describe the steps that need to be followed for the computer to carry out a particular task. This is called: describing the procedure to be carried out by the computer.

5 Testing the Program Unit testing Integration testing System 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? When you have written the program it should be tested before it can be released for people to use.

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. Tis is the main part of this lecture. How do we make a plan for a computer program. Note down the definitions above, but the examples to follow will make them a bit more clear. Main point: One method is visual and the other uses special words.

7 Write address on envelope.
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 Imagine you have to give instructions to a robot to carry out the task. You break it down into small tasks that have to be done (rectangles) and any decisions that have to be made (diamonds) – did you notice that there is only one way into each diamond but more than one way out? Why do you think that is? Imagine that you are the robot and have a bunch of envelopes (already filled) and a list of names and address (some marked urgent) in front of you, and see if you can follow the instructions. Seal envelope Any more letters? Yes No End

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. must have one exit point for each possible answer. Arrows show which action is executed after the previous one. Open door Yes Is door open? No Note that the entry point for a diamond must be in a corner. Note also that it is best to have simple yes/no or true/false decisions – then you just have to have 2 exit points from the diamond. Be sure that you know the difference between an action and a decision.

9 Flowchart Assignment Design flowcharts for the following tasks!
Leaving the classroom. Knocking a set of nails into a block of wood. Making a sandwich (choice of ham or cheese) Getting dressed in the morning. Write our your flowcharts and hand them in. These are just some exercise for you to try. If you can get your hands on the out of print book by CS French in the library you will see solutions there. Here’s one without a solution: Try just doing a flowchart for a robot leaving the classroom. It should repeatedly take one step until it can reach the doorhandle. Every time it takes a step it needs to decide if it needs to take another one (This is what is called a loop). When it is finally close enough it should then decide whether to push or pull the door open, before going out. Watch the rules on the previous slide.

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! The next slide includes an example of using Top-down design. You could say that if a task is complicated, just give it a name, and leavit it to one side – you can specify the details later.

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 The action ‘open door’ may be quite complicated. In our first effort we ignore the complications. Next we have a look at the details that ‘open door’ might entail. ‘Swing door open’ requires a decision, yet in the 2nd effort we ignore the details involved in that task. Our third effort includes some of the details. 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) You should be aware that programs have variables and constants. You should also be aware that there are 2 main distinctions in data: numbers and text, and that they are stored using slightly different methods in the computers memory or storage.

13 Data Constant Variable Identifier Literal
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” Data Identifier – the name you give to a variable (or constant) If you have instructions in your program with “inverted commas” around pieces of text. These are called LITERALS as they never change and they don’t represent anything other than themselves.

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) The trouble with flowcharts is that you may end up with diagrams with lines going everywhere that can be very hard to follow (known as ‘spagetti’ programs). Pseuducode (pronounced sudocode) has strict rules to stop the program design getting so complicated that no one can follow it.

15 The Three Programming Control Structures.
All programs can be described by combining the following 3 control structures: Funnily enough it was a couple of Italian computer scientists that came up with this idea to get over the spagetti problem. They said that all programs can be broken down into the 3 structures shown here. You can either design a program with one structure after the other after the other, or even insert one of the structure inside one of the other structures ( top-down design). 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. However, because you have to be very disciplined when drawing flowcharts to make sure they don’t end up like spagetti, it is considered to be safer to use PSEUDOCODE. You can’t draw diagrams in Pseudocode. Instead you use special words (called key words) to specify the 3 layouts already shown.

17 Note the ‘indentation’
Simple Sequence Start BEGIN Walk to door Open door Move through door END Walk to door Open door This is the easy one. BEGIN and END are the keywords that indicate the start and finish of the simple sequence. Move through door Note the ‘indentation’ End

18 Note the ‘indentation’
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 This structure has 4 keywords. What are they? Notice that between keywords you are free to use ordinary language. The best technique is to write down the keywords first and then fill in the blanks later. Note the ‘indentation’

19 Note the ‘indentation’
Simple Repitition WHILE nail is sticking out hit nail ENDWHILE Hit nail Is nail sticking out? Two keywords here. This is more or less saying that ‘as long as the nail is sticking out’ keep repeating the command ‘hit nail’. If you follow the logic of the design, you first ask ‘is the nail sticking out’ , then ‘hit nail’ if it is, then ask the question again. When you finally get no as an answer to the question, you don’t hit the nail , but instead continue on with whatever instructions follow on after the REPITITION structure. 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. Try these examples. Some advice on next page.

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 Assignment
Design pseudocode for the following tasks! Leaving the classroom. Making a sandwich (choice of ham or cheese) Adding up a set of numbers. Calculating the average of a set of numbers. Finding the middle 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 Print out your pseudocode and hand it in with your flowcharts for marking. If you want some practice, try a few of these.


Download ppt "Flowcharts and Pseudocode"

Similar presentations


Ads by Google