# 11- Trace Tables

1. A trace table allows a programmer to list the variables and predict the output of each variable to see how the code is actually functioning.

True

False

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

test

graph

table

3. The advantages of using a trace table (sometimes called white box testing as the code is tested directly) includes:

it helps the programmer see how the code works

It can be helpful in identifying why an error is occuring and specifically where

All of the above are valid advantages of the use of trace tables

it can help identify and spot errors

4. The flowchart inputs the size of a number of car engines; a value of –1 stops the input. Information is output. Can you fil in the second line of the trace table?
```Complete the trace table (the second line) for the
following input data:

1.8, 2.0, 1.0, 1.3, 1.0, 2.5, 2.0, 1.3, 1.8, 1.3, –1

Engine ¦ Count  ¦ Number ¦ Size ¦ Average ¦ OUTPUT
0    ¦    0   ¦    0   ¦  1.8 ¦         ¦
---------------------------------------------------
¦        ¦        ¦      ¦         ¦
---------------------------------------------------- ``` 1.8,1,1, 1.8

2.0,1,1, 2.0

1,1,1, 2.0

1.8,1,1, 2.0

5. A woman invests £200 on January 1 for each of three years in a fixed income bond that pays interest of 8 per cent per annum, the interest being added to her account at the end of each year.
```The  algorithm gives the total
value of her investment after the three year period. Here is a trace table to show the output.

A	I
0	1
200	1
216	1
216	?
416	?
449.28	?
449.28	3
649.28	3
701.22	3
701.22	3

Can you fill in the blanks for the three question marks?```

3,3,3

2,2,2

2,3,4

1,1,1

6. Trace the algorithm (first and second lines) when A = 1 and B = 1.
```The algorithm below is used to generate a sequence of numbers.

LINE 10	INPUT A, B
LINE 20	PRINT A, B
LINE 30	LET C = A + B
LINE 40	PRINT C
LINE 50	LET A = B
LINE 60	LET B = C
LINE 70	IF C < 10 THEN GOTO LINE 30
LINE 80	END```

A = 1, B=1, C=2 and second line A = 1, B = 1, C = 4

A = 1, B=1, C=3 and second line A = 2, B = 3, C = 4

A = 1, B=1, C=2 and second line A = 1, B = 2, C = 3

A = 1, B=2, C=3 and second line A = 1, B = 2, C = 3

7. John uses internet banking. This pseudocode checks his pin. Trace the logic to predict the value of c and what message would be output if 5 1 0 2 0 was entered?
```c <-- 0
INPUT PIN
x <-- PIN
REPEAT
x <-- x/10
c <-- c + 1
UNTIL x < 1
IF c <> 5
THEN
PRINT “error in PIN entered”
ELSE
PRINT “PIN OK”
ENDIF ```

4 / PIN OK

6 / PIN OK

5 / error in PIN

5 / PIN OK

8. This pseudocode checks the entered PIN. Trace the logic to predict the value of c and what message would be output if 5 1 2 0 was entered?
```c < 0
INPUT PIN
x < PIN
REPEAT
x < x/10
c < c + 1
UNTIL x < 1
IF c <> 5
THEN
PRINT “error in PIN entered”
ELSE
PRINT “PIN OK”
ENDIF
```

4 / error in PIN entered

5 / error in PIN entered

4 / PIN OK

5 / PIN OK

9. A student is using the algorithm below. Trace the algorithm in the case where A = 5 and B = 3. Is the answer shown correct? If not, why?
```LINE 10	INPUT A, B
LINE 20	LET C = A – B
LINE 30	LET D = A + B
LINE 40	LET E = (D*D) – (C * C)
LINE 50	LET F = E / 4
LINE 60	PRINT F
LINE 70	END

TRACE TABLE
------------

A	B	C	D	E	F
===========================================
5	1
2
8
60
15```

The value of B on the first line of the trace table should be 3

The value of B on the first line of the trace table should be 2

The trace table is correct

The value of B on the first line of the trace table should be 4

10. The user enters an input of 2. Is the trace table below correct?
```1.	dogs_age <--USERINPUT
2.	IF dogs_age is less than or equal to 2
3.	 	human_dog_years = dogs_age * 12
4.	ELSE
5.	      extra_years = (dogs_age – 2) * 6
6.	      human_dog_years = 24 + extra_years
7.	END IF
8.	OUTPUT human_dog_years

The user enters a dog’s age of 2. Complete the trace table below

TRACE TABLE
============
dogs_age  ¦ dogs_age < or equal 2 ¦ human_dog_years ¦ extra_years ¦  OUTPUT
---------------------------------------------------------------------
2	            True	                  20		                   20
----------------------------------------------------------------------```

No - 20 in both cases should be 24

Yes

No - 20 in both cases should be 6

No - 20 in both cases should be 12