CPS08: Lab5 Reading Files, Timing 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 (more on this later). 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 only need to run the set program once. 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. Change into this directory and create a lab5 subdirectory. Then change into this and copy files by typing what's below (don't forget the trailing dot). cp ~ola/cps08/lab5/* . If you type ls you should see the following files: Makefile, wordcheck.cc, power.cc. 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. You should check with a TA since all students should have working mentor accounts. ******Acpub Alternative Change into your cps08 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 /cps08/lab5. 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/lab5/* . This command will copy all the files in the directory ~ola/cps08/lab5 into your cps08/lab5/ directory. If you type ls you should see the following files: Makefile, wordcheck.cc, power.cc. ***********Lab Use the emacs commands C-x C-f to find/load the file wordcheck.cc. Then compile this command using C-x c and type make wordcheck in the minibuffer. The program is designed to read from a file specified by the user. To get access to Shakespeare's plays (without typing a full pathname) type the following: ln -s ~ola/misc/data/shakespeare shake This will create a link from a directory I have to a directory you've just created called shake. After typing this, type ls shake to see a list of the plays. You can also type the following sequence of commands to go to a directory, but ``remember'' where you came from. pushd shake ls pwd dirs pushd pwd dirs pushd pwd popd ls pwd You should be able to explain the purpose of all the commands above. If you need help figuring them out, please ask. You should be in your lab5 subdirectory. Type wordcheck and enter shake/hamlet when prompted for a file. How many words are there in Hamlet ____________ ? ********More statistics You'll calculate some statistics for Shakespeare's plays by modifying the wordcheck.cc program. First you'll calculate the average word length. To do this you'll need to add up the number of characters as well as the number of words. The number of characters in word can be determined using word.Length(). The function Length() returns an int, so if you create another counter you'll be able to total up the number of characters as well as the number of words. When you've done this, include an output statement like the one below: cout << "average length = " << double(numChars)/numWords << endl; Using double like a function is called a typecast and makes the division double division instead of integer division. *************Common Chars If you include the proper header file, you can determine whether a character is a vowel or a consonant. To do this, you'll need #include "charutils.h" at the top of your program. This gives you access to three functions whose prototypes are shown below. // utility boolean functions to determine // if a letter is a vowel, consonant, or "common" // // Owen Astrachan, 2/12/95 extern bool IsVowel(char ch); // postcondition: returns true if ch is vowel (a,e,i,o.u) extern bool IsConsonant(char ch); // postcondition: returns true of ch is consonant (not vowel) extern bool IsCommon(char ch); // postcondition: returns true if ch is 'common' letter // (etaoinshrdlu) (ignore, for now, the word extern). You are to write a function named NumCommon, you'll need to change the version included in the program. In the program, the function counts the number of times an 'a' appears in the string s. Note that the brackets [] are used to access a character in a string and that the characters in "cat" are s[0], s[1], and s[2] (if s represents the string "cat".) Change it to count the number of common characters in a string by calling IsCommon instead of comparing to 'a'. Then use the function to calculate the percentage of common letters (to all letters) in both Macbeth and Much Ado About Nothing. Write these percentages here: ***************Math Functions You should now load the file power.cc into emacs (C-x C-f), then compile it (C-x c). The program will calculate the result of raising where the numbers are both integers and are entered by the user. However, instead of using the type int, the type Integer is used. This type is part of g++, it is NOT a standard, built-in type. Run the program and use to calculate . How many digits are there in the answer __________? Write a function that can be used to determine the number of times a given digit (character) occurs in a string. The prototype is given below. int NumOccurs(String s, char ch) // postcondition: returns number of times ch occurs in s The call NumOccurs("terrible",'r') should evaluate to 2. Write the function (you'll need to use [] to get at individual string characters). Then use the function to determine how many times the digit '0' occurs in , write the answer here ____________ Then find out how many times each of the digits '0' '9' occurs in this number by using a loop that calls NumOccurs 10 times and prints the answers appropriately. You can receive extra credit for the lab (that can be counted towards missed points on other labs) by writing a function to computing , factorial. where , e.g., . You should then use your functions to calculate how many times the digit '3' occurs in . Write the answer in your README file. ***********Printing the program. Practice printing. To print the program follow the steps below. Create a lab5 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/lab5 directory using the cd command: cd cps08/lab5. You can type ls to see what's there. Now you want to get the modified wordcheck.cc program. To do this type get wordcheck.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 wordcheck.cc by typing 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. To turn in programs: When your modified programs word, 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 lab5 wordcheck.cc power.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 also include any comments you have about the lab. You should receive a message telling you that the programs were submitted correctly.