CS-183 Lab 1: Using Existing Classes


Overview

The purpose of this lab is to design and implement a stand-alone "console mode" program that incorporates basic C++ programming concepts. These concepts, covered in CS-182, are prerequisites for CS-183.

Acknowledgement

This lab was developed by Dr. Mark Sebern.

Activities

In this lab, you will:

  • Design a program to satisfy a problem statement
  • Write, compile, build, and test the program
  • Report on your results

Problem statement

You have been asked to design and implement a C++ program to analyze a text file. This program will make use of an existing class library containing the single class GameWord. The GameWord class represents a single word from a popular "crossword" board game, and can compute the point value of that word according to the game rules. The definition of this class is in the file gameword.h, while the implementation is in gameword.cpp; both files may be downloaded in the archive gameword.zip. Do not modify the "gameword" files, but do add them to your project.

Your program should:

  1. Prompt the user for the name of the text file to be analyzed.
  2. Open the text file and read it one "word" at a time, stopping when the end of file is reached. Note:
    • For the purposes of this program, a "word" is one or more characters (not necessarily alphabetic) separated by "whitespace" (spaces, newline characters, etc.).
    • The last "word" is guaranteed to be followed by whitespace (e.g., a space or a newline character), before the end of file.
  3. Count the total number of "words" in the text file.
  4. Convert each "word" to lower case.
  5. Store each unique "word" in a vector of string objects. In other words, add the "word" to the vector only if the word is not already present. You will recall that vector is a container from the Standard Template Library (STL) and that string is defined in the standard C++ class library referenced as <string>.
  6. Using the vector of unique "words", create a vector of GameWord objects, with each GameWord object set to the value of the corresponding unique "word".
  7. Sort the vector of unique "words" using the Standard Template library sort algorithm.
  8. Similarly, sort the GameWord vector; note that the GameWord class is set up so the sort will be in ascending order by point value of the words.
  9. Open an output file (first of two) called "words.txt". Write the following data to this file:
    • The sorted lower-case "words", one per line.
    • A blank line.
    • A line reading "Read nn words, mm unique." where nn is the total number of "words" read from the text file and mm is the number of unique "words" stored in the vector.
    • A series of lines of the form "Found nn words of length mm.", where nn is the number of unique "words" containing mm characters. If the longest "word" is ten characters long, then ten lines should be printed (for "word" lengths from 1 to 10); it is possible that the count (nn) for some word lengths may be zero, but print them anyway.
  10. Open a second output file called "values.txt". Write the following data to this file:
    • The point value, and the word itself, of the GameWord objects, sorted as described above.
  11. Write a message to the screen, giving the total number of "words" read from the text file.

For a simple test case, the input text file might look like this:

Software is now a critical element in many businesses,
but all too often the work is late, over budget, or of
poor quality. Society is now far too dependent on
software products for us to continue with the craft-like
practices of the past. It needs engineers who consistently
use effective disciplines. For this to happen, they must
be taught these disciplines and have an opportunity to
practice and perfect them during their formal educations. 

(This quotation, in the file word0in.txt, is taken from A Discipline for Software Engineering by Watts Humphrey.)

The corresponding "words" output file would be (displayed in columns here to save space):

a
all
an
and
be
budget,
businesses,
but
consistently
continue
craft-like
critical
dependent
disciplines
disciplines.
during
educations.
effective
element
engineers
far 
for
formal
happen,
have
in
is
it
late,
many
must
needs
now
of
often
on
opportunity
or
over
past.
perfect
poor
practice
practices
products
quality.
society
software
taught
the
their
them
these
they
this
to
too
us
use
who
with
work

Read 74 words, 62 unique.
Found 1 word(s) of length 1.
Found 10 word(s) of length 2.
Found 10 word(s) of length 3.
Found 10 word(s) of length 4.
Found 6 word(s) of length 5.
Found 3 word(s) of length 6.
Found 5 word(s) of length 7.
Found 6 word(s) of length 8.
Found 4 word(s) of length 9.
Found 1 word(s) of length 10.
Found 4 word(s) of length 11.
Found 2 word(s) of length 12.

The "values" output file would look something like this:

1 a
2 an
2 in
2 is
2 it
2 on
2 or
2 to
2 us
3 all
3 too
3 use
4 and
4 be
4 late,
5 but
5 of
6 far
6 for
6 must
6 needs
6 now
6 past.
6 poor
6 the
7 over
7 this
8 during
8 often
8 their
8 these
9 element
9 many
9 them
9 who
10 budget,
10 continue
10 engineers
10 have
10 taught
10 they
10 with
11 formal
11 work
12 businesses,
12 critical
12 society
13 dependent
13 educations.
13 happen,
13 products
14 perfect
14 practice
14 software
15 practices
16 disciplines
16 disciplines.
17 consistently
18 craft-like
18 opportunity
19 quality.
20 effective

Your program must have at least two functions in addition to main(); you may wish to have more. One of these functions should read the input text file and take care of loading the vector of "words" and the other should write one of the output files. (It is likely that you will want to add another function to write the other output file, but that is your choice.) You may not use any global data objects (at file scope).

Detailed instructions

If you have difficulties with any part of the lab, consult the instructor for assistance. The basic sequence is:

  1. Prepare a design to meet the problem specifications, identifying:
    • Data objects.
    • Functions (with arguments and return values, if any).
    • Program structure (selection and iteration statements, etc.)
  2. Write the program (source code) to implement your design. Be sure to include internal documentation (comments) describing all data objects, function interfaces, and significant program segments.
  3. Test the program. Two test input files (word0in.txt and word1in.txt) can be downloaded as testword.zip.
  4. Submit the lab report (details below).

Lab report

The lab report should consist of the following:

  • Your design documentation.
  • The source code for your program. Do not include the "gameword" files.
  • The program output files for the test text file word1in.txt.
  • A brief description of any problems you encountered or questions you have regarding the lab.
  • Any suggestions you have for how the lab could be improved.
  • Extra credit: Note that some "words" end up with a comma or period as the last character. Modify your program to discard a punctuation character if it is at the end of a "word".

As with any report you submit, correct spelling and grammar are required. In addition, your report should be submitted electronically following the Electronic submission guidelines. (You may wish to consult the sample report before submitting your report.) Be sure to keep copies of all your files, in case something gets lost. It may be wise to keep a diskette backup as well.

The lab report is due at 5:00pm, Monday, March 22, 1999, though you are encouraged to submit it sooner if you can. Your grade will depend on quality of design and clarity of the code and documentation, as well as whether your program produces the correct results. If you have any questions, consult your instructor.


This page is a slightly modified (by Dr. Christopher C. Taylor) version of a similar page written by Mark Sebern