\documentstyle{article} \input{/u/ola/lib/wide} \input{/u/ola/lib/para} \begin{document} \begin{center} \Large \bf Lab 9: CPS08: Arrays, Vectors, Histograms \end{center} You should work on this assignment on mentor. It is on the acpub system as well, but you should get used to working on mentor during lab. It would be prudent, however, to back things up by ftp-ing files to your acpub account. If you can't log into mentor, get help NOW! \subsection*{Logging in to mentor} \begin{enumerate} \item From an xterm window type {\tt telnet mentor.cs.duke.edu} to connect to the machine mentor. \item Change into your {\tt cps08} directory and create a {\tt lab9} subdirectory. Then change into this and copy files by typing what's below (don't forget the trailing dot). \begin{verbatim} cp ~ola/cps08/lab9/* . \end{verbatim} If you type {\tt ls} you should see the following files: {\tt Makefile}, {\tt rollem.cc}, {\tt words.cc}, {\tt letters.cc}, {\tt histo.cc} and {\tt histo.h}. \item You need to give your machine permission for mentor to connect to it. To do this type {\em in an xterm on the DEC station} NOT in a mentor xterm the following: \begin{verbatim} xhost +mentor.cs.duke.edu. \end{verbatim} Do NOT put a space after the + sign. \item You should then be able to type {\tt 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 {\bf Acpub Alternative} below for completing the lab on the acpub machines. You should check with a TA since all students should have working mentor accounts. \end{enumerate} \subsection*{Acpub Alternative} Change into your {\tt cps08} directory using the {\bf cd} command and create another directory called {\bf lab9} using the {\bf mkdir} command. Change into the {\bf lab9} directory. If you did this correctly, when you type {\bf pwd} you should see a long path name that ends with {\bf /cps08/lab9.} You need to copy some files to do this lab. The {\bf 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): \begin{verbatim} cp ~ola/cps08/lab9/* . \end{verbatim} This command will copy all the files in the directory \verb!~ola/cps08/lab9! into your {\tt cps08/lab9/} directory. \subsection*{Lab} First make a link to several text data files. To do this type \begin{verbatim} ln -s /u/ola/misc/data data \end{verbatim} Then you'll be able to use the directory {\tt data} to access several big text files. (On acpub, the path is \verb!~ola/data! rather than \verb!~ola/misc/data!). \subsection*{Counting Letters} Use the emacs commands {\tt C-x C-f} to find/load the file {\sl letters.cc}. Then compile this command using {\tt C-x c} and type {\tt make letters} in the minibuffer. You'll be prompted for the name of a file. Enter {\tt data/poe.txt} and the program will print a histogram of all the letters in Edgar Allan Poe's ``The Cask of Amontillado''. How do you know if the data is correct? Run the program again and type {\tt letters.cc} for the name of the file. The counts in parentheses are correct, but the bars are NOT scaled properly. You need to add some code to the {\tt histo.cc} file for the function {\tt Display}. In its current version, there is a {\tt for} loop that has no body. This loop is supposed to find the maximal (largest) entry in {\tt counts} and set the variable {\tt max} to this value. You must write code in this loop to compare all entries of {\tt count} to {\tt max} and reset {\tt max} when necessary. When you've done this, you should run the program using as an input file {\tt data/shakespeare/romeo}. How many times does the letter 'e' occur in the play? \underline{\hspace*{0.5in}} How many times does the letter 'z' occur and why is the bar for the letter 'z' as long as it is? \hspace*{1in} \subsection*{Counting Dice Rolls} You should now load the file {\tt rollem.cc} into emacs using {\tt C-x C-f}. Do NOT start another emacs session. The program is designed to track how many times each combination of dice rolls occurs. There are two arrays defined, but the loop to roll the dice and update the appopriate array entry is missing. You are to add a loop that that ``rolls'' two ``dice'' the specified number of times and updates the appropriate entry in the array {\tt counts}. Then compile and execute the program for 6-sided dice and 12-sided dice, rolling the dice 20,000 times. If you think the output is ``wrong'' make some noise. How many times does the sum 23 occur when you roll two 12-sided dice 20,000 times \underline{\hspace*{1in}} ? \subsection*{Walk} You are to write a complete program called {\tt walk.cc} that simulates a two-dimensional random walk. In this simulation a ``frog'' (or other random walker) starts at the origin (0) and goes left or right one step, at random (use a 2-sided dice). The logic for this is shown below. \begin{verbatim} for(k=0; k < numSteps; k++) { if (die.Roll() == 1) position += 1; else position -= 1; } \end{verbatim} Here the variable {\tt position} keeps track of where the frog is. You are to write a program that does this, but keeps track of how many times the frog visits each location in the range -100 to 100. Don't worry about keeping counts for positions outside this range. To do this, you'll need a vector of size (at least) 201, and you'll need to think about the expression you use to index the array. Use this program to track a random-walk of 1,000 steps and 10,000 steps. In each case, display a histogram of how many times each position in the range -30 to 30 is visited. \subsection*{Words} This is extra. You do NOT need to do this part of the lab, but if you do you can earn extra lab points. The program {\tt words.cc} is designed to keep track of how many times each word in an input file occurs (not letters, complete words). The function {\tt Store} is NOT currently implemented. You should write this function, search the entire vector {\tt wordList} for the string {\tt word}. If the word is already in the vector, you don't need to do anything. If the word is NOT in the vector, it should be added as the last entry and the count of the number of words updated. If you do this correctly, the number of unique words in ``The Cask of Amontillado'' should be 1,000. How many unique words are there in ``Macbeth'' (this will take a long time to determine). For extra extra credit, modify the program {\tt words.cc} so that it keeps track of how many times each word occurs (not just the unique words, but how many times each unique word occurs). The easiest way to do this is to use a struct: \begin{verbatim} struct WordInfo { String word; int count; }; \end{verbatim} and then define \verb!Vector wordList(10000);!. \subsection*{Submission} {\bf To turn in programs}: When your modified programs 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. You can type {\tt make submit} to submit the appropriate files, or you can type the command below (this doesn't include {\tt words.cc}). \begin{center} \tt submit08 lab9 README rollem.cc walk.cc histo.cc \end{center} The README file should include your name, how long you worked on the program, and anyone you received significant help from. You should also include any comments you have about the lab. You should receive a message telling you that the programs were submitted correctly. \end{document}