CPS 6: Lab #5

A Random Walk in the Park

6 points

Create a new subdirectory of cps6 called lab5 using the mkdir command. Change directories into lab5 and copy some files using the following cp command (don't forget the trailing period, or dot): cp ~ola/cps6/lab5/* .

This command will copy all the files in the directory ~ola/cps6/lab5 into your directory for you to use. If you type ls you should see the following files: Makefile and randwalk.cc.

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

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

Part 1: Random Walking

Use the emacs commands C-x C-f to find/load the file randwalk.cc. Then compile this command using C-x c and type make randwalk in the minibuffer. Run the program from an xterm window to see that it works. This program simulates a random walk: a frog starts at the origin of a line and hops randomly left or right (with equal probability). The variable position keeps track of where on the line the frog is located (the frog's x-coordinate).

Note that the indentation of the code is messed up. To fix this, put the cursor on the left curly brace { just below the word main. Then type M-C-q (press escape, let go, then type control-q). This should line up all the code properly.

The first thing you'll do is create a function named Walk to move the code out of main. The prototype of the function (for now) is int Walk(int numSteps); put this prototype at the top of the program, and then write the function below main. To write the function first put an ``empty'' function:

    int Walk(int numSteps)
    // precondition: 0 < numSteps
    // postcondition: returns final x-position of frog after numSteps steps
    {

    }
Then cut and past the for-loop from main into Walk. Move the cursor to the beginning of the section of text (block) you want to copy, then press C-' ' (control-space) or C-'@'. In the mini-buffer you'll see the message 'mark set'. Now move the cursor to the end of the block you want to copy. If you type M-'w' (escape-w) you'll copy the text. If you type C-'w' (control-w) you'll cut the text (remove it). In this case you want to cut it, so type C-'w'. Then move the cursor into the function walk and type C-'y' to yank (paste) the text back.

Replace the cout statement with a statement return position that returns the final position of the frog. Then call the function from main as shown below.

    position = Walk(numSteps);
    cout << "final position = " << position << endl;

Now recompile the program, fix any mistakes, and run it. Remember that to goto a line with an error you can type C-x ` (control-x back quote) and that to goto a line number you can type C-x g then the number in the mini-buffer.

Part 2: Reference Parameters

Now you'll modify the program so that the Walk function computes the farthest position to the right (positive) that the frog reaches. To do this, define a variable maxRight and initialize it to zero. In the body of the loop in Walk, compare the value of position with the value of maxRight. If position is larger than maxRight, then maxRight needs a new value:

if (position > maxRight) { maxRight = position; // new maximal right position }

You'll need to return this value from Walk. Since two values now need to be returned you'll use reference parameters. Change the function prototype to

    void Walk(int numSteps, int & position, int & maxRight);

You'll need to remove the definitions of the local variables position and maxRight in the function Walk since these are now parameters. You'll also need to change how the function is called from main. You'll need to write:

    Walk(numSteps,position,maxRight);
    cout << "final position = " << position << endl;
    cout << "furthest right = " << maxRight << endl;

Make these changes, recompile, and rerun the program.

Once this part of your program is working correctly, remove the &'s in the Walk function (and function prototype), recompile and run your program. Explain what happens and why in your README file.

Now put the &'s back in your program, and make sure your program compiles and runs correctly before moving on to the next section.

Part 3: Plotting Output

This program only does one simulation. You'll make a modification to it to do several simulations, but first you'll make a plot of the output of a single simulation. To do this, add a cout statement in the for loop of Walk.

       cout << k << " " << position << endl;
Now when you run the program it will print the position of the frog at every step. This is too much for humans to read, but fine for a graphing program. Run the program for 100 steps to see that it works. Then rerun the program by typing what's shown below. You won't see a prompt for the number of steps, but enter 1000 (the program is waiting for input).
    randwalk > walk.out
The output of the program is now stored in walk.out. This is called redirecting I/O. Load the file walk.out into emacs (C-x C-f). Remove the first line of the file. It's easy to do this by putting the cursor at the beginning of the line (C-a) and typing C-k (control-k). Now go to the end of of the file. To do this type M-> (that's escape >). Remove the output lines that contain words. The file should now be just a list of 1000 pairs of numbers. Save the program (C-x C-s).

Now type gnuplot to run a plotting program. When you have the gnuplot$>$ prompt, type plot "walk.out" with lines (you'll need the quotes). This will plot the output. To quit gnuplot type 'quit' at the prompt.

Part 4: Many simulations

Running one simulation doesn't mean your program is correct, so in this part you'll run many simulations and calculate the furthest position to the right, and the average furthest position to the right. You should add a loop in main so that the function Walk is called 100 times (or some other number entered by the user). Each time the function is called, it will run a simulation of numSteps steps. By calling it 100 (or more) times you can get a more reliable estimate on the simulation results.

Run the program for 1000 steps and 1000 simulations (a total of one million steps). How long does it take? What's the average final position and maximally right position? Record this information in your README file.

Part 5: Maximally Left

Finally, modify Walk so that it also determines the maximally-left position attained by the frog. Then run the program again to calculate what this result is for 1000 steps 1000 times.

Submission

Make sure your README file contains your name, how long you worked on these programs, and anyone you received significant help from. When your programs compile and produce the correct output, you can submit them by typing: (N is 1, 2, 3, 4, or 5, your lab section number.)

           submit6 lab5secN README randwalk.cc

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