due on Sunday, 2/19 before 11:59 PM EST.
Sue Evans & Patti Ordóñez
Hit the space bar for next slide
One of the most important skills that a programmer can develop is the ability to trace code. Tracing code requires walking through your program step by step and examining the content of the variables at each step to determine the final outcome of the program.
For your first assignment, you are to trace the chaos program shown below. You write out the values of each variable at each step of your program in a table. You should also show what the program is printing out at each step. Here's an example program and its trace table. It's only necessary to show the value of a variable when it changes. The ~ (tilde) means that the variable doesn't exist yet (it's out of scope).
# File: trace.py # Written by: Sue Evans # Date: 7/23/09 # Section: All # Email: bogar@cs.umbc.edu # Description: A simple program used to illustrate tracing # Inline comments have been intentionally omitted print "This program illustrates tracing" a = 5 b = 2 sum = a + b difference = a - b print "sum = ", sum print "difference = ", difference a = a + 2 b = b + 1 sum = a + b difference = a - b print "sum = ", sum print "difference = ", difference
This is the simple trace table for the code shown above, as it would appear in a plain text file written with emacs.
Notice that there is one column for each variable and a separate one for
the output.
There is one line for each processing step. There should never be more
than one column filled on any one line, except the first line where the
initial state is shown.
a b sum difference output __________________________________________________________________________ ~ ~ ~ ~ This program illustrates tracing -------------------------------------------------------------------------- 5 -------------------------------------------------------------------------- 2 -------------------------------------------------------------------------- 7 -------------------------------------------------------------------------- 3 -------------------------------------------------------------------------- sum = 7 -------------------------------------------------------------------------- difference = 3 -------------------------------------------------------------------------- 7 -------------------------------------------------------------------------- 3 -------------------------------------------------------------------------- 10 -------------------------------------------------------------------------- 4 -------------------------------------------------------------------------- sum = 10 -------------------------------------------------------------------------- difference = 4 __________________________________________________________________________
Even though the chaos program only consists of 5 lines of code excluding the comments, your trace table should contain many more steps than the example trace, since the chaos program contains a looping construct.
Create a trace table for the chaos program shown below. For lines that
require user input, just make up a value (not 0 or 1
).
Show the values of x to 5 decimal places.
# File: chaos.py # Written by: John Zelle # Date: ?/?/04 # Section: 2 # Email: zelle@umbc.edu (not really) # Description: A simple program executing chaotic behavior # Inline comments have been intentionally omitted print "This program illustrates a chaotic function" x = input("Enter a number between 0 and 1: ") for i in range(10): x = 3.9 * x * (1-x) print x
submit cs201 HW1 trace.txt
The submit command has four parts. The word submit, followed by the class, followed by the name of the assignment (note uppercase HW), followed by the name(s) of the file(s) to be submitted. You must be in the same directory as the files you are submitting for the submit command to work properly. The submit command does produce error messages if the command fails and confirmation if the command works properly.
After entering the submit command shown above, you should get a
confirmation that submit worked correctly. Specifically, the confirmation
will say:
Submitting trace.txt...OK
If not, check your spelling and that you have included each of the required
parts and try again.
If you believe that you have typed everything correctly and the submit command fails again, then send your instructor, Mr. Lupoli (slupoli@cs.umbc.edu) or Dawn Block (dblock@umbc.edu), an email about the problem. In this email, please show exactly what you typed in and the exact message that was displayed. We'll be able to fix submit problems quickly.
You can check to make sure your submission went through by entering
submitls cs201 HW1
You should see the name of the file that you just submitted, in this
case, trace.txt.