CPS 6: Lab #6

Counting words and CDs

6 points

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

This command will copy all the files in the directory ~ola/cps6/lab6 into your directory for you to use. If you type ls you should see the following files: Makefile, fingerp.cc and cdread.cc.

You should also create a link to the data directory with text files in it. To do this type
ln -s ~ola/data data
Then you should be able to type ls data to see many files including several of Shakespeare's plays and files of CDs.

Part 1: Getting Shakespeare's Bacon

Use the emacs commands C-x C-f to find/load the file fingerp.cc . Then compile this command using C-x c and type make fingerp in the minibuffer. Run the program from an xterm window to see that it works. You should enter, for example, data/hamlet.txt to count words in Hamlet .

You are to modify the function FingerPrint so that it keeps track of, and returns via another reference parameter, the average length of all the words read. Add one more parameter to FingerPrint and call it appropriately. 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.

What is the average word-length of the words in Hamlet _____ ? (Put answer in README file).


Part 2: CD Superstore

Get the file cdread.cc into emacs and compile it by typing make cdread after typing C-x c in emacs. This program is designed to read information stored in a file in the format
  price artist title
for example, part of a sample data file is shown below.
  11.98  Nine_Inch_Nails  Broken
  14.98  Violent_Femmes  Violent_Femmes
  14.98  Enya  Enya
  15.98  John_Elton  Greatest_Hits_1976-1986
  14.98  SWV  It's_About_Time
  14.98  Gill_Vince  Best_Of_Vince_Gill
  15.98  Tillis_Pam  Homeward_Looking_Angel
  14.98  Fox_Samantha  Greatest_Hits
  14.98  R.E.M.  Murmur
  14.98  R.E.M.  Reckoning
There are NO spaces in this file except between items on a line. This means that Elvis is stored as Presley_Elvis , note that the last name comes first and that an underscore is used to separate first and last names (as well as words in a title).

Run the program and enter data/lab6cd.dat when prompted for the name of a file --- this file stores information for more than 1000 CDs. When run all the CD artists/titles are printed. You are to modify the program to do three things.

  1. Allow the user to enter the name of an artist. The program should print ONLY those CDs by the artist and it should total the prices of all such CDs as well as printing the number of CDs found. For example:
       search for: The_Smiths
       1. The_Queen_is_Dead
       2. This_Charming_Man
       3. Louder_Than_Bombs
       4. Strangeways_Here_We_Come
       total # cd's = 4, total price = $ 59.92
    
  2. Allow the user to continue to search for CDs by entering artists until a special sentinel value is entered (you can decide on the sentinel value). With this modification you will NOT need to re-run the program each time you want to search for a new artist.

    In order to re-read a file, you'll need to include two lines to reset the ifstream to the beginning. These lines are shown below. You should put these lines just before the while loop that reads the data.

        input.clear();       // clear any failed flags
        input.seekg(0);      // reset to beginning of stream
    
  3. You should move the code that reads the file to a function. This function should have the prototype shown below.
        void Process(ifstream & input, string searchKey,
                     int & count, double & total)    
    
    The stream is passed in (it MUST be passed by reference) and the search key is passed in (the artist to look for). The reference parameters count (number of CD's by artist) and total are passed out of the function Process .

Submission

Make sure your README file contains 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 lab6secN README fingerp.cc cdread.cc