Chapter 1.3 Programming Tools and Techniques.
1.3. (a) Structure
The solving of problems using computing techniques relies heavily on being able to divide the problem into a series of steps that should be solved in a sequence in order to solve the entire problem.
The sequence of steps that need to be carried out in order to solve a problem, which takes into account the order of the steps, is called an algorithm.
Eg algorithm for calculating perimeter of rectangle is:
1. Find the length and breadth
2. Add the length and the breadth together
3. Multiply the result by 2
4. End
3. Another way of showing this order is to use arrows pointing from one instruction to the next
This type of representation of the algorithm is known as a flow diagram.
1.3. (b) Techniques for Writing Software.
When a piece of software needs to be produced, the problem to be solved is likely to be far more complex than the calculation of the perimeter of a rectangle. An algorithm like that is easy for a human being to be able think about because it is so short, however, most useful problems involve such complex algorithms that it is not possible to consider them in one go. For this reason problems are normally divided up into a number of smaller problems, each one of which can be solved separately, and then combined to give a solution to the whole problem.
Consider that you have been asked to produce a solution to the problem of computerising a set of traffic lights in the centre of a town. The problem is a very complex one, but if it is divided into:
· How is the data going to be collected from sensors and stored in the system?
· How is the decision going to be made about the changing of the lights?
· How is the data collected from the pedestrian crossing and then stored..
· and processed?
· What outputs are necessary, and how are they controlled?
Then each problem becomes more manageable.
This sort of approach to problem solving is called a top-down approach, in that we started with an original problem (the top) and split the problem into smaller and smaller parts until they became manageable. When each of the individual problems has been solved the solutions are combined to produce a solution to the whole problem.
These individual parts of the solution are known as modules.
There are other advantages in this sort of approach.
· More than one person can be engaged on solving parts of the same problem simultaneously, consequently the problem should be solved more quickly.
· Different people are good at different things, so our company uses Aisha to solve the problem of collecting the information from the sensors, because Leon is not very good at that, whereas he is an expert concerning controlling things using a computer, so he does the processing module.
· Last month, our company produced a similar solution for a set of traffic lights in a different town. The sensors used were different, so Aisha still has to produce her module, and the number of roads is different so Leon will still need to come up with a solution for his module. However, the lights and other output devices being controlled are the same, so we might as well use the same module that we used last time. This happens often, that one of the modules is the same as one used previously. Because of this, software firms store the modules that they have produced so that they can be used again if the chance arises. Collections of modules like this are known as software libraries.
· Because the algorithms for the modules are so much shorter than for the whole problem, it is likely that the person producing a module will make fewer mistakes. Also, because the module is quite short, it is much easier to spot any errors that have been made, and correct them. This means that the finished software should be more reliable when it is being used.
1.3. (c) Program Translation
A computer can only understand 0s and 1s, in other words, binary numbers. So the language which the computer uses is all in binary and very difficult for human beings to use. Languages like this are called machine code languages. If the program is written in a high level language and the computer can only run it in machine code, then it has to be translated from one to the other, just as a letter in Chinese may need to be translated into English if an English speaker is to be able to read it.
A program which is written in a high level language is called the source code. After the source code has been translated the program is called the object code (or executable form).
The object code is normally much bigger than the source code because it takes lots of machine code commands for each of the high level language commands.
There are two types of translator, one is called a compiler and the other is called an interpreter.
1.3. (d) Types of Programming Errors
When programs are being written it is not surprising that mistakes are made, after all they are very complicated. There are three types of error which can be made when program code is being produced.
1. Syntax errors. These are errors in the use of the language. The rules have been broken. A command word might be PRINT. If it was typed in as PLINT the translator program would not recognise it and so would not be able to translate it. This is not a difficult error to spot because when the programmer tells the translator to translate it, a message will be returned saying that a mistake has been made and telling the programmer which word it cannot recognise. There are other ways of breaking the rules, for instance a language might be set up to only accept a mathematical formula if it is in the form of a variable followed by an equals sign and then a collection of variables. So X = 3*(2+A) would be accepted by the translator, but 3*(2+A) = X would be rejected as an error even though, mathematically, it is identical.
2. Logic errors. A logic error is a mistake in the way the program solution has been designed. For example, an instruction in a program may tell the computer to jump to another part of the program. This is fine as long as the instruction tells the computer to go to the right part of the program. If it tells the computer to go to the wrong place, it is highly unlikely that the program will produce the results that were expected. Unfortunately, such a mistake does not usually make the program stop working, it just makes it produce wrong results. This makes it difficult to spot that a mistake has been made, and even when the programmer realises that something has gone wrong, finding where the error is can be very difficult. Another example of a typical logic error is when an incorrect formula is used e.g. Total_number = Previous_total – New_number. There is nothing obviously wrong with the formula so it will work, but it won’t produce the right answer because it should have been +. Notice that this is not an arithmetic error, the computer will do the arithmetic perfectly, it is the logic that is wrong.
3. Arithmetic error. This is sometimes thought of as a type of logic error but is rather different because the computer is asked to do something that is impossible. A good example is when a computer is asked to divide by 0, this is impossible and would cause the program to fail.
1.3. (e) Testing Methods
When a system is designed it is important that some consideration is given to making sure that no mistakes have been made. A schedule should be drawn up which contains a test for every type of input that could be made and methods of testing that the program actually does what it was meant to do. This schedule is known as the test plan. Note that it is produced before the system is produced.
There are a number of ways of testing a program.
1. Different values can be input for variables to determine whether the program can cope with them. These values should include typical values, borderline values and values which are not acceptable. For example, if a program is written which uses marks out of 100 from a maths examination as input, the test data would include typical data like 27, 73.., borderline data which would be 0 and 100, and unacceptable data like –34, 123, 16.345 This type of testing is called black box testing.
2. White box testing is testing the program to determine whether all the possible paths through the program produce the desired results.
Think of black box as a test where you cannot see into the box (program) all you see is what comes out at the end. White box testing means that you are able to see what is happening as the data goes through the box because it is transparent.
3. Alpha and Beta testing. When you have written a program and you sit down to test it, you have a certain advantage because you know what to expect. After all, you wrote the program. This can be extended to the whole software company, as the employees are all computer-minded people. Testing carried out by people like this is known as alpha testing. Eventually, the company will want ordinary users to test the program because they are likely to find errors that the software specialists did not find. Testing carried out by the users of the program is called beta testing.
1.3. (f) Selection of test data
If a solution is to be tested, someone has to choose what data is going to be used to do the testing. The test data is usually chosen by the programmer because they know what they want to test. It is important to test as many different things as possible, the important word there being ‘different’. The problem for the programmer is that they know what inputs were expected so they find it very difficult to think of the sort of inputs that the user of the program might try to put in.
When you are asked to think up different inputs to test a program, it must be different types of input, not just changing numbers. Imagine a question that states that a program has been written that will work out the mean of three numbers. You have to come up with different test data and the reasons for doing those tests. That last bit is in bold because that is what you get the marks for and the reasons for the tests are the things that have to be different. In this example you would me thinking of
1, 2, 3 to test whether integers will give an integer answer
1, 2, 4 to test whether the software can cope with a recurring decimal answer
(Note that “1, 2, 4 to test a different set of integers” would not get a mark because the reason for the test is not different)
1, 2.5, 3 to test whether the program can use decimal inputs
1, 2½, 3 to test whether fractions are allowed
-1, -2, -3 to test whether negative numbers can be handled
1, 2 to test what happens when only two values are input
There are many more that would be acceptable. The important thing to notice is that the numbers themselves are almost identical but that the reasons for choosing them are very different.
1.3 (g) Debugging
Errors in computer solutions are called bugs. They create two problems. One is that the error needs to be corrected, this is normally fairly straightforward because most errors are caused by silly mistakes. The second problem, however, is much more complicated, the errors have to be found before they can be corrected. Finding where the error is and identifying it, can be very difficult and there are a number of techniques available for solving such problems.
1. Translator diagnostics. In section 1.3.c we were introduced to the idea of a translator program. Each of the commands that are in the original program is looked at separately by the translator. Each command will have a special word which says what sort of command it is. The translator looks at the special word in the command and then goes to its dictionary to look it up. The dictionary tells the translator program what the rules are for that particular special word. If the word has been typed in wrongly, the translator will not be able to find it in the dictionary and will know that something is wrong. If the word is there, but the rules governing how it should be used have not been followed properly, the translator will know that there is something wrong. Either way, the translator program knows that a mistake has been made, it knows where the mistake is and, often, it also knows what mistake has been made. A message detailing all this can be sent to the programmer to give hints as to what to do. These messages are called translator diagnostics.
2. Sometimes the program looks alright to the translator, but it still doesn’t work properly. Debugging tools are part of the software which help the user to identify where the errors are. The techniques available include:
a) Cross-referencing. This software checks the program that has been written and finds places where particular variables have been used. This lets the programmer check to make sure that the same variable has not been used twice for different things.
b) Traces. A trace is where the program is run and the values of all the relevant variables are printed out, as are the individual instructions, as each instruction is executed. In this way, the values can be checked to see where they suddenly change or take on an unexpected value.
c) Variable dumps. At specified parts of the program, the values of all the variables are displayed to enable the user to compare them with the expected results.
3. Desk checking is sometimes known as a dry run. The user works through the program instructions manually, keeping track of the values of the variables. Most computer programs require a very large number of instructions to be carried out, so it is usual to only dry run small segments of code that the programmer suspects of harbouring an error.
4. When the program is written, each small program is written separately, allowing the small programs to be tested thoroughly before being combined. This is called bottom-up programming. The technique is particularly useful for testing the finished program because it is far easier to test a lot of small programs than it is to test one large one. One problem can arise, because the small programs have to be joined together, these joints have to be tested too to make sure that no silly mistakes have been made like using the same variable name for two different things in two parts of the program (tested by cross referencing).
5. Test strategies are important to establish before the start of testing to ensure that all the elements of a solution are tested, and that unnecessary duplication of tests is avoided.
1.3 (h) Annotation
In order to make a program more readable and understandable, the following annotation techniques can be used.
Ø Use of comments
Ø Use of sensible variable names
Ø Indentation
1.3 (i) Reading pseudocode
1. Selection
Selection is the process of making a choice. The simplest example is the command If…Then…Else… This is used to divide the sequence into two possible routes. There is a condition (something like an answer being more than 10) and then two possible outcomes, one is followed if the condition is met (for instance, use a discount price because of the number bought), and the other is followed if the condition is not met (use the standard price).
Diagrammatically, this can be represented as
2. Repetition
There are a number of ways of repeating the same instructions more than once. We will have a look at three methods here.
A repeating construct is often called a loop.
a) The first of these loops is one where the control of the loop is at the end of the loop. It is called a REPEAT…UNTIL loop.
Note that the instructions in the loop have to be carried out at least once before there is the chance of meeting the condition.
b) The second loop structure is a WHILE…ENDWHILE loop. This structure has the condition at the start so that it is possible to miss out the instructions in the loop entirely.
c) The third type of loop is called a FOR…NEXT loop. This loop is repeated a predetermined number of times. A counter keeps check of the number of times the loop has been done, and when it has done enough the loop is exited.
NOTE: A procedure is a piece of code that does not return a value whereas a function is a piece of code that returns a value.
Example Questions.
1. A roll of wallpaper is 50cm wide. Each roll is 15m long. A room measures x metres long by y metres wide and 2.5 metres high. Ignoring the door and any windows that there may be, describe an algorithm for calculating the number of rolls of wallpaper needed to paper the room. (4)
Describe the effect of this algorithm. (4)
3. Give three advantages to using a top down approach when designing a solution to a problem. (3)
4. A piece of software is to be written which will control a central heating system. It is decided that the software is to be written as a set of four modules. State four modules, whose combined solution would be a solution for the whole problem.
5. X = 1
WHILE X < 9 DO
X = X + 1
Y = X * X
PRINT Y
ENDWHILE
The intention of this module is to print out the square numbers from 1 to 100.
a) Write down the output that the module will produce. (2)
b) Using the same structure, produce a similar module that will work. (2)
.
6. REPEAT
INPUT NUMBER
IF NUMBER <> 0 THEN TOTAL = TOTAL + NUMBER
COUNT = COUNT + 1
ELSE ZERO = ZERO + 1
UNTIL COUNT = 5
ANSWER = TOTAL/COUNT
PRINT ANSWER, ZERO
a) If the numbers 3,5,4,0,4,0,9,1 are used as data for this procedure, state what the printout will be when the procedure is completed. (2)
b) Identify the condition statement, and explain what it does. (3)
7. State what is meant by the terms
(i) Source code
(ii) Object code
(iii) Machine code (4)
8. Describe the relationship between the source code and the object code.
(2)
9. State three types of program error and give an example of each. (6)
10. A program is written that will allow the user to input a list of 5 numbers and will print out the largest and the smallest of them.
The program is to be tested to see that it works under all conditions. The programmer decides to use the test data 3,4,2,5,6,7 in order to test what will happen if more than 5 numbers are input.
Give four other different sets of test data explaining the purpose of each. (8)
11. Explain the difference between alpha testing and beta testing. (2)
12. Explain how the translator program can issue error diagnostics and give an example of an error that can be spotted by the translator. (3)
13. Bugs are common in computer programs. Describe two techniques that can be used to help debug a program. (4)
14.a)Describe what is meant by annotation of a program code. (2)
b) Describe two methods that can be used to annotate code. (4)
0 comments:
Post a Comment