Download presentation
1
Dry Runs and Trace Tables
2
Dry Runs An important part of the algorithm design process is that of testing the final design. To do this we carry out a dry run of the algorithm. When dry running an algorithm we select some appropriate test data and complete a full execution of the algorithm until termination is achieved ( or not, if the algorithm is incorrectly specified ).
3
Trace Table When executing a dry run of an algorithm we record the state of the algorithm after each instruction. Generally, we define the state of an algorithm as: the contents of any variables that have been declared during the execution of the algorithm.
4
Trace Table To record the state of an algorithm we use a trace table.
When tracing a non-recursive algorithm our trace tables take the following form: The column headings of a trace table record the names of all the variables used in the algorithm. The rows record the state of the variables after every instruction execution.
5
Example Dry Run the iterative algorithm that computes the triangular value of a positive number. Use the value 5 as test data. {Print triangular value of input number} PRINT “Enter a number “ READ number triangular_value 0 FOR ( value FROM 1 TO number ) triangular_value triangular_value + value ENDFOR PRINT triangular_value
6
number triangular_value value PRINT - READ 5 FOR 1 2 3 6 4 10 15
7
Example Dry Run the recursive algorithm that computes the triangular value of a positive number. Use the value 5 as test data. Module Name: tri Inputs: n Outputs: triangular value of n Process: IF ( n = 1 ) THEN /* Base Case */ RETURN 1 ELSE /* Recursive solution */ RETURN n + tri(n-1) ENDIF
8
n=5 5=1 ? FALSE RETURN 5+tri(4) n=4 4=1 ? FALSE RETURN 4+tri(3) n=3 3=1 ? FALSE RETURN 3+tri(2) n=2 2=1 ? FALSE RETURN 2+tri(1) n=1 1=1 ? TRUE RETURN 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.