Question: Your task is to write a program, words, that reports information about the number of words read from standard input. For example, entering the following

Your task is to write a program, words, that reports information about the number of words read from standard input. For example, entering the following text should cause the program to report 9 words:
./words // or words.exe (of course, you can just run from within CLion)
the quick brown fox jumps over the lazy dog
Total: 9
Background
The following skeleton code for the program is provided in the starter code file words.cpp.
int main(int argc, char** argv){
enum { total, unique } mode = total;
for (int c; (c = getopt(argc, argv, "tu"))!=-1;){
switch(c){
case 't': mode = total; break;
case 'u': mode = unique; break;
}
}
argc -= optind;
argv += optind;
string word;
int count =0;
while (cin >> word){
count +=1;
}
switch (mode){
case total: cout << "Total: "<< count << endl; break;
case unique: cout << "Unique: "<<"** missing **"<< endl; break;
}
return 0;
}
The getopt function (#include ) provides a standard way of handling option values in command line arguments to programs. It analyses the command line parameters argc and argv looking for arguments that begin with '-'. It then examines all such arguments for specified option letters, returning individual letters on successive calls and adjusting the variable optind to indicate which arguments it has processed. Consult getopt documentation for details.
In this case, the option processing code is used to optionally modify a variable that determines what output the program should produce. By default, mode is set to total indicating that it should display the total number of words read. The getopt code looks for the t and u options, which would be specified on the command line as -t or -u, and overwrites the mode variable accordingly. When there are no more options indicated by getopt returning -1, argc and argv are adjusted to remove the option arguments that getopt has processed.
Level 1: Unique words
Extend the program so that if run with the u option, specified by the command line argument -u, it displays the number of unique words. To count unique words, implement your own vector class (called MyVector) to keep track of which words you have already seen. When each word is read, check to see if it is already in the vector; if it is not, insert it. The number of unique words will then be the size of the vector.
You will need to write your own class to keep track of the list of words. At this level, you can assume that the test case input will be no more than 1000 unique words, so you can use a statically allocated array to store the items.
A minimal class would have an interface like this:
template class MyVector {
public:
typedef T* iterator; // creates an alias of the T* type called iterator
MyVector();
iterator begin();
iterator end();
int size();
iterator insert (iterator position, const T& item);
private:
T items[1000];
int used;
};
For this practical, you may wish to implement your class as a template class, which is defined entirely in the MyVector.h header file without an accompanying .cpp file. In this approach, the complete method is written within the class scope as a single unit.
Level 2: Iterative mode
Extend the program so that if run with the i option, it iterates over all of the words, and displays the counts of the individual words in alphabetical order. For example
./words -i // or words.exe -i (the -i can also be specified as an argument within CLion)
the quick brown fox jumps over the lazy dog
should result in the output shown below.
brown: 1
dog: 1
fox: 1
jumps: 1
lazy: 1
over: 1
quick: 1
the: 2
Your program will need to store two pieces of information for each word. Define a struct called WordInfo that contains a string, named text, for the word's text, and an int, named count, for the number of times it has been seen. Then create a vector of WordInfo instead of a vector of string. When you add a new word, give it a count of 1. When you see a word that is already in the vector, increment its count.
The simplest way to display the result in alphabetic order is to keep the vector in sorted order as words are added, then simply iterate through the vector at the end. The insertion sort algorithm is a suitable way to find where a new word should be inserted. Iterate through the list until you find a word that is alphabetically after the new word, then insert at that position.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!