Assume the book has 3 pages, the trace table looks like this:
Step | next page | count |
Get book | ? | ? |
Open book | ? | ? |
If there is no next page go to step 7 | NO | ? |
Find next page | NO | ? |
Increment count | NO | 1 |
If there is no next page go to step 7 | NO | 1 |
Find next page | NO | 1 |
Increment count | NO | 2 |
If there is no next page go to step 7 | NO | 2 |
Find next page | NO | 2 |
Increment count | NO | 3 |
If there is no next page go to step 7 | YES | 3 |
Finish | YES | 3 |
The column headed next page looks odd. Why is the answer NO when there is a next page? Keep in mind that the question that is asked is "Is there NO next page", if there is no next page then the answer is YES, if there is a next page then the answer to NO next page is NO.
Exercise 2 Trace Table:
Here is the algorithm again:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Since
it is in the repetitions that a program does most of its work it follows
that the trace table will contain lots of repetition. One way to simplify
the trace table is to split it up on the basis of the loops that are in
the table. The diagram here shows two loops, the paging loop and the vowel
counting loop. The counting loop is contained within the paging loop. We
can build three trace tables, one for each loop and one main table, or,
we could build two tables, one main table containing the outer main paging
loop and a separate table for the inner counting loop.
I've taken the two-tables option, a main table and a vowel_counter table
and abbreviated the variable names:
Step | total | next_page | current | last | vowel_counter | not_vowel |
1 | 0 | ? | ? | ? | ? | ? |
2 | 0 | ? | ? | ? | ? | ? |
3 | 0 | ? | ? | ? | ? | ? |
4 | 0 | ? | ? | ? | ? | ? |
5 | 0 | NO | ? | ? | ? | ? |
6 - 14 | The trace table for these rows will appear below | |||||
15 | 20 | NO | 301 | 300 | 20 | YES |
16 | 20 | NO | 301 | 300 | 0 | YES |
17 | 20 | NO | 301 | 300 | 0 | YES |
4 | 20 | NO | 301 | 300 | 0 | YES |
5 | 20 | NO | 301 | 300 | 0 | YES |
6 - 14 | Note that the main table shows the current state of the variables in the vowel counter table | |||||
15 | 51 | NO | 422 | 421 | 31 | NO |
16 | 51 | NO | 422 | 421 | 0 | NO |
17 | 51 | NO | 422 | 421 | 0 | NO |
4* | 51 | YES | 422 | 421 | 0 | NO |
18 | 51 | YES | 422 | 421 | 0 | NO |
This trace table sample shows that we have a book of two pages - not much of a book - that the first page contained 300 letters with twenty vowels, the second page contained 421 letters with 31 vowels. The last step 4 (shown with an asterisk) was the point at which the algorithm determined that there was no more pages. I haven't rebuilt the vowel-counting trace here since it won't differ substantially from the table above (Vowel counter trace table - vowels per page).
Although trace tables are a very useful tool for testing algorithms you have probably already realised that the tables can get quite large and cumbersome. There are other algorithm testing and proving techniques but they are outside the scope of this course.
How you build and use trace tables is largely a matter of choice and as you develop your program design skills you will refine your testing and checking skills.
This publication is copyright David Beech and Learning Systems 1997-2002
and may not be reproduced by any means without the written permission of
David Beech.
9 Wyndella Street, Tasmania, Australia