CPS08: Lab4 Simulation, Classes Hopefully you'll be using mentor for this lab (but things have been acting weirdly). The operating system on mentor is different than on the DEC stations so you'll need to get used to a few things. In case mentor isn't working properly this lab can be completed on both the DEC stations and on mentor. The same instructions will work in both places. If you can't log into mentor, log into an acpub machine and do your work there. *Logging in to mentor From an xterm window type telnet mentor.cs.duke.edu to connect to the machine mentor. You'll be prompted for your login and password so you should supply these. If you haven't changed your password from the default given you, change it now. To do this, type passwd. You'll be prompted for your old password and your new password. After changing your password you should run the command ~ola/setup! (just type that). This will set up your files so that you can work more easily. ************************ You MUST run ~ola/setup or your environment won't be right. (You only need to run it once). After running it, log out and log back in (to log out type logout). ********************************** 1. Create a cps08 directory (if you haven't already) using the mkdir command. There's no method for setting individual permissions on this directory as we did on the acpub machines, we'll try to fix this later. 2. Change into this directory and create a lab4 subdirectory. Then change into this and copy files by typing what's below (don't forget the trailing dot). 3. If you type ls you should see the following files: Makefile, randwalk.cc. 4. You need to give your machine permission for mentor to connect to it. To do this type in an xterm on the DEC station NOT in a mentor xterm the following: xhost +mentor.cs.duke.edu Do NOT put a space after the + sign. You should then be able to type emacs & to get an emacs window. You want to use the ampersand & which leaves you with control of your xterm (so you can run programs from it). If this doesn't work, skip to the description of Acpub Alternative below for completing the lab on the acpub machines. *Acpub Alternative Change into your cps08 directory using the cd command and create another directory called lab4 using the mkdir command. Change into the lab4 directory. If you did this correctly, when you type pwd you should see a long path name that ends with /cps08/lab4. You need to copy some files to do this lab. The cp command is used for copying files on Unix systems. Type the following command to copy files for the lab (don't forget the trailing period, or dot): cp ~ola/cps08/lab4/* . This command will copy all the files in the directory ~ola/cps08/lab4 into your cps08/lab4/ directory. If you type ls you should see the following files: Makefile, randwalk.cc. ****** *Lab 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. 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. *********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 from the xterm window on mentor type gnuplot to run a plotting program. When you have the gnuplot prompt, type plot "walk.out" (you'll need the quotes). This will plot the output. To quit gnuplot type 'quit' at the prompt. *************Many simulations Rather than one simulation, you should now add a for 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 times you can get a more reliable estimate on the simulation results. To track the reults, define a double variable named posTotal and use it to accumulate the sum of all the final positions. This is partially shown below. for(k=0; k < numSimulations; k++) { Walk(numSteps,position,maxRight); posTotal += position; } cout << "average position = " << posTotal/numSimulations << endl; You should also include a variable so that the average maximally-right position for all the simulations is calculated and printed too. 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? *************Maximally Left Finally (if there's time), 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. *Printing the program. Practice printing. To print the program follow the steps below. Create a lab4 subdirectory in your cps08 directory on your acpub account. Change into this directory and then type ftp mentor.cs.duke.edu (you'll be prompted for your password). From the ftp prompt ftp> you should change into your cps08/lab4 directory using the cd command: cd cps08/lab4. You can type ls to see what's there. Now you want to get the modified randwalk.cc program. To do this type get randwalk.cc. Check to see if you got the file in another xterm (you can't check with ftp active). If you retrieved the file, you can type quit to get out of ftp. You can also practice ftp'ing from mentor to the acpub machine. The same protocol is followed, but from mentor you would type ftp teer5.acpub.duke.edu for example, and you'd use put instead of get. Print the file randwalk.cc by typing enscript -2rG -Pteerlp1 randwalk.cc. If this doesn't work ask a TA. (You can substitute another printer for teerlp1). The -r option may cause an error message, but it still works. To turn in programs: When your modified randwalk.cc works, you can submit it, along with a README describing how long it took you and what your impressions of the lab are. Submit works on the DEC stations and on mentor. submit08 lab4 randwalk.cc README The README file should include your name, how long you worked on the program, and anyone you received significant help from. You should receive a message telling you that the programs were submitted correctly.