\documentstyle{article} \input{/u/ola/lib/wide} \input{/u/ola/lib/para} \begin{document} \begin{center} \Large \bf Lab 10: CPS08: Arrays, Sorting, Searching \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 lab10} subdirectory. Then change into this and copy files by typing what's below (don't forget the trailing dot). \begin{verbatim} cp ~ola/cps08/lab10/* . \end{verbatim} If you type {\tt ls} you should see the following files: {\tt Makefile}, {\tt words.cc}, {\tt logstuff.cc}, {\tt logins.h}, {\tt logins.cc}, {\tt logins.mentor}, and {\tt logins.acpub}. \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, check with a TA since all students should have working mentor accounts. \end{enumerate} \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. You may also want to remove the file {\tt logins.acpub} because it is big (the remove command is {\tt rm}). You can link to this file using \begin{verbatim} ln -s /u/ola/cps08/lab10/logins.acpub logins.acpub \end{verbatim} then you can refer to {\tt logins.acpub} as though it is in your directory. \subsection*{Counting Words} Use the emacs commands {\tt C-x C-f} to find/load the file {\sl words.cc}. Then compile this command using {\tt C-x c} and type {\tt make words} in the minibuffer. You'll be prompted for the name of a file. Enter {\tt data/poe.txt} and the program will keep track of all the unique words in Edgar Allan Poe's ``The Cask of Amontillado''. The program stores each word in an array, and checks to see if a word is already present in the array before adding it again. The program prints how long reading takes, and how long sorting the words read in takes. How long does it take to sort the words in Poe's ``cask'' \underline{\hspace*{1in}}? How long to read them \underline{\hspace*{1in}}? In its current version, the program prints all the words to the screen. You are to change this so that the words are printed to a file whose name you enter. Replace the call \verb!list.Print(cout)! with the code sequence below. {\small\begin{verbatim} ofstream output; cout << "output file: "; cin >> filename; output.open(filename); list.Print(output); \end{verbatim}} Now run the program again. Find the alphabetically fifth word in Poe's ``cask'', what is it \underline{\hspace*{1in}}? \paragraph{Word Counting} The struct {\tt WordList} contains a vector of strings. You're to change it so that it uses a vector of type {\tt Info} defined below. ({\bf Do NOT forget the semi-colon after the \} !!}). \begin{verbatim} struct Info { String word; int count; }; \end{verbatim} Define this struct before {\tt WordList} and change the vector definition to \verb!Vector list!. Try to compile the program. You'll get errors due to the change in the definition. For example, in {\tt Search} you'll need to change the line: \begin{verbatim} if (mylist[k] == key) \end{verbatim} to \begin{verbatim} if (mylist[k].word == key) \end{verbatim} so that the \verb!word! field is compared to key. In the {\tt Add} function, you'll need to check first to see if the word is already in mylist (call {\tt Search} from within Add). If the word is in the mylist ({\tt Search} returns its index), then the count of how many times it occurs should be incremented. The call to {\tt Search} is just \verb!index = Search(word)! (where {\tt index} is a local variable). This is an example of one member function calling another member function. If it is NOT in mylist, the new word should be added to the end of mylist and its {\tt count} field set to 1. You'll need to make some changes in {\tt Sort} and {\tt Print} as well, and then change the {\tt while} loop in {\tt main} so that the only two statements in the loop are: \begin{verbatim} word = word.Downcase(); list.Add(word); \end{verbatim} since a word is always ``added'' to the list (but adding sometimes bumps a count and sometimes adds a new word). After the program works, change the {\tt Sort} function so that it compares {\tt count} fields of each item in the list rather than using an alphabetic comparison. Doing this requires changing ONLY the line that compares \verb!list[k]! and \verb!list[min]!. Then rerun the program with Poe's cask and determine what the top 5 most frequently occurring words are. Write them down here: \vspace*{0.5in} You should run the program on {\tt data/melville.txt} as well, this should take longer. Write the top 5 most frequently occurring words from this file too. \vspace*{0.5in} \subsection*{Login Search} Now load the file {\tt logstuff.cc}, compile it, and run it. When prompted for the name of an input file, enter {\tt logins.mentor}, a file that contains all names and logins of users on the mentor system. Search (using the 'l' command) for yourself by entering your login. Then search using the 'n' command for anyone with your name. Search again using the 'n' command for anyone with 'owen' as part of their name. How many such users are there \underline{\hspace*{1in}}? Then quit the program, and run it again but use {\tt logins.acpub} as the database of logins. This is a list of everyone with a login on the acpub system. How many users are there with 'owen' as part of either their first or last name and how long does it take to make this search \underline{\hspace*{0.5in}}? You should study the programs {\tt logstuff.cc} and {\tt logins.cc} carefully since they can help with the CD data base assignment. You may want to print these files. \subsection*{Printing the program} Practice printing. To print the program follow the steps below. \begin{enumerate} \item Create a lab10 subdirectory in your cps08 directory {\bf on your acpub account}. Change into this directory and then type {\tt ftp mentor.cs.duke.edu} (you'll be prompted for your password). \item From the ftp prompt \verb!ftp> ! you should change into your {\tt cps08/lab10} directory using the {\tt cd} command: {\tt cd cps08/lab10}. You can type {\tt ls} to see what's there. \item Now you want to get the file {\tt logins.cc}; to do this type {\tt get logins.cc}. \item Check to see if you got the file in another xterm (you can't check with ftp active). \item If you retrieved the file, you can type {\tt quit} to get out of ftp. \item You can also practice ftp'ing from mentor to the acpub machine. The same protocol is followed, but from mentor you would type \begin{verbatim} ftp teer5.acpub.duke.edu \end{verbatim} for example, and you'd use {\tt put} instead of {\tt get}. \item Print the file logins.cc by typing {\tt enscript -2rG -Pteerlp1 wordcheck.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. \end{enumerate} \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 lab10 README words.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}