7.4 Using flowcharts to describe a task (contd)
Now consider what happens when you are weighing, for example, flour on a set of scales. You slowly add more flour to the scalepan until you reach the desired weight. As you do this the display constantly changes, showing the weight increasing as you add more flour. To do this, the scales’ computer must repeatedly examine the input and update the display each time it does so. The flowcharts in figures Figure 15 and 17 do not implement this. They simply take one reading and need to be re-started to take another.
The flowchart can be changed so that the input is repeatedly examined and the output repeatedly displayed by means of what is called a loop. A loop allows a certain part of a flowchart to be carried out as many times as necessary depending on the results of a decision. In this instance, after every ‘send to the display’, the flowchart in Figure 18 shows a loop back to examine the sensor again, provided the scales have not been switched off. Note that a loop has to start as a branch from a decision box. Spend a few moments examining the loop in Figure 18; make sure that you understand how this flowchart differs from the one in Figure 17 and why it would enable the scales to display an increasing weight as, for example, flour was slowly added to the scalepan.
Activity 13 (Self assessment)
Figure 12 in Section 5.4 showed five boxes that form the basis for a flowchart describing the operation of a digital camera.
You can now see that one of these boxes is in fact the wrong shape. Which box, and why?
How can Figure 12 be expanded to show that the shutter has to be pressed before the image is captured? Draw the appropriate flowchart. It should incorporate a loop, start and end boxes and also two boxes with the text:
Examine shutter input
Is shutter pressed?
The first box is the wrong shape. It refers to an input, so it should have sloping sides.
The complete flowchart is shown in Figure 19. Notice that the two boxes to test whether the shutter has been pressed must come first, before the five boxes from Figure 12. Did you remember to write Yes and No on the appropriate branches after the decision box?
Activity 14 (Self assessment)
In Section 5.3 you met the idea that the user can switch the flash facility of the camera on or off, and that if the flash facility is switched on then a light reading will have to be taken and its value be used to determine if flash is needed. Draw a flowchart to describe this. The text for the boxes of your flowchart is given below, but not in the correct order. Where in the flowchart you drew for Activity 13 would your new flowchart fit?
Is light level above threshold?
Examine flash button input
Read light level
Prepare flash for use
Is flash input on?
The flowchart is shown in Figure 20. Check that you put the boxes in the right order, that you chose the right shape for each and that you inserted Yes and No correctly after each decision box. Notice that I have started and ended this flowchart with a connector symbol. That is because it is not a self-contained program, but fits into Figure 19. Did you spot that it will fit right at the start of Figure 19, before ‘Examine shutter input? It's important to know whether flash is to be used before the user takes the picture!
Now here's an example of a task that an application program called a typing tutor needs to perform. A typing tutor checks how accurately the user types certain groups of letters at the computer keyboard. For example, the group ‘asdf might have to be typed ten times as follows ‘asdf asdf asdf …’. As the user types at the keyboard, the program checks that the correct letters have been typed in each group, using space characters to define where a group ends. When the program has counted that ten groups have been entered it displays how many correct and how many incorrect groups have been typed.
The flowchart to describe this task is rather complex, so you will work through it step by step. One helpful step would be to write down a list of actions in the same manner as you did for boiling the kettle.
Activity 15 (Exploratory)
Starting with the two actions given below, write, in order, the list of actions that need to be carried out to complete the task of the typing tutor.
Read in a group of letters from the keyboard
If the letters are asdf then count this as a good entry and …
A suitable list of actions would be:
Read in a group of letters from the keyboard
If the letters are asdf then count this as a good entry and add 1 to the total of good entries
If the letters are not asdf then count this as a bad entry and add 1 to the total of bad entries
Add 1 to the number of groups received
If 10 groups have been received then send the data about performance to the display
If 10 groups have not been received then get another group of letters
Activity 16 (Self assessment)
Use the answer to Activity 15 to help you to produce a flowchart for implementing the task of the typing tutor. (Hint: you will not be able to use exactly the words shown above in some of the boxes of your flowchart. In particular, think carefully about how you will implement the sentences beginning with ‘if in the flowchart.)
You have now seen several examples of how flowcharts can be used to describe tasks carried out by computers. Drawing such flowcharts is just one stage in the process of developing an application program. The process of developing any software starts with an analysis of the task or tasks to be performed by the computer, an analysis designed to tease out just how the computer is to behave under every possible circumstance. The software development process may continue through the drawing of flowcharts like the ones you have just met, or the software developers may prefer other means of arriving at an understanding of each individual element of the task and how these elements fit together. In either case, however, the next step will be to write the computer program. This will take the various elements of the task and convert them into a program by use of a programming language, which is a structured language with a limited set of words and symbols and which can be used to tell a computer how to perform a task. Finally, the program must be ‘debugged’ (made free of errors) through extensive testing, and also documented to facilitate any future work on it.