CPS 6: Lab #4

Time in Shakespeare

6 points

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

cp  ~ola/cps6/lab4/*   .

This command will copy all the files in the directory ~ola/cps6/lab4 into your directory for you to use. If you type "ls" you should see the following files: Makefile and wordcheck.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 wordcheck" will compile "wordcheck.cc" and create an executable called "wordcheck".

Put your answers to the questions below in a README file.

Problems

  1. Five Acts

    Use the emacs commands "C-x C-f" to find/load the file "wordcheck.cc". Then compile this program using "C-x c" and type "make wordcheck" in the minibuffer. The program is designed to read from a file specified by the user. We'll use "wordcheck.cc" to search through some of Shakespeare's plays. To get access to these, type

    ln -s ~ola/data shake
    

    This will create a "link" to a directory you've just created called "shake". After typing this, type "ls shake" to see a list of plays (and other things).

    Now type "wordcheck" and enter "shake/hamlet.txt" when prompted for a file. How many words are there in "Hamlet"? Put your answer in your README file.

  2. 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 << end;
    

    Using "double" like a function is called a "typecast" and makes the division a double division instead of an integer division.

    Compile the program. What is the average number of characters per word in "The Tempest"? Put your answer into your README file.

  3. Searching

    Use the "cp" command to make a new copy of "wordcheck.cc" called "wordsearch.cc".

    Now instead of looking at the total number of words in a play, we're interested in the number of times a specific word occurs. To do this, modify "wordsearch.cc" to prompt the user to enter a string for which to search, then read in that string. Then modify the "while" loop to include a check if the word just read in is equal to the search target. Compile the program. How many times does "time" occur in "Macbeth"? Put this into your README file.

    Will this find instances of the word "Time"? No, since "time" with a lower-case 't' and "Time" with an upper-case 'T' will not be considered equal by C++. There's a member function of string called Downcase(), which returns the string with all upper-case letters replaced by lower-case letters. Add this so that searching for "time" finds all instances of "Time" as well, and add your answer to README.

  4. Timing

    We're interested in how long all of this takes. To do this, we'll use the CTimer class. Go back to editing "wordcheck.cc". You'll need to add "#include "ctimer.h"" to the beginning.

    To use a timer, declare an instance of the class CTimer, for example "CTimer timer;". Right before the "while" loop, add a line that reads "timer.Start()". This will start the timer. After the "while" loop, add another line that reads "timer.Stop()". You can probably guess what this does.

    To find out how long the timer ran, use "timer.CumulativeTime", which returns a double. Add statements to the bottom of your main loop which determine the total time the "while" loop ran for, and the time per word. After you've made all these changes, compile "wordcheck.cc" again. How fast does it count "Hamlet"? How about "The Comedy of Errors"? (Look in errors.txt). Again, put answers in README.

To submit programs:

Make sure your README file contains your answers, 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 lab4secN README wordcheck.cc wordsearch.cc

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