Planning
and writing algorithms
Throughout this course you will be
required, in both labs and assessments, to write algorithms to solve a
particular problem. When planning and writing the algorithm it is useful to
consider the following points:
General
algorithm design and structure
In general, before writing an
algorithm, it is useful to spend a few minutes thinking about the problem and
how you would go about solving it without a programming language. One
question you can ask yourself is if I had to solve this problem with a pen,
paper and pocket calculator how would I go about doing it? Some students
find it useful to sketch a simple flow diagram during this process.
In all cases you should be clear
about the exact sequence of commands that the algorithm has to perform. It must
be possible to follow an algorithm step-by-step. The instructions must be given
in the correct order and there must be some way of telling when to finish one
instruction and begin another. This is particulary important for programs that
involve repeating a process several times (see Session 3 on
looping). Similarly, your algorithm must have a definite stop
condition. Often this is a trivial thing like outputting a calculated value.
However, some care has to be given to ensure that a program will stop.
Writing
and checking the algorithm
When writing your algorithm it is a
very good idea to compile the source code regularly, after each few lines of
code have been added. In this way you are able to spot any compilation errors
immediately. Another reason why this is good practise is that it forces your
code to be saved regularly so that if your computer crashes you do not lose hours
worth of editing.
As you develop the algorithm you may
want to add simple printf statements to write out values along the way
to check that your code is doing what you expect it to.
Other things to bear in mind whilst
developing your algorithm include
- Think carefully about the type of each variable, in
particular, don't use double or float for variables that can
only have integer values - you may run into rounding error problems. You
should never use real numbers as for loop indices.
- Think carefully about initialisation of
variables. If you start adding numbers to an uninitialised variable you
may well get a meaningless value. In lots of cases it makes sense to
initialise to 0 but this is not always the case (consider, e.g. how you
initialised for Exercise 3.2 which
calculated a factorial).
- Where appropriate, any data read in or written out must
be checked if it makes sense. A good example of this is in the Day of week
exercise which is part of this topic. You are asked to enter a date in the
month and a month in the year. Clearly entering 55 then 9 for the 55th of
September makes no sense. Your algorithm should always check for
this type of error. If such checks are missing you will lose marks in an
assessment.
Testing the final algorithm
Once you have finished writing your
algorithm and you are confident it is working correctly you will want to check
it is working OK. Sometimes an assessment may give you some example values to
check. Otherwise, you can always modify your code to check values that you
know.