Information Technology Topic
Trace Tables
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- A trace table follows an algorithm by hand: the column headings are the names of the variables, the cells hold their values, and there is one row for each pass of a loop
- Build the table by listing the variables across the top, writing the starting values in a first row, then working through the loop one pass at a time and filling in the new values
- Compare the traced output with the answer you predicted: if they differ, or a loop never stops, the trace shows exactly which variable goes wrong
Section 1What a trace table is
A trace table is a table you fill in by hand to follow what an algorithm does. It shows the value of every variable as the algorithm runs, so you can see the result before the computer ever runs it. It works like a dry run: you play the part of the computer, one step at a time, and write down what changes. Teachers also call this tracing the algorithm. Why bother? Because a program can look right and still give wrong answers. A trace makes the faults show up. It can also prove that a loop will stop, or show that it never does.
Section 2Setting up the table
The column headings are the names of the variables in the algorithm, such as i, n and total. Add a first column for the pass number so you can keep count. Add an Output column if the algorithm displays something along the way. There is one row for each pass of the loop. Each cell holds the value of that variable at the end of the pass. Write a dash for a variable that has no value yet. Here's the trick: put a start row above the first pass. It holds the values set by the lines before the loop, such as Store 0 in total. Then do the loop, a pass at a time, and add a row each time.
Section 3A worked trace of a For loop
The values entered are 5, 3, 8 and 4. Store 0 in total For i from 1 to 4 Read n Store total + n in total End For Display total The columns are i, n and total. pass | i | n | total start | - | - | 0 1 | 1 | 5 | 5 2 | 2 | 3 | 8 3 | 3 | 8 | 16 4 | 4 | 4 | 20 In the start row only total has a value, 0. On pass 1, i becomes 1, n reads 5 and total becomes 0 + 5 = 5. Each later row carries the old total forward and adds the new n. After pass 4, the loop ends and the Display line shows 20. The table has 4 rows because the loop runs 4 times.
Section 4Tracing a While loop and its test
A While loop tests its condition before every pass. If the test is TRUE the body runs and you add a row. If it is FALSE the loop ends and no row is added for that test. Store 1 in n Store 0 in sum While n <= 5 Store sum + n in sum Store n + 1 in n End While Display sum The columns are n and sum. pass | n | sum start | 1 | 0 1 | 2 | 1 2 | 3 | 3 3 | 4 | 6 4 | 5 | 10 5 | 6 | 15 After pass 5, n is 6. The test n <= 5 is now FALSE, so the loop ends and the program displays 15. The rows count the passes, so there are 5 rows, not 6. A While loop whose condition is FALSE the first time has no rows at all, and the start row is the whole table.
Section 5Using a trace to find faults
Tracing is most useful when something is wrong. Look at this loop, which is meant to count down from 10 to 0 in steps of 3: Store 10 in n While n <> 0 Store n - 3 in n End While Display n The column is n. pass | n start | 10 1 | 7 2 | 4 3 | 1 4 | -2 5 | -5 The values are 7, 4, 1, -2, -5 and the list goes on. n jumps from 1 to -2 and never equals 0, so the test n <> 0 is never FALSE. The loop never ends. Easy to mix up: the trace tells you the loop is stuck, but you must read the values to see why. A fix is to test n > 0 instead: Store 10 in n While n > 0 Store n - 3 in n End While Display n Now the loop ends when n becomes -2, after 4 passes, and the program displays -2.
Section 6Tracing decisions, and choosing test data
A program with no loop has no passes, so you trace it from start to finish. Write the values, then record the TRUE or FALSE result of each condition and what is displayed. Read age If age <= 12 then Display 100 Else Display 200 End If If age is 12, then age <= 12 is TRUE and the program displays 100. If age is 13, the test is FALSE and the program displays 200. Choose test data you can check. Work out the answer by hand before tracing, then compare. Always include the boundary values, such as 12 and 13 here, because that is where faults hide. If the traced output differs from the predicted output, the algorithm, the trace or the prediction contains a mistake.
Examples
A shop in Mandeville records the number of items sold in four hours: 5, 3, 8 and 4. The totalling program is run with these values. Store 0 in total For i from 1 to 4 Read n Store total + n in total End For Display total (a) Complete a trace table with the columns pass, i, n and total, including a start row. (b) State what the program displays. (c) State how many rows the table has and why.
(a) The start row is i -, n -, total 0. Then one row per pass: pass | i | n | total start | - | - | 0 1 | 1 | 5 | 5 2 | 2 | 3 | 8 3 | 3 | 8 | 16 4 | 4 | 4 | 20 (b) After pass 4, the loop ends and the program displays total, 20. (c) There are 4 rows after the start row, because For i from 1 to 4 repeats its body 4 times, once for each value of i.
A bakery in Spanish Town enters the prices 200, 350, 150 and 300, then 0 to finish. The average is displayed by this program: Store 0 in total Store 0 in count Read price While price <> 0 Store total + price in total Store count + 1 in count Read price End While Display total / count (a) Complete a trace table with the columns pass, price, total and count, with a start row. (b) State what the program displays. (c) A student changes the counter line to Store count + 2 in count. Trace the changed program and explain how the trace shows the fault.
(a) The first Read happens before the loop, so the start row has price 200, total 0, count 0. In each pass the old price is added and counted, then the next price is read. pass | price | total | count start | 200 | 0 | 0 1 | 350 | 200 | 1 2 | 150 | 550 | 2 3 | 300 | 700 | 3 4 | 0 | 1000 | 4 In pass 4 the next price read is 0, so the test price <> 0 is FALSE and the loop ends. (b) total is 1000 and count is 4, so the program displays 1000 / 4 = 250. (c) With the counter going up by 2, the count column reads 2, 4, 6, 8: pass | price | total | count start | 200 | 0 | 0 1 | 350 | 200 | 2 2 | 150 | 550 | 4 3 | 300 | 700 | 6 4 | 0 | 1000 | 8 The last count is 8, but only 4 prices were counted. The program would display 1000 / 8 = 125, not 250. The count column in the trace shows the fault straight away: it is double what it should be. The fix is Store count + 1 in count.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 2, Specific Objective 8 · Syllabus scope
- The Python Tutorial (sections 3, 4, 8) and The Python Standard Library, Built-in Types (version 3.15 pages) · Python Software Foundation · Section 3.2, First steps towards programming (variables take new values on each pass of a loop until the condition is false), checked against (https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 13, Statements, 13.2.7 The while..do statement, checked against (https://www.freepascal.org/docs-html/ref/refsu61.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 13, Statements, 13.2.4 The for..to/downto..do statement, checked against (https://www.freepascal.org/docs-html/ref/refsu58.html) · Facts