Trace tables
Dry run
A Trace Table is a tool used by programmers to trace the flow of execution of a computer program. It is a tabular representation of the program's variables and their values at each step of the program's execution.
The table typically has columns for each variable in the program, and a row for each step in the program's execution. Each row shows the values of the variables at that step in the program's execution.
The Trace Table is useful for debugging programs, as it allows the programmer to see how the program's variables change as the program executes. By examining the Trace Table, the programmer can identify errors in the program and correct them.
The Trace Table is also useful for understanding how the program works, as it provides a detailed view of the program's execution. This can be especially helpful for complex programs with many variables and complex logic.
Let's say we have a simple program that calculates the sum of two numbers entered by the user. The program takes two numbers as input, adds them together, and then displays the result to the user. We can create a Trace Table to track the values of the variables as the program executes.
Here's an example code for the program that calculates the sum of two numbers entered by the user in Python:
3 number2 = int(input("Enter the second number: "))
4 sum = number1 + number2
5 print("The sum of", number1, "and", number2, "is", sum)
Here's an example of what the Trace Table might look like:
Step | Number1 | Number2 | Sum | Output |
---|---|---|---|---|
1 | 0 | 0 | ||
2 | 5 | |||
3 | 5 | 7 | ||
4 | 5 | 7 | 12 | |
5 | 5 | 7 | 12 | 12 |
In the first row, all the variables are initialized to zero. In the second row, the program prompts the user to enter the first number, and the value of number1 is updated to 5. In the third row, the program prompts the user to enter the second number, and the value of number2 is updated to 7. In the fourth row, the program calculates the sum of the two numbers and updates the value of sum to 12. Finally, print() function displays the value of the sum variable.
This Trace Table shows how the program executes and how the values of the variables change as the program runs. It can be a useful tool for debugging the program or understanding how it works.
ChatGPT made the explanation above.
Here is another explanation from 101computing:
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 demonstrates 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.
General rules to follow when creating a trace table for a program:
- Start with an initial state: Begin with an initial state of the variables in the program, before any instructions have been executed.
- Identify the next instruction: Identify the next instruction to be executed in the program, based on the current state of the variables and the program counter.
- Execute the instruction: Execute the instruction and update the state of the variables accordingly.
- Record the new state: Record the new state of the variables after the instruction has been executed.
- Repeat steps 2-4: Repeat steps 2-4 for each subsequent instruction in the program, until the program terminates.
- Include comments: Include comments or notes to describe what each instruction is doing and how it affects the state of the variables.
- Use clear formatting: Use a clear and consistent formatting style for the trace table, such as using a table with columns for the instruction number, instruction mnemonic, variables used, and new variable values.
- Check for errors: Check the trace table for errors and inconsistencies, such as incorrect values or missing steps.
- Test the trace table: Test the trace table by running the program and verifying that the values in the trace table match the actual values produced by the program.
By following these rules, you can create a clear and accurate trace table that accurately reflects the behavior of the program and helps you understand how it works.
Now you are ready to create your own trace tables. Good luck!
Create Trace Table for the Algorithms below
Trace tables are a way of testing pseudo code, flowcharts or a program. They ensure that all variables are changing as you would expect as the different parts execute and can be used to identify logical errors.
Task 1.
Task 3
Task 4.
Trace Tables and Flow Charts
Trace table: Challenges level 2 Tasks from Chat GPT
Practice makes perfect. You will create trace tables for various tasks by completing this set of challenges. These are simple examples, but they illustrate how you can use pseudocode, LMC, to describe a program's logic and then use that to create a trace table. Once you have the pseudocode, you can break down each step into the relevant variables and values, and then use the rules for creating a trace table to fill in the values and track the state of the program.
Task 1: Adding two numbers
- Set variable x to 5
- Set variable y to 10
- Set variable z to x + y
- Print variable z
- Set variable n to 5
- Set variable result to 1
- For each number i from n down to 1, do the following:
- Multiply result by i
- End loop
- Print variable result
- Set variable n to 7
- If n is less than 2, then
- Set variable is_prime to false
- Else if n is equal to 2, then
- Set variable is_prime to true
- Else,
- Set variable is_prime to true
- For each number i from 2 to n-1, do the following:
- If n is divisible by i, then
- Set variable is_prime to false
- Exit loop
- End if
- End loop
- End if
- Print variable is_prime
Task 4. LMC conditions
- INP
- STA A
- INP
- STA B
- LDA A
- SUB B
- BRP SUM
- BRA DIFF
- LDA A
- STA TEMP
- LDA B
- STA A
- LDA TEMP
- STA B
- SUM: LDA A
- ADD B
- OUT
- HLT
- DIFF: LDA B
- SUB A
- OUT
- HLT
- A DAT 0
- B DAT 0
- TEMP DAT 0
Task 5. LMC conditions
- INP
- STA COUNT
- LOOP: LDA COUNT
- OUT
- SUB ONE
- BRZ END
- STA COUNT
- BRA LOOP
- END: HLT
- // Data section
- COUNT DAT 0
- ONE DAT 1
Task 6. LMC loops
- INP // input a number
- STA COUNT // store the number in the COUNT variable
- LDA ZERO // load the ZERO value into the accumulator
- STA SUM // initialize the SUM variable to zero
- LOOP: // start of the loop
- LDA COUNT // load the COUNT variable into the accumulator
- SUB ONE // subtract one from the COUNT variable
- STA COUNT // store the result back in the COUNT variable
- BRZ END // if the result is zero, exit the loop
- LDA SUM // load the SUM variable into the accumulator
- ADD COUNT // add the COUNT variable to the accumulator
- STA SUM // store the result back in the SUM variable
- BRA LOOP // jump back to the start of the loop
- END: // end of the loop
- LDA SUM // load the SUM variable into the accumulator
- OUT // output the result
- HLT // halt the program
- // Data section
- COUNT DAT 0
- SUM DAT 0
- ZERO DAT 0
- ONE DAT 1
Task 7. LMC loops
- INP // input the first number
- STA A // store it in the A variable
- INP // input the second number
- STA B // store it in the B variable
- LDA A // load A into the accumulator
- BRZ END // if A is zero, skip the loop
- LOOP: // start of the loop
- SUB ONE // subtract one from A
- BRZ END // if A is zero, exit the loop
- ADD B // add B to the accumulator
- STA B // store the result back in B
- BRA LOOP // jump back to the start of the loop
- END: // end of the loop
- LDA B // load the final result into the accumulator
- OUT // output the result
- HLT // halt the program
- // Data section
- A DAT 0
- B DAT 0
- ONE DAT 1
Here are some interactive tasks from 101computing!
More interactive trace table tasks : click here!
Additional exercises on trace tables: click here!
2. Analyse the code snippet below
Complete the trace table. [4]
Line number | Address | Instruction |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
00
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 |
LDA 16
STA 13 LDA 13 BRZ 10 SUB 14 STA 13 LDA 15 ADD 16 STA 15 BRA 02 LDA 15 OUT HLT DAT 00 DAT 01 DAT 00 DAT 03 |