230 likes | 699 Views
Dry Runs and Trace Tables. 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.
E N D
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 ).
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.
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.
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
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
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