Thursday, March 14, 2013

Detection of Program Errors

Detection of Program Errors

Detection of Program Errors 

To debug a program means to find and to correct errors in it. There are a number of methods of detecting errors.

COMPILATION ERROR MESSAGES

When a source program is compiled, syntax errors can be detected by routines, called diagnostic routines, and messages are printed out giving the type of error and where it occurs in the program.

Examples of compiler error messages

1 ERROR 25 AT LINE 200-MISMATCHED PARENTHESES

Parentheses are brackets. Line 200 probably contains different numbers of opening and closing brackets.

2 LINE l60-NEXT WITHOUT FOR ERROR

EXECUTION ERROR MESSAGES

An execution error or run-time error is an error which occurs when a computer is running a program and is unable to carry out an instruction. The computer has been asked to do something which is not possible in that situation.

Examples of execution error messages

1 File not open error

This would occur if the program tried to write data to, or read data from, a file without opening it first.

2 Division by zero at line 200

It is possible to divide by a very small number to get a very large answer, but it is not possible to divide by 0, for example, the message would arise if N has the value 1 and line 200 is

200 NEWVAL=6/1(N-l)

3 Device not present error

This would occur if the program tried to use a device such as a printer and the device was not connected up or not switched on.

Common causes of execution errors

1 Incorrect data. A well-written program should validate the data and prevent execution errors.

2 A logical error in the program can lead to the computer trying to carry out some operation which was not intended.

3 The hardware system not being set up correctly, for example. the wrong devices or too little memory, etc.

4 The user giving the wrong instructions to the operating system.

TRACES

A trace is a printout of the steps taken by all or part of a program. Usually the trace iii produced while the program is running. Common types of trace are:

1 Arithmetic trace-prints the results of each calculation as it is performed.

2 Transfer trace-shows which statements in the program are executed.

A trace is produced by a trace routine or a trace program. This adds extra instructions to the program which is being traced.

DRY RUNS

A dry run (or desk check) is a check for errors in a program by working through a flowchart or program by hand, i.e. without a computer.

A dry run is useful:

1 So that the results of running a program with test data are known beforehand.

2 To help check the logic of a program.

A dry run is often carried out by drawing a trace table. A trace table is a table which shows the instructions which are being executed and the results they produce. (For a low-level example )

Worked question

The following is a module of a BASIC program. It should input a set of lengths and print out their total and average. In fact it gives the right total and wrong average.

1 Draw a trace table for the input data 10, 14, 0.

2 State the correct output with these values.

3 Suggest a change to one line of the program to correct it.

610 PRINT "Enter lengths, one after another, in ems. Type 0 after last one"

620 I=0

630 Total=0

640 I=I+1

650 PAINT "Next length ";: INPUT length

660 IF length =0 THEN GOTO 700

670 Total = Total + Length

680 GOTO 640

700 PAINT "Total length = "; Total

710 PAINT "Average length = ": Total/I

I The trace table is as follows:

Line

I

Total

Length

Comments, results of instructions

610

 

 

 

PRINT ''Input lengths . .. " message

620

0

 

 

 

630

0

0

 

 

640

1

0

 

 

650

1

0

10

PRINT "Next length ". INPUT 10

660

1

0

10

Length is not 0.

670

1

10

10

 

680

1

10

10

GOTO 640

640

2

10

10

 

650

2

10

14

PRINT "Next length ". INPUT 14

660

2

10

14

Length is not O.

 

670

2

24

14

 

 

680

2

24

14

GOTO 640

 

640

3

24

14

 

 

650

3

24

0

PRINT "Next length ". INPUT 0

660

3

24

0

Length =0. GOTO 700

 

700

3

24

0

PRINT "Total length =24"

710

3

24

0

PRINT "Average length = 8"

2 The output should be: Total length = 24

Average length = 12

3 The average is incorrect because the division is done by 1 in line 710. In fact the number of values is 1-1. Change line 710 to

710 PAINT "Average length="; Total/(l-1)