A trace table is a technique used to test an algorithm and predict step by step how the computer will run the algorithm. It can be used to understand or predict what an algorithm is doing and to identify potential logic errors (when the program compiles but does not produce the expected output).
The animation below demonstrate the use of a trace table used to track the values of variables as they change while the program is running.
Using a trace table to test an algorithm is called dry run testing.
Check the following algorithms and complete their trace tables. (Click on any cells of these tables to add content)
i = 0 j = 10 WHILE i < j i = i + 1 j = j - 1 END WHILE OUTPUT(i) OUTPUT(j)
Line Number | i | j | i < j | OUTPUT |
---|---|---|---|---|
+ – | 1 | 0 | ||
+ – | 2 | 10 | ||
+ – | 4 | True | ||
+ – | 5 | 1 | ||
+ – | 6 | 9 | ||
+ – | 4 | True | ||
+ – | … |
number = 5 factorial = number WHILE number>2 number = number - 1 factorial = factorial * number END WHILE OUTPUT(factorial)
Line Number | number | factorial | number > 2 | OUTPUT |
---|---|---|---|---|
+ – | 1 | 5 | ||
+ – | 2 | 5 | ||
+ – | … |
FOR i FROM 1 TO 20 IF i MOD 3 == 0 AND i MOD 5 == 0 THEN OUTPUT "Fizz-Buzz" ELSE IF i MOD 3 == 0 THEN OUTPUT "Fizz" ELSE IF i MOD 5 == 0 THEN OUTPUT "Buzz" ELSE OUTPUT i END IF NEXT i OUTPUT("The End")
Line Number | i | i MOD 3 == 0 | i MOD 5 == 0 | OUTPUT |
---|---|---|---|---|
+ – | 1 | 1 | ||
+ – | 2 | False | False | |
+ – | 4 | False | ||
+ – | 6 | False | ||
+ – | 8 | |||
+ – | 9 | 1 | ||
+ – | 1 | 2 | ||
+ – | … |
FUNCTION max(a, b) IF a>b THEN RETURN a ELSE RETURN b END IF END FUNCTION number1 = 7 number2 = 12 number3 = max(number1, number2) OUTPUT(number3)
Line Number | number1 | number2 | number3 | a | b | a>b | RETURN | OUTPUT |
---|---|---|---|---|---|---|---|---|
+ – | 9 | 7 | ||||||
+ – | 10 | 12 | ||||||
+ – | 11 | 7 | 12 | |||||
+ – | 1 | 7 | 12 | |||||
+ – | … |
Did you like this challenge?
Click on a star to rate it!
Average rating 2.6 / 5. Vote count: 1308
No votes so far! Be the first to rate this post.
As you found this challenge interesting.
Follow us on social media!