Hand Trace Instructions


Purpose:  
    A code trace is a method for hand simulating the execution of your code in order to manually verify that it works correctly before you compile it.   It is also known as a "code trace" or "desk check."

1.    Draw a table with each variable in the program shown at the top of the column.  Draw a column for Statement number on the left. Also draw a column for Output.

2.    Number each executable statement in the code.

3.    Starting with the first executable statement #1, simulate the action the computer will take when it executes each statement.   In the  appropriate column, write the value that is assigned to that variable (or output produced).  Continue in this manner, writing the action of each statement on a new line.

4.    If input data is required, use the inputs from one of your test cases from your test plans.  Label your table to indicate which test case you are using.

5.    When the trace is complete, check that the output produced matches the expected output in your test plan.


Example:  Here is a code trace table for the Ounces program shown on the next page.

Statement     ounces  cups    quarts  gallons         Output
---------     ------  ----    ------  -------         ------
16                                                  Enter number of ounces

17             9946

20                    1243

21                              310

22                                      155

25                                    9946 Ounces =
                                                    1243 cups
                                                    310 quarts
                                                    155 gallons

   
     1      /* Program : Ounces
     2       * Abstract: Converts ounces to cups, quarts, and gallons
     3       *          User must enter number of ounces.
     4       * Author  : J. Dalbey    10/11/97
     5       */
     6              #include <stdio.h>
     7
     8              int main(void)
     9              {
    10                  int ounces;      /* Input - number of ounces */
    11                  int cups;        /* Output - converted to cups */
    12                  int quarts;      /*        - converted to quarts */
    13                  int gallons;     /*        - converted to gallons */
    14
    15                  /* Get Input Data */
    16                  printf("Enter number of ounces: ");
    17                  scanf("%d", &ounces);
    18
    19                  /* Calculate conversions */
    20                  cups = ounces / 8;
    21                  quarts = cups / 4;
    22                  gallons = ounces / 64;
    23
    24                  /* Display results */
    25                  printf("%d Ounces = \n%d  cups\n%d quarts\n%d gallons\n",
    26                            ounces, cups, quarts, gallons);
    27                  return 0;
    28               }


View code without line numbers