Next: Desk-checking Up: Algorithms Previous: Statements required to describe

Verifying the correctness of the algorithm

Before proceeding to implement an algorithm as a program it should be checked for correctness. There are formal ways of doing this but informal methods are used here.

In considering the algorithm just developed the general case, i.e. there are numbers in the file, is checked first. After opening the file the current reading point is set at the first number. On entering the loop statement the current number is read and the reading point in the file is advanced to the next item. Ultimately the the end-of-file marker will be the next item to be read and the condition `not at end of file' becomes false and exit is made from the loop. This shows that inside the loop numbers only are read from the file and no attempt is made to treat the end-of-file marker as a number. Hence all the numbers in the file are read but nothing more. By studying the body of the loop it is seen that every time a number is read it is added to the accumulated total and the count is incremented. Hence as both these quantities start of at zero and the body of the loop is executed for each number in turn the accumulated total and number count must be correct, hence the average must be correct.

Having checked the general case any boundary conditions should then be checked. In this case the only boundary condition is the case of the file being empty. In this case the count is initialised to zero, the body of the loop is never executed hence the number count remains at zero. The condition `number count is zero' in the conditional statement is then true and the appropriate message is output.

This form of informal reasoning should be applied to check an algorithm before implementing it as a program.



Subsections

Next: Desk-checking Up: Algorithms Previous: Statements required to describe