CPS 6: Lab #7

Summer 1999

Vectors, Classes, Searching

6 points

Due: Tuesday, June 22 - 11:59pm

This lab will provide practice using output streams, vectors, structs, and modifying classes. Change into your "cps6" directory using the "cd" command and create another directory called "lab7" using the "mkdir" command. Change into the "lab7" directory. If you did this correctly, when you type "pwd" you should see a long path name that ends with "/cps6/lab7"

In order to do this lab, you need to copy some files using the following "cp" (for "copy") command (don't forget the trailing period, or dot):

       cp  ~dr/cps6/lab7/*  .

You'll need to link to datafiles by typing the following command:

    ln  -s  ~dr/cps6/data  data

If you type "ls" to list the files now in your lab7 directory, you should see the following files: Makefile, data, testfile, ctimer.h, and allword.cc.

For each of the programming problems that follow, you should use the style rules discussed in class, which includes indentation, meaningful variable names and comments at the top of the file and for each function.

Part 1 - Output Streams

Compile the program allword.cc. Run the program and enter the name of the file testfile

The program will keep track of all the unique words in the file testfile . The program stores each word in a vector in sorted order, and checks to see if a word is already present in the array before adding it again. The program prints all the words, the number of unique words, the total number of words, and the total time of the update (searching to see if a word is already in the Vector, and if not, inserting it into the vector in sorted order).

Run the program again and enter the file: data/poe.txt

In its current version, the program prints all the words to the screen. Modify the Print function in the WordList class so that the words are printed to a file whose name you enter. You'll create a new output stream to send output to a file.

Add the following to the WordList::Print function

    ofstream output;
    string filename;
    cout << "output file: ";
    cin >> filename;
    output.open(filename);

Now, in the Print function, use output instead of cout to write the words to the specified file.

Compile and run the program again. When it is correct, Show your TA how to run the program and put the words from Poe's cask into the file poe.out. Also show your TA the first 10 words in alphabetical order in Poe's "cask".

Part 2a - Timing

Note that the program currently shows the total time used in the Update Function which includes the total time for searching and the total time for inserting. A timer is declared and used in the main function. The CTimer class is in the textbook and also in the file ctimer.h in your directory.

Add a timer to the WordList class to time the total search time.

Compile and run your program with the file: data/poe.txt

What is the total update time for the words in Poe's ``cask'' __________________? What is the total search time for the words in Poe's ``cask'' ________________ ? Put these answers in your README file.

Run your program with the data file data/melville.txt (this may take awhile)

What is the total update time __________________? What is the total search time ________________ ? Put these answers in your README file.

Part 2b - Binary Search Timing

So far, the search we have been using is sequential search. There is also a function for Binary Search already in the file allword.cc, named BinSearch. Since the words in the Vector are in sorted order, you can use this searching method.

In the Update function, change the call

    int loc = Search(word);
to
    int loc = BinSearch(word);

Compile and run the program again with the file: data/poe.txt

What is the total update time for the words in Poe's ``cask'' __________________? What is the total search time for the words in Poe's ``cask'' ________________ ? Put these answers in your README file.

Run your program with the data file data/melville.txt

What is the total update time __________________? What is the total search time ________________ ? Put these answers in your README file.

Part 3 - Word Counting

Modify allword.cc to print beside each word the number of times this word appears in the text. This information should also be written to the output file.

The class "WordList" contains a vector of strings. You're to change it so that it uses a vector of type "Info" defined below. (Do NOT forget the semi-colon after the } !!).

Define this struct before the class "WordList" and change the vector definition to "Vector<Info> myList".

    struct Info
    {
        string word;
        int count;
    };

Try to compile the program. You'll get errors due to the change in the definition. For example, in "Search" you'll need to change the line:

    if (myList[k] == key)

to

    if (myList[k].word == key)

so that the "word" field is compared to key. In the "Update" function, if the word is already in "myList" then the count of how many times it occurs should be incremented.

In Update, if "key" is NOT in "myList", the new word should be added to "myList" with a count of 1.

You'll need to make other changes as well.

How do you know if your program works correctly? You should first test it on the small file testfile.

Run your program on Poe's ``cask''. How many times do each of the first five words in alphabetical order occur ________________ ? Put these answers in your README file.

Part 4 - Words Occuring Most Often

In this part, you'll print to the screen the words that occur the most often (there may be only one such word).

How do you know if your program works correctly? You should first test it on the small file testfile.

Run your program on Poe's ``cask''. Which word(s) occurs the most often ________________ ? Put this answer in your README file.


Submission

When your program compiles and produces the correct output, add to your README file your name, section number, the date, how long you worked on these programs, and anyone you received significant help from. You can then turn everything in by typing:

            submit6 lab7 README allword.cc

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