Algorithms and Tracetables Types of Program Errors As

Algorithms and Tracetables

Types of Program Errors As programs are written by humans, they may contain errors. There are several different types of error. A SYNTAX ERROR is where a program statement doesn’t obey the rules of the programming language. A program cannot be translated if it contains syntax errors. A LOGIC ERROR is where the program doesn’t do what the programmer wanted it to do. Logic errors are found when a program is being run. (-and through testing, such errors can be hidden and only found in odd circumstances making then dangerous. As such robust testing is always needed to make sure programs are safe. )

Handling Syntax Errors When a program is being compiled, if any syntax errors are found no translated program is produced. Instead, a list of all the errors in the whole program is produced. The programmer corrects these errors and recompiles the program. When a program is being interpreted, the interpreter performs the actions specified by each statement until a syntax error is found. The programmer is then alerted to the place in the program where the error was found. The error is corrected by the programmer and the interpretation continues until the next error is found or the task is completed.

Handling Logic Errors and Trace Tables When a program is being run, if it doesn’t do what it should do having passed there is a logic error. These can be found by tracing what the program does and using test data with expected results. A TRACE TABLE can be used to record the results from each step in an algorithm; it is used to record the value of an item (variable) each time that it changes. This manual exercise is called a DRY RUN. A trace table is set up with a column for each variable and a column for any output.

Test data is then used to dry run the flowchart and record the results on a trace table. Test data: 9, 7, 3, 12, 6, 4, 15, 2, 8, 5 TRACE TABLES It can be seen from the output that the algorithm selects the largest and the smallest numbers from a list of 10 positive numbers. The same trace table could have been used if the algorithm had been shown using pseudocode.

Test data is then used to dry run the flowchart and record the results on a trace table. Test data: 9, 7, 3, 12, 6, 4, 15, 2, 8, 5 TRACE TABLES It can be seen from the output that the algorithm selects the largest and the smallest numbers from a list of 10 positive numbers. The same trace table could have been used if the algorithm had been shown using pseudocode.

Write out the Pseudo code , Draw and use the trace table below and the test data 4, 8, 19, 17, 3, 11, 6, 1, 13, 9 to record a dry run of the pseudo code. TASK

Use a trace table and the test data 400, 800, 190, 170, 300, 110, 600, 150, 130, 900 to record another dry run of the pseudocode or flowchart. TASK

Dealing with errors Use a trace table and some negative test data to record another dry run of the pseudocode or flowchart. What error have you found? Redraw algorithm with your correction.

Possible Improvement Error found? The algorithm only works for numbers between 0 and 100; Redraw and improve the algorithm. Could have the on the left. • This algorithm is very similar and works for a much larger range of numbers but it still does not work for every set of numbers. In order to do this the algorithm needs to be rewritten to allow the largest and smallest numbers to be tested against numbers that appear in the list. • (see next slide)

A Better Algorithm Change the pseudocode so it works for every set of numbers like this flowchart

Stages in producing an Algorithm • 1 Make sure that the problem is clearly specified. • 2 Break the problem down into sub-problems; if it is complex, you may want to consider writing an algorithm for each sub-problem. Most problems, even the simplest ones can be divided into: • • set up input processing output of results • 3 Decide on how any data is to be obtained and stored, what is going to happen to the data and how any results are going to be displayed. • 4 Decide on how you are going to construct your algorithm, using a flowchart or pseudocode. • 5 Construct your algorithm, making sure that it can be easily read and understood by someone else. This involves setting it out clearly and using meaningful names for any data stores. The algorithms that you have looked at so far in this chapter were not designed with readability in mind because you needed to work out what the problem being solved was. • 6 Use several sets of test data (normal, abnormal and boundary) and trace tables to find any errors. • 7 If any errors are found, repeat the process until you think that your algorithm works perfectly.

A More Ideal Algorithm Have a look at a more readable flowchart showing the algorithm to select the largest and smallest numbers from a list of 10 numbers.

The effectiveness of a solution There are many different solutions to the same problem. In order to consider the effectiveness of a given solution ask the following questions. • 1 Does the solution work for all sets of data? • 2 Does the solution have any unnecessary processes that are never used? • 3 Are any actions repeated more often than necessary? • 4 Can the solution be simplified and still work as well?
- Slides: 14