This assignment is due at 9 PM on Wednesday, February 21st, 2001. The assignment is a written one, and must be submitted as a single file, a README. Refer to the homework policies page for general homework guidelines.
This assignment has only one purpose, which is to familiarize you with interactive debuggers.
The program assign_05
is a simple file shuffler. It
reads lines from a file (provided on the standard input device,
cin
), shuffles them into a random order, and writes them
to its standard output, cout
. Although it is quite
simple, the program can be used to perform relatively complex tasks.
For example, the class summary
schedule was produced by running the CS70 class roster through a
C version of the same program. A random line can be chosen from a file such
as /etc/passwd
with the command:
./assign_05 /etc/passwd | head -1
To the best of my knowledge, the program is free of bugs. However, for instructional reasons it is much less efficient than it could be.
For this assignment, you will run the program under an interactive debugger and follow a list of steps. Most steps will require you to record the answer to a question. The final result will be submitted as a README file. Other than a file header (name, date, etc.), the README should not have "documentation" components such as the program purpose or input format. Please number your answers corresponding to the step in which the question is asked, leaving gaps if a step asks no question.
The assignment is written for use with the interactive debugger
ddd
on Turing.
It is not possible to do the assignment with a different debugger,
primarily because minor debugger variations make grading impossible.
Also, it is probably not possible to do the assignment by using a
Windows machine to connect to Turing, because ddd expects that you
will be connecting from an X-compatible display, and to the best of my
knowledge Windows machines can't do that securely.
The program for the assignment is implemented using 4 files: a Makefile, a main program (assign_05.cc), and a header file (line.hh) and implementation (line.cc) for a pair of classes that support some simple operations on text lines. There is also a sample input file (hw5-input.txt). As usual, you can download all of the files as a gzipped tar file or a zip archive.
The assignment is divided into several sections. You should perform the sections in order. However, each section is independent, so that you can log out and come back later, or go back to the start of a section if you think you have gotten things out of whack. If you log out and come back, you will need to repeat the steps listed under "Setup".
make
".
echo $EDITOR
". If the response is the name of
some other editor, fix it by typing "setenv EDITOR
vi
", "setenv EDITOR pico
", or whatever is
appropriate to your taste.
(Note: these commands assume you use tcsh
as your
shell. If you use bash
, use
"export EDITOR=vi
" and so forth.
ddd
debugger
from the command line by typing "ddd assign_05
".
Run
button won't work in this case,
because the program needs arguments. Instead, explore the
menus to find a way to run the program that will pop up a
window asking for arguments. Specify
"-d
" in the window and let the program run.
argv[argNo][0]
. Sweep out this expression
with the mouse
(be sure to include both sets of brackets, and no more. Use
the toolbar to display it. Question:
What is shown in the top sub-window?
argv[argNo]
.
Question: What is it?
argNo
and argc
?
argc
is the size of
argv
. As you know, if a C++ array
foo
has 5
elements, it's illegal to refer to foo[5]
.
Question: Why did the program violate that rule?
Edit
button to bring up your
favorite editor, and fix the bug. Exit your editor and click the
Make
button. Then click the green
Run
button to try it again with the same arguments.
A large number should appear in the bottom window.
Question: What
is it? (You may wish to use cut-and-paste to make sure
you copy it accurately.)
Undisp
button
on the toolbar. Repeat for the other display. You should
now be back to two windows.
/foo/bar
/baz/zap
". This should cause the program to try to open
two nonexistent files by those names.
Question: What output do you
see in the bottom window? Be sure to scroll back to make
sure you see everything after the "Starting program:" message.
doShuffling
. You can find it quickly by
typing that name in the small window on the toolbar (the
one labeled "()") and then clicking Find
.
Next
button is a good way to work your
way through the function one line at a time. Hit
Next
until you get to the statement
"ifstream nextFile(argv[argNo]);
". The
constructor argument is the name of the file to open.
Question: What file is it trying to open?
Up
button to see the
line that called us. Display argv[argNo]
.
Question: What is its value?
Down
button to get back where we
were. Question: Why is the value of
argv[argNo] different?
Next
button a few times to get to the place
where the bug happened. There seems to be a bug in DDD,
so if the value of argv[argNo]
isn't
automatically displayed, try hitting Up
and
the Down
to make it show up.
Question: What is
argv[argNo]
?
Cont
("continue") button to continue
running the program at full speed. You should see two
error messages in the bottom window.
ddd
to get rid of your breakpoints and
display commands. Alternatively, you could use
Source/Breakpoints...
and
Data/Displays...
to achieve the same effect.
File/Open Source...
to bring up a list of
the source files involved in the program. Most of them are
system files that aren't of interest to us, so find
line.cc
and open it.
LineGetter::getLine
,
by scrolling or searching. Then find the call to
expand
.
expand
call.
-s 0 assign_05.cc
".
Next
button.
Question: What happens?
Step
button.
Question: What happens?
Source/Breakpoints...
to disable or
delete the breakpoint in getLine
.
-s 1
hw5-input.txt
". Question: Is
the output random? (You may wish to compare the output to
the contents of the input file.)
randomize
function
and run the program again. If necessary, use
Next
to get to the first statement of the
for
loop.
array
. Question:
What is its value?
array
.
Question: What happens?
Show
All
from the pulldown Show
menu on the
toolbar. Question: What is the value of
M_start
?
M_start
and choose
Display/Other...
on the toolbar. If
necessary, add an asterisk at the beginning of the
expression in the dialog box, and add "@5
" at
the end before displaying. Question:
What is displayed? (You may have to scroll around a bit.)
Show
. Question: What
do you now see?
Next
button to step through one
iteration of the
loop. Question: Do the values in the
array change?
swap
line, run the
program again, and hit
Cont
. Question: What are
the values of i
and j
?
swap
function interchanges two
values. It should be fairly obvious what is wrong here.
If not, look at the comments at the beginning of
randomize
.
Fix the bug and compile the program again. (Incidentally,
the rather odd "(*array)[i]
" notation is
needed because array
is a pointer to an STL
vector
, and the [i]
needs to be
applied to the vector
rather than the
pointer. The notation wouldn't be necessary if we had
passed array
by reference instead.)
swap
line in randomize
,
and nowhere else. If you're starting fresh, you'll have
to add it; if you're continuing from part F, you'll need
to delete the first breakpoint in randomize
.
Run the program again (with the arguments "-s 1
hw5-input.txt
"). What are the values of
i
and j
when you hit the breakpoint?
array
displayed (you may have to repeat some
or all of steps 31-35). Scroll around or resize your
display window so that you can see the box that has line
number 2 in it. Use Next
to go past the swap
function. Question: What is in the box
now?
Next
,
Step
, or Cont
a lot of times,
you can save trouble by typing in the command (bottom)
window. Try this by first clicking Cont
to
get back to the breakpoint, and then typing c
5
(only the first letter of the command is needed
for these often-used operations).
Question: What happens to the first two
elements of array
?
hw5-input.txt
". You will see 16 words
scroll by in
the window at the bottom of the screen. Question:
In order, what are
they? (You may use cut-and-paste, and you don't have to list
them one per line).
The only file you need to submit for this assignment is the README.
Please use cs70submit
to submit it, rather than
cs70submitall
, so that only the README will get picked
up. If you use cs70submitall
, you will merely clutter up
the grader directories.
Don't forget to spell-check your file.
Assign_05.cc
deliberately uses a somewhat
inefficient data structure for illustrative purposes. If you
ever expect to use it in the future, you should change randomize
so that the vector
stores pointers to lines, rather than
actual lines.
Don't worry if you don't know how to do that right
now; by the end of the course you'll be a pointer expert.
© 2001, Geoff Kuenning
This page is maintained by Geoff Kuenning.