Trace Tables (Desk Checking)
Desk checking is a manual (non computerised) technique for checking the logic of an algorithm. The person performing the desk check effectively acts as the computer, using pen and paper to record results. The desk checker carefully follows the algorithm being careful to rigidly adhere to the logic specified. The desk check can expose problems with the algorithm.
Desk checks are useful to check an algorithm (before coding) thereby confirming that the algorithm works as expected and saves time possibly writing a program that doesn't do what was intended. Another benefit of a desk check is that it confirms to the programmer/designer that the algorithm performs as intended.
A desk check is normally done as a table with columns for:
- Pseudo code line number column (Pseudo code doesn't normally have lines numbers, but these are necessary in a desk check to specify the line(s) being executed)
- One column per variable used. The columns should be in alphabetical order on variable name with the variable name at the top of the column. As the algorithm is executed, the new values of the variables are put in the appropriate column. Show working for calculations. If variable names consist of a number of words it is permissible to put a space between each word in the name so that the name fits better in the column by wrapping to the next line. e.g. the variable column heading discount Price could be used rather than the actual variable name discountPrice.
- A condition column. The result of the condition will be true (T) or false (F). As the algorithm is executed, conditions are evaluated and the details are recorded in the column. Show working when evaluating the conditions. This is used whenever a condition is evaluated - IF WHILE or FOR statements all have explicit or implicit conditions.
- An Input/Output column is used to show what is input by the user and displayed by the program. Show inputs with: the variable name, a "?" and the value input e.g. price ? 200. Show outputs with the variable name, an =, and the value displayed e.g. discountPrice = 180 .
The algorithm is followed line by line, with each line of pseudo code for which some action is performed taking up one row in the desk check. When the line of pseudo code does not perform any action, such as an ENDIF (the row in the desk check would be blank), the line number column in the desk check may contain multiple line numbers. In example 2 below, there is an entry in the line number column of "6, 7" means that line 6 was executed, but did not result in any thing new being shown in the desk check; then line 7 was executed which did perform some action that was shown in that row of the desk check. When line numbers in a row for are 3 or more consecutive lines, they can optionally be abbreviated to a range using a hyphen e.g. 10, 11, 12 becomes 10 - 12 (see example 4)
Normally portrait page layout would be used for the desk check, however if the table is too wide, landscape layout may be used.
Desk Checks vs Test Plans
A Desk Check concentrates on the value of variables and the logic i.e. what is the value of variable x after statement n; what is the next statement to be executed? A Test Plan focuses on the values of inputs and outputs required to test a program without concern for the internal workings i.e. are the results (outputs) correct for the inputs?.
Example 1
Problem Description: Calculate the discounted price of an item purchased. (Note: a problem description is not normally specified in a desk check, it is only included to assist in understanding the problem.)
The following example shows desk checking involving sequence, executing instructions one after the other.
Sequence involves executing all instructions once, from top to bottom, one after the other.
Algorithm (with line numbers added for the Desk Check)
1 calcDiscountPrice() 2 Input price, discountPercent 3 discount = price * discountPercent / 100 4 discountPrice = price - discount 5 Display discountPrice 6 STOPDesk Check
Test Data
Inputs: price = $200, discountPercent = 10%.
Correct results: discount = $20, discountPrice = $180.
Line Number discount discount Percent discount Price price Input/Output 1, 2 10 200 price ? 200; discountPercent ? 10 3 200 * 10 / 100 = 20 4 200 - 20 = 180 5 discountPrice = 180 6 Example 2
Problem Description: Calculate the discounted price of an item purchased. Customers receive a discount of 15% on an item if the purchase price of the item is over $100.
The following example shows desk checking involving selection using an IF.
For an IF without an ELSE, if the condition evaluates to True then execution continues to the next line and the lines between the IF and ENDIF are executed (inclusive), otherwise (the condition is False) execution jumps from the IF to the ENDIF.
Algorithm (with line numbers added for the Desk Check)
1 calcPrice() 2 Input price 3 IF price > 100 THEN 4 discount = price * 15 / 100 5 price = price - discount 6 ENDIF 7 Display price 8 STOPDesk Check
Inputs: price = $200
Correct results: price = $170.
Line Number discount price Conditions Input/Output 1, 2 200 price ? 200 3 200 > 100 ? is T 4 200 * 15 / 100 = 30 5 200 - 30 = 170 6, 7 price = 170 8 Inputs: price = $50
Correct results: price = $50.
Line Number discount price Conditions Input/Output 1, 2 50 price ? 50 3 50 > 100 ? is F 6, 7 price = 50 8 Example 3
Problem Description: Display whether a student passed or failed a subject depending on their mark.
The following example shows desk checking involving selection using an IF-ELSE.
For an IF-ELSE, if the condition evaluates to True, execution continues to the next line after the IF and the lines up to but excluding the ELSE are executed, then execution jumps to the ENDIF. If the condition is False, execution jump from the IF to the ELSE, and the lines from the ELSE to the ENDIF (inclusive) are executed.
Algorithm (with line numbers added for the Desk Check)
1 displayGrade() 2 Input mark 3 IF mark >= 50 THEN 4 grade = "Pass" 5 Display "Well done" 6 ELSE 7 grade = "Not pass" 8 ENDIF 9 Display grade 10 STOPDesk Check
Inputs: mark = 75
Correct results: grade = "Pass", "Well done"
Line Number grade mark Conditions Input/Output 1, 2 75 mark ? 75 3 75 >= 50 ? is T 4 Pass 5 "Well done" 8, 9 Pass 10 Desk Check
Inputs: mark = 40
Correct results: grade = "Not pass"
Line Number grade mark conditions Input/Output 1, 2 40 mark ? 40 3 40 >= 50 ? is F 6, 7 Not pass "Well done" 8, 9 Not pass 10 Example 4
Problem Description: Calculate the cost of electricity based on the electricity used.
The following example shows desk checking involving selection using a linear nested IF-ELSEs.
Algorithm (with line numbers added for the Desk Check)
1 calcElectrictyCost() 2 Input electricityUsed 3 IF electricityUsed < 1000 THEN 4 cost = 100 5 usageLevel = "Low" 6 ELSE 7 IF electricityUsed < 5000 THEN 8 cost = 100 + (electricityUsed - 1000) * 0.1 9 usageLevel = "Moderate" 10 ELSE 11 cost = 500 + (electricityUsed - 5000) * 0.08 12 usageLevel = "High" 13 ENDIF 14 ENDIF 15 Display cost, usageLevel 16 STOPDesk Check
Inputs: electricityUsed = 500,
Correct results: cost = 100, usageLevel = "Low".
Line Number cost electricity Used usage Level Conditions Input/ Output 1, 2 500 electricityUsed ? 500 3 500 < 1000 ? is T 4 100 5 Low 14, 15 cost = 100, usageLevel = "Low" 16 Inputs: electricityUsed = 3000,
Correct results: cost = 300, usageLevel = "Moderate".
Line Number cost electricity Used usage Level Conditions Input/ Output 1, 2 3000 electricityUsed ? 3000 3 3000 < 1000 ? is F 6, 7 3000 < 5000 ? is T 8 100 + (3000-1000) * 0.1 = 300 9 Moderate 13 - 15 cost = 300, usageLevel = "Moderate" 16 Inputs: electricityUsed = 15000,
Correct results: cost = 1300, usageLevel = "High".
Line Number cost electricity Used usage Level Conditions Input/ Output 1, 2 15000 electricityUsed ? 15000 3 15000 < 1000 ? is F 6, 7 15000 < 5000 ? is F 10, 11 500 + (15000-5000) * 0.08 = 1300 12 High 13 - 15 cost = 1300, usageLevel = "High" 16 Example 5
Problem Description: Calculate the cost of water based on the water used and whether the customer is a Domestic or Commercial user.
The following example shows desk checking involving selection using a non-linear nested IF-ELSEs.
Algorithm (with line numbers added for the Desk Check)
1 calcWaterCost() 2 Input waterUsed 3 IF waterUsed < 100 THEN 4 cost = 50 5 ELSE 6 Input customerType 7 IF customerType = "D" THEN 8 cost = waterUsed * 0.5 9 ELSE 10 cost = waterUsed * 0.6 11 Display "Commercial rate" 12 ENDIF 13 Display "High usage" 14 ENDIF 15 Display cost 16 STOPDesk Check
Inputs: waterUsed = 70, customerType (not required)
Correct results: cost = 50
Line Number cost customer Type water Used Conditions Input/ Output 1, 2 70 waterUsed ? 70 3 70 < 100 ? is T 4 50 14, 15 cost = 50 16 Inputs: waterUsed = 200, customerType = D
Correct results: cost = 100
Line Number cost customer Type water Used Conditions Input/ Output 1, 2 200 waterUsed ? 200 3 200 < 100 ? is F 5, 6 D customerType ? D 7 D = D ? is T 8 200 * 0.5 = 100 12, 13 High usage 14, 15 cost = 100 16 Inputs: waterUsed = 200, customerType = C
Correct results: cost = 120
Line Number cost customer Type water Used Conditions Input/ Output 1, 2 200 waterUsed ? 200 3 200 < 100 ? is F 5, 6 C customerType ? C 7 C = D ? is F 9, 10 200 * 0.6 = 120 11 Commercial rate 12, 13 High usage 14, 15 cost = 120 16 Example 6
Problem Description: Display a table of values of x and x squared. X is to start at 1 and increase by 1 up to 3.
The following example shows desk checking involving iteration (repetition) using a FOR loop.
The counter variable is initialized once at the top of the loop, the first time through the loop. The implied condition is evaluated at the top of the loop, with execution going to the next line if the condition is True and going to the line after the ENDFOR if the condition is False. In this case the implied condition is x <= 3 ?. On the ENDFOR line the counter variable is incremented by 1, then program execution loops from the ENDFOR line, back to the FOR line.
Algorithm (with line numbers added for the Desk Check)
1 calcSquares() 2 Display "X", "X Squared" 3 FOR x = 1 TO 3 DO 4 xSquared = x * x 5 Display x, xSquared 6 ENDFOR 7 Display "-----------" 8 STOPDesk Check
Test Data
Input: None
Correct results: x = 1, xSquared = 1; x = 2, xSquared = 4; x = 3, xSquared = 9.
Line Number x xSquared Conditions Input/ Output 1, 2 X, X Squared 3 1 1 <= 3 ? is T 4 1 * 1 = 1 5 x = 1, xSquared = 1 6 1 + 1 = 2 3 2 <= 3 ? is T 4 2 * 2 = 4 5 x = 2, xSquared = 4 6 2 + 1 = 3 3 3 <= 3 ? is T 4 3 * 3 = 9 5 x = 3, xSquared = 9 6 3 + 1 = 4 3 4 <= 3? is F 7 ----------- 8 Example 7
Problem Description: Total a series of numbers input by the user. Allow the user to continue inputting numbers until a negative number is input, then display the total of the numbers input.
The following example shows desk checking involving iteration (repetition) using a WHILE loop.
The condition is evaluated at the top of the loop, with execution going to the next line if the condition is True and going to the line after the ENDWHILE if the condition is False. Program execution loops from the ENDWHILE line, back to the WHILE line.
Algorithm (with line numbers added for the Desk Check)
1 totalNumbers() 2 total = 0 3 Input number 4 WHILE number >= 0 DO 5 total = total + number 6 Input number 7 ENDWHILE 8 Display total 9 STOPDesk Check
Inputs: numbers = 10, 6, -1.
Correct results: total = 16
Line Number number total Conditions Input/Output 1, 2 0 3 10 number ? 10 4 10 >= 0 ? is T 5 0 + 10 = 10 6 6 number ? 6 7, 4 6 >= 0 ? is T 5 10 + 6 = 16 6 -1 number ? -1 7, 4 -1 >= 0 ? is F 8 total = 16 9 Example 8
Problem Description: Calculate the membership fee for club members.
The following example shows desk checking involving sequence, selection and iteration (repetition).
Algorithm (with line numbers added for the Desk Check)
1 calcMembershipFees() 2 totalFees = 0 3 Input membershipType 4 WHILE membershipType <> "X" DO 5 IF membershipType = "F" THEN 6 membershipFee = 160 7 membershipName = "Full" 8 ELSE 9 IF membershipType = "J" THEN 10 membershipFee = 80 11 membershipName = "Junior" 12 ELSE 13 IF membershipType = "P" 14 membershipFee = 30 15 membershipName = "Pensioner" 16 ELSE 17 membershipFee = 10 18 membershipName = "Life" 19 ENDIF 20 ENDIF 21 ENDIF 22 totalFees = totalFees + membershipFee 23 Display membershipFee, membershipName 24 Input membershipType 25 ENDWHILE 26 Display totalFees 27 STOPDesk Check
Inputs: membershipType = "J", membershipType = "F", membershipType = "X".
Correct results: membershipFee = 80, membershipName = "Junior"; membershipFee = 160, membershipName = "Full"; totalFees = 240.
Line Number membership Fee membership Name membership Type total Fees Conditions Input/ Output 1, 2 0 3 J membership Type ? J 4 J <> X ? is T 5 J = F? is F 8, 9 J = J ? is T 10 80 11 Junior 20, 21, 22 0 + 80 = 80 23 membership Fee = 80, membership Name = Junior 24 F membership Type ? F 25, 4 F <> X ? is T 5 F = F? is T 6 160 7 Full 21, 22 80 + 160 = 240 23 membership Fee = 160, membership Name = Full 24 X membership Type ? X 25, 4 X <> X ? is F 26 totalFees = 240 27 Example 9
Problem Description: find the smallest of 3 numbers.
The following example shows desk checking involving subroutines.
When a subroutine is called, execution goes from the calling line (subroutine call e.g. line 3 in this example) to the first line in the subroutine (e.g. line 6). The subroutine is then executed, and when the end of the subroutine is reached (e.g. line 14), execution goes back to the line after where it was originally called (e.g. line 4).
Algorithm (with line numbers added for the Desk Check)
1 compareNumbers() 2 Input number1, number2, number3 3 getSmallestNumber() 4 Display smallest 5 STOP6 getSmallestNumber() 7 smallest = number1 8 IF number2 < smallest THEN 9 smallest = number2 10 ENDIF 11 IF number3 < smallest THEN 12 smallest = number3 13 ENDIF 14 EXITDesk Check
Inputs: number1 = 5, number2 = 3, number3 = 8.
Correct result: smallest = 3.
Line Number number1 number2 number3 smallest Conditions Input/ Output 1, 2 5 3 8 number1 ? 5; number2 ? 3; number3 ? 8 3, 6, 7 5 8 3 < 5 ? is T 9 3 10, 11 8 < 3 ? is F 13, 14, 4 smallest = 3 5 Inputs: number1 = 7, number2 = 12, number3 = 9.
Correct result: smallest = 7.
Line Number number1 number2 number3 smallest Conditions Input/ Output 1, 2 7 12 9 number1 ? 7; number2 ? 12; number3 ? 9 3, 6, 7 7 8 12 < 7 ? is F 10, 11 9 < 7 ? is F 13, 14, 4 smallest = 7 5