CPS 6: Lab #5

Summer 1999

A Random Walk on the Dock

6 points

Due: Friday, June 11 - 11:59pm

Change into your "cps6" directory using the "cd" command and create another directory called "lab5" using the "mkdir" command. Change into the "lab5" directory. If you did this correctly, when you type "pwd" you should see a long path name that ends with "/cps6/lab5"

In order to do this lab, you need to copy some files using the following "cp" (for "copy") command (don't forget the trailing period, or dot):

       cp  ~dr/cps6/lab5/*  .

If you type "ls" to list the files now in your lab5 directory, you should see the following files: Makefile, gwindow.h, gwindow.cc, gwalk.cc, gwalk.h and frogwalk.cc.

For each of the programming problems that follow, you should use the style rules discussed in class, which includes indentation, meaningful variable names and comments at the top of the file and for each function.

Remember, to compile these programs, use the command make. For example make frogwalk will compile frogwalk.cc and create an executable called frogwalk.

Note on the Window class

DO NOT MODIFY THE WINDOW CLASS!

Part 1: How big is this file?

Copy the files above before doing this part. Show one of your lab TA's the following using Unix commands. These commands have appeared in previous labs.

  1. Show the percentage of disk space you have used up.

  2. Show which file is the biggest file in your lab5 directory.

Part 2: Random Walking

Use emacs to find/load the file frogwalk.cc and lets take a look at it before compiling it.

This program simulates a one dimensional random walk of a frog on a dock using a modified version of the RandomWalk class described on pages 292-296. The modifed RandomWalk class is located in the files gwalk.cc and gwalk.h. Some of the functions have been modified to create an animation of the frog walking. Some additional member functions and variables have also been added.

Looking at frogwalk.cc, a randomly walking frog is created (which also creates a graphic window for the animation), and the new member function DisplayMessage is called which displays the message "ONE DIMENSIONAL RANDOM WALK" at the top of the graphic window. The frog always starts in the middle of the dock and takes its first step by calling the member function First and randomly moves one position right or left (with equal probability) each time by calling the member function Next. The member function IsDone() returns true after the frog has moved the specified number of times. Since this is a one dimensional walk, the frog's y-position never changes.

Compile this program by typing

 make frogwalk 
and run it to watch the one dimensional walk using the Samba animator on high speed and enter 500 in the xterm window for the number of steps. To run it, type:
frogwalk | samba

The frog may go off the dock and keep moving, it may even go off the screen and you won't be able to see it. When the message DONE appears at the top of the graphic window, the frog has stopped moving. If you get tired of waiting for it, you can click on QUIT to remove the Xtango window.

Modification 1: Using Member functions

Modify frogwalk.cc in the following manner.

Now recompile the program, fix any mistakes, and run it to make sure it is correct. As soon as the frog goes off the dock, the frog should halt. If the frog doesn't go off the dock, then it should halt after it has moved the specified number of times.

Modification 2: 2 Dimensional Walk

In this section you will modify two of the member functions in the RandomWalk class. Even though you are modifying gwalk.cc and gwalk.h, you will still type make frogwalk to recompile the frogwalk program. It doesn't make sense to type make gwalk because gwalk.cc doesn't have a main function!

Make the following modifications to convert to a two dimensional walk.

Now recompile the program, fix any mistakes, and run it to make sure it is correct. You should now see a 2-dimensional random walk. Click here to see what it might look like when the frog is moving around. Not too much to see different in a picture, a different message and the y-value of the frog is no longer in the middle of the dock. You'll notice the real difference in the animation, since the frog will now be moving all over the dock.

Modification 3: More Movement and Marking

In this section, you'll make the frog move around more and make a mark where it has been.

Make the following modifications.

Now recompile the program by typing make frogwalk , fix any mistakes, and run it to make sure it is correct.

Modification 4: Messages and Marking

Now recompile the program by typing make frogwalk , fix any mistakes, and run it to make sure it is correct.

EXTRA CREDIT: Many Simulations and Hopping (2 pts)

The extra credit will further modify the files frogwalk.cc, gwalk.cc and gwalk.h. If you do the extra credit, be sure to include a note in your README file that says you did the extra credit so the grader will look for it.

Make the following modifications.

Click here to see what the picture might look like. This has 7 frogs that have already stopped (in red) and one green frog currently hopping.

Submission of programs:

When your program compiles and produces the correct output, create a README file. This file should include your name, section number, the date, how long you worked on these programs, and anyone you received significant help from. Remember to include a note in your README file if you did the extra credit! . You can then turn everything in by typing (where N is your section number (1, 2, ..., or 7):

            submit6 lab5 README frogwalk.cc gwalk.cc gwalk.h 

You should receive a message telling you that the programs were submitted correctly.