Algorithm Debugging with Trace Tables: A Comprehensive Guide - Prof. Akerman, Lecture notes of Computer Science

How to use trace tables to find errors in algorithms. It provides examples of how to create and use trace tables to determine the output of an algorithm, find errors, and understand the algorithm's purpose. The document also covers syntax and logical errors, providing examples and corrections. It includes exercises to practice creating and using trace tables, making it a useful resource for students learning about algorithm analysis and debugging. Useful for high school students and university students. the document also includes examples of how to correct errors in code and how to determine the function of an algorithm using trace tables. It provides a step-by-step guide on how to create a trace table and how to use it to find errors in a program. The document also includes a section on syntax errors and logical errors, explaining what both of these mean with examples. A valuable resource for anyone learning about algorithms and debugging.

Typology: Lecture notes

2024/2025

Uploaded on 05/22/2025

bananana-apple
bananana-apple 🇬🇧

4

(1)

25 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
When programming, it is common to make mistakes.
What techniques can a programmer use to find mistakes in
their programs?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Algorithm Debugging with Trace Tables: A Comprehensive Guide - Prof. Akerman and more Lecture notes Computer Science in PDF only on Docsity!

When programming, it is common to make mistakes.

What techniques can a programmer use to find mistakes in

their programs?

There are a number of ways:

The first is to use a trace table – this is where the

programmer goes through the code, line by line, writing

down the values of variables

 Alternatively IDEs (Integrated development

environments) can be used

These have the ability to set breakpoints, step through

code and watch variables. This is similar to trace tables

and allows the computer to do the processing

Trace tables

Sometimes you may be given an algorithm in the form of a

flowchart or pseudocode, and asked to determine its purpose

One way to do this is to use a trace table

 Trace tables are used to determine the outputs from a

program as it runs

They enable a programmer to find errors in their

programs

Using a trace table

The value of each variable is recorded as it changes

What value is output from the code below?

num = 3 n = 0 while n < 4 num = num + n n = n + 1 endwhile print(num) num n n < 4 OUTPUT 3 0 TRUE

Using a trace table

  • (^) The value of each variable is recorded as it changes
  • (^) What value is output from the code below? num = 3 n = 0 while n < 4 num = num + n n = n + 1 endwhile print(num) num n n < 4 OUTPUT 3 0 TRUE 3 1 TRUE 4 2 TRUE

Using a trace table

  • (^) The value of each variable is recorded as it changes
  • (^) What value is output from the code below? num = 3 n = 0 while n < 4 num = num + n n = n + 1 endwhile print(num) num n n < 4 OUTPUT 3 0 TRUE 3 1 TRUE 4 2 TRUE 6 3 TRUE

Creating a trace table

A trace table is useful for

 Determining the purpose of an algorithm

 Finding the output of an algorithm

 Finding errors in an algorithm

To draw a trace table, make a column for each variable used,

in the order in which they appear

You don’t need to fill in a value for a variable which does not

change in a particular row

Determining the function of an algorithm

Complete the trace table for the algorithm and state its

function:

Assume the user enters values 8, 3, 6, 5, 10, 6

total count base height area output 0 1 8 3 12 total = 0 for count = 1 to 3 base = input() height = input() area = (base*height)/ total = total + area next count result = total / 3 print(result)

total = 0 for count = 1 to 3 base = input() height = input() area = (base*height)/ total = total + area next count result = total / 3 print(result) total count base height area output 0 1 8 3 12 12 2 6 5 15 27 3 10 6 30

Determining the function of an algorithm

Complete the trace table for the algorithm and state its

function:

Assume the user enters values 8, 3, 6, 5, 10, 6

total = 0 for count = 1 to 3 base = input() height = input() area = (base*height)/ total = total + area next count result = total / 3 print(result) total count base height area output 0 1 8 3 12 12 2 6 5 15 27 3 10 6 30 57 4 19

Determining the function of an algorithm

The algorithm finds the average of the areas of the

triangles.

Assume the user enters values 8, 3, 6, 5, 10, 6

Finding errors

Trace tables are often used to find errors

  • (^) The Fibonacci sequence takes the previous two numbers to

find the next number – e.g. 0, 1, 1, 2, 3, 5, 8, 13…

  • (^) Look at the following code and complete the trace table for it i newNum num1 Num2 output 0 1 0 1 num1 = 0 num2 = 1 print(num1) print(num2) for i = 1 to 5 newNum = num1 + num print(newNum) num1 = num num2 = num1 + num endfor

Finding errors

The trace table shows the output will be:

0, 1, 1, 3, 6, 12, 24 – it should be 0, 1, 1, 2, 3, 5, 8

  • (^) Change one line of code below to fix the problem
  • (^) Create another table and check your algorithm i newNum num1 Num2 output 0 1 0 1 1 1 1 2 1 2 3 2 4 3 3 6 4 8 6 4 12 8 16 12 5 24 16 32 24 num1 = 0 num2 = 1 print(num1) print(num2) for i = 1 to 5 newNum = num1 + num print(newNum) num1 = num num2 = num1 + num endfor

Errors

The original Fibonacci program worked, but gave a result that

wasn’t intended by the programmer

  • (^) This is an example of a logical error

The below program has three syntax errors – what are they?

name = input("Type in your name)

if name = "George"

print("hello" name)

else

print("Your name isn’t George")

endif

Errors

Corrected code:

name = input("Type in your name")

if name == "George"

print("hello" + name)

else

print("Your name isn’t George")

endif

Missing quote mark to end string == for equality (= means assignment)

  • needed to concatenate two strings