# Trace Tables

A trace table is a technique used to test algorithms, in order to make sure that no logical errors occur whilst the algorithm is being processed.

The table usually takes the form of a multi-column, multi-row table; With each column showing a variable, and each row showing each number input into the algorithm and the subsequent values of the variables.

Trace tables are typically used in schools and colleges when teaching students how to program, they can be an essential tool in teaching students how a certain algorithm works and the systematic process that is occurring when the algorithm is executed.

They can also be useful for debugging applications, using a trace table can help a programmer easily detect what error is occurring, and why it may be occurring.

``````
int x = 0;
for(int i = 1; i <= 10) i++){
x = i * 2;
}
```
```
i x
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
10 20

## Example problem

Bernadette owns a painting and decorating company. She is writing an algorithm to calculate estimates for clients.

Bernadette charges £10 per square metre to paint. If wallpaper has to be removed she charges an additional £75. To calculate the area of wall to paint she needs the height and width for each wall.

Bernadette has chosen to use a subprogram to calculate the estimate. The subprogram will take three parameter value – the name of the client, the number of walls to paint and whether there is wallpaper to remove ("Y") or not ("N").

She has written a pseudocode version of the algorithm which is shown below.

``````#Example Pseudocode solution for the painter problem
PROCEDURE calcEstimate (customer, walls, paper)#declaring the procedure with arguments
START PROCEDURE
SET totalCost TO 0
SET totalArea TO 0

IF paper = "Y" THEN
SET totalCost TO totalCost + 75
END IF

For i FROM 0 TO walls DO
SEND "Enter the height of wall", i+1 TO DISPLAY
RECIEVE ht FROM (REAL) KEYBOARD

SET area TO ht * wd

SET totalArea TO totalArea + area
END FOR

SET totalCost TO totalCost + (totalArea * 10)

SEND "Estimate for ", customer TO DISPLAY
SEND "Cost is ", totalCost TO DISPLAY
END PROCEDURE

calcEstimate ('Pillar', 2, 'Y') #call to the procedure passing the input parameters
```
```

Assume that two walls need to be painted are:

• 1. 2m high by 4m wide
• 2. 2m high by 3.5m wide

Complete a trace table, using this call, to show the values expected throughout the execution of the procedure

 customer walls paper totalCost totalArea i ht wd area

For more support with this look at this pdf.