Software Design and Development

Home > Software Design and Development > Core > Software Development Cycle > Testing an algorithm

Testing an algorithm

When an algorithm is created it needs to be tested and checked to see if it works. There are two common methods used to test an algorithm:

It is best to get someone else to check your algorithm, this is called peer checking. (If you wrote the algorithm you are less likely to spot the mistakes).

Desk-checking
Involves drawing up a table of the variables and outputs from the algorithm and then methodically working through the algorithm, with sets of test data, recording the changing values of the variables. The final results can then be compared to the expected results to see if the algorithm works as it should.

Example
The following algorithm is meant to calculate the average of 5 numbers. So for the test data, of 5, 4, 6, 2, 8 the average should be (25/5 =  5)

BEGIN

Count = 0

Total = 0

Average = 0

REPEAT

Count = count + 1

Get number

Total = Total + number

UNTIL count = 5

Average = Total/Count

Display average

END

Count Total Number Average Output
0 0   0  
1 5 5    
2 9 4    
3 15 6    
4 17 2    
5 25 8 5 5

Activity 1:
The following algorithm is meant to work out the pay for an employee, given the payrate and number of hours they have worked each day.

BEGIN

count = 0

Totalhours = 0

Enter Payrate

WHILE count < 7

count=count + 1

Enter hours worked

Totalhours = Totalhours + hours

END WHILE

Pay = payrate * Totalhours

Display Pay

END

Complete a desk check, using the following test data:

Variables Outputs
Count Hours Totalhours Payrate Pay  
           
           
           
           
           
           
           
           
           

Check your Answer

Walk-through
A walk-through is used to quickly test the logic of an algorithm or to find out what an algorithm does.  It is usually done mentally and this is what a peer-checker might do to check the logic of the algorithm.

Activity 2:
Outline examples of errors you might expect to find using a Walk-through.

Check your Answer

Go To Top



Neals logo | Copyright | Disclaimer | Contact Us | Help