Professional Documents
Culture Documents
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.
End
The example shows the individual instructions in nice neat little boxes, this is certainly
not necessary, and also uses the normal rule of reading down a page. However, providing
the arrows are in the correct order then the instructions can be placed anywhere. This type
of representation of the algorithm is known as a flow diagram.
There are many other ways of defining the solutions to problems, some of which you will
meet later in this course.
1.3. (b) Techniques for Writing Software.
If we imagine the same problem, and start with a control module, then realise that we
have to provide an input module, and so on… the problem will still be solved, although
the approach is the opposite to the one that we have just considered. This type of
approach is known as a bottom up approach. Although the outcome is the same it is a far
more dangerous way to approach the problem because the final outcome has not been
considered and may not be arrived at by the most efficient algorithm. Because what we
are doing is really to choose a library program we have already written and then add other
bits of program to it in order to arrive at a solution, it is called ‘bottom up programming’
as opposed to ‘top down design’.
1.3. (c) Program Translation
When a human being writes a computer program it is important for them that they can
use a language which is easy to understand. After all, the algorithm will probably be
difficult enough to follow without the language being difficult as well. Computer
languages like this, that the person finds easy to understand, but the computer can’t, are
called high-level languages.
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.
High-level Machine
Language Translator code
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).
Source Object
code Translator code
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. We do not need to know any more about these two translators yet.
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
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
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. In section 1.3.b we discussed the splitting up of a problem into smaller and smaller
parts, until each part was a manageable size. When the parts were combined they would
produce a solution to the original problem. Because we are starting with a big problem
and splitting it into smaller problems, this was called top-down design. 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
Program writers need to be aware that the programs that they write will need to be
maintained long after they have gone on to other projects. For example, a program is
written to control the stock in a warehouse. The program has been fully tested and any
problems appear to have been solved. Some months later, a particular chain of events
occurs that means that the software does some instructions in an order that has never
arisen before, and an error occurs. Someone has to find the error in the program. Some
time later, the warehouse expands in order to deal with an increased supply of goods and
the computer system needs to be amended to allow enquiries from the Internet. This
again implies that the program will need to be altered to accommodate the new
circumstances. In both these cases it is highly unlikely that the original programmer will
be the person that has to do the adapting of the code.
The new programmer is going to be grateful for all the help they can get in understanding
the old program. Help given in the code itself is known as annotation, the purpose of
which is to make the code as simple as possible to follow.
One way to help demonstrate the structure of the program is to make parts of the
code stand out from the rest by indentation. If this is done without some thought
then it is meaningless, but if a procedure of a program is made to stand out in this
way then it is very easy to see whereabouts the procedure is.
If there is a loop structure, perhaps a While…Endwhile loop inside the
procedure, then that can also be made to stand out by using a further
indent.
Many programming languages automatically indent some of the programming
constructs available in the language.
Another way is to write comments in the code of the program which are aimed at a
human being looking at the program, rather than at the computer that is running it. Most
programming languages have an instruction that can be used for this. It simply tells the
computer that anything that comes in this instruction is to be ignored.
A further technique is to use sensible variable names. If an input is required at some point
in the program, and the input is the number of marks that the student obtained in their
maths exam, the variable used to store the number could be many things. However, if the
variable name X was used, another programmer looking at the code would need extra
information to understand what X stood for. If the original programmer had used the
variable name MATHS_MARK, the name is self-explanatory and hence makes the
program easier to follow.
The other obvious way of making the program easier to follow is to adopt a top-down
design which enables the individual procedures to be easier to understand than one large
program.
1.3 (i) Reading pseudocode
As we saw in the first section of this module, the solution to a problem can often be
thought of as a sequence of steps which should be taken in order. The same is true when a
program is being written. Generally, you start at the beginning and follow the steps, in
order, until you come to the end, by which time the problem should have been solved.
Looked at diagrammatically, a computer program could be drawn as
Start
Program instructions
End
This type of combination of operations is known as a sequence. However, the real power
of computer programs becomes apparent when we change these rules. There are a
number of ways of breaking out of the simple sequence, we will look at two of them here.
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
Start Start
This example simply gives two options, the condition statement can have a number of
possible results, each giving a different route.
Note that, eventually, all these different routes must either come back together or must
have some suitable end point.
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. Each one of these methods has two things in common.
The first is some sort of pointer that sends control of an algorithm to some point other
than the next instruction in the sequence, and the second is some method of controlling
whether the pointer is followed or whether the normal sequence of instructions is
followed.
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.
Add 1 to
the counter (LOOP)
Note that with the For/Next loop the number of times around the loop is determined
before entry into the loop.
As we saw in section 1.3.b, it is useful to be able to divide a problem into modules so that
each part of the problem is easier to solve. When the algorithm is turned into a computer
program it makes sense to program each of these smaller modules separately and then
connect them together to make the whole program. These small segments of program are
called procedures and functions. To start with we will call them all procedures, they are
very similar anyway, and later we’ll see what the difference is.
Imagine a computer program. It is made up of a number of instructions taken in order. In
this section we have seen some constructs that could change the simple order, but for the
sake of clarity we will ignore them and return to our original diagram of a program:
Start
Program instructions
End
If the program has been split into sections by modularising the algorithm, it is more likely
to look like this:
Start
Start of Module 1
Start of module 2
When a question is asked about using these techniques in pseudocode it will give you a
short section of pseudocode which you will be expected to dry run and then possibly be
expected to change some of the lines so that it will produce a different result. You will
not be expected to write a lot of pseudocode in an exam. However, you may be asked to
describe an algorithm…
1.3 (j) Algorithm production
Remember that all an algorithm is is a way of writing down the solution to a problem. Do
not get worried about an algorithm question.
The first thing to remember is that it does not have to be in pseudocode, it is just as good
to write your answer in any way that you like. A number were mentioned in section
1.3.a. You don’t even have to write the answer down, draw it in the form of a flow
diagram if you prefer. The examiner is interested in you getting the right steps that need
to be carried out and putting them in the right order.
When you get a question which asks you to produce an algorithm it must describe the
thing that needs to be done otherwise you cannot know what to do. Make sure you read
through the question in the correct order and, as you read it, jot down all the things that
need to be done, using a different line for each thing. When you have finished reading
through the question, your jottings will almost be an algorithm, then all you have to do is
tidy it up. One thing to remember is that there is nearly always a loop in the answer
somewhere, even if it is simply at the end so that the algorithm goes back to the start and
does it again. There are always marks for a loop, for putting the start and end in the right
place and for having a condition that will let you get out of the loop. Typical would be:
Do the following instructions
………..
………..
………..
Until the switch is turned off
If we assume that the beginning and end are in the right place, the instructions above
would normally be worth 3 marks (Loop, beginning and end in right place, condition to
get out of the loop)
Don’t be scared by an algorithm question, even writing down obvious things will get
marks, just write down as much as you can.
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)
2. Set the counter to 0 and the total to 0
Read a number
Yes
Are there any more numbers?
No
Divide the Total by the counter
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)