Computer Science 111, Assignment 4
Sample code traces with a loop

In Assignment 4, you will be asked to write code traces for one of the practice problems.

Here are some sample code traces for average1G.cpp, one of the Assignment 3 example programs. Line numbers refer to this line numbered copy of average1G.cpp.  (For info on how to generate a line-numbered copy, see the section on Code traces with while loops in Assignment 4's Introductory tutorial on while loops.)

Because this program contains a loop, some lines may be executed more than once. In the first code trace below, "33a" refers to the first time line 33 is executed, "33b" refers to the second time line 33 is executed, and "33c" refers to the third time line 33 is executed. As in the Assignment 2 examples, I have included entries for each variable only for lines on which a variable's value CHANGES. A question mark indicates a garbage value. Under the heading "boolean value of condition" is the value of whichever condition, if any, is tested on the particular line, in the heading of either an if statement or a for loop.


Sample code trace:   Input:  3 for the number of numbers, then 3, 4, and 6 for the numbers to be averaged:

Line       Variables                                       boolean
number                                                     value of
           numberOfNumbers  sum   count  number  average   condition 

  26              ?
  27              3
  30                                                       true

  38                         0
  39                                0

  43a                                                       true
  46a                                       ?
  47a                                       3
  50a                        3
  53a                               1

  43b                                                       true
  46b                                       ?
  47b                                       4
  50b                        7
  53b                               2

  43c                                                       true
  46c                                       ?
  47c                                       6
  50c                        13
  53c                               3

  43d                                                       false

  57                                              4.33333


Sample code trace:   Input:  0 for the number of numbers:

Line       Variables                                       boolean
number                                                     value of
           numberOfNumbers  sum   count  number  average   condition 

  26              ?
  27              0
  30                                                       false


Back to: