Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this computer assignment, you are to write and implement an interactive C++

ID: 3871901 • Letter: F

Question

For this computer assignment, you are to write and implement an interactive C++ program to scan and process a stream of words of a plain text.

Use a map container to store the words, where each word is represented by a pair < string, int >. The first element of the pair, named first, contains the name of the input word, and the second element of the pair, named second, contains its frequency in the input stream. To use a map container in your program, you need to insert the statement: #include in your header file, and to use the functions in the STL, you also need to insert the statement: #include in your header file.

In addition to the main ( ) routine, implement the following subroutines in your program:

• void get_words ( map < string, int >& ) : It gets a word from the input stream and removes its punctuation marks.

• void print_words ( const map < string, int >& ) : It prints the final list of words and their frequencies. It also prints the number of nonempty words and the number of distinct words in the input stream.

• void clean_entry ( const string&, string& ) : It cleans a word from its punctuation marks. The first argument is the original word in the input stream and the second argument contains the same word after cleaning.

The main ( ) routine calls the subroutine get_words ( ) to get words from the input stream, and to clean an individual word from its punctuation marks, get_words ( ) calls the subroutine clean_entry ( ).

You can check the size of a string by the member function length ( ). For string s, the statement: s.length ( ) returns the total number of characters in s. If the returned length is 0 (i.e., the original word contains nothing but punctuation marks), simply ignore the word and read in the next one.  

Use a map container to store the cleaned words (but not the empty words). The statement: m [ s ]++ copies the string s into the map m and updates its frequency. Remember a map keeps only one copy of each individual item (in sorted order) specified in its first argument, key, and stores its frequency in its second argument, value. When key is used as an index to a map, it returns the corresponding value in the map.

Print the number of nonempty words in the input stream and the number of distinct words. You can obtain the number of elements in a map by the member function size ( ). Also print the contents of your map: name of each word and its frequency (NO_ITEMS words per line and the words are left aligned). To allocate certain number of spaces for an output value, use the function setw ( ITEM_W ), and for its left alignment, use left in your cout statement. In your program, use NO_ITEMS = 3, and ITEM_W = 16. For printing, the main ( ) routine calls the subroutine print_words ( ).

Explanation / Answer

A Simple STL Mapping:

The initial problem:

Given a text file, count the number of occurrences of each word in the file, then print the all of the words alphabetically with their corresponding count. For example, if a file contained this line:

it would be presented like this:

Before implementing this algorithm with std::map, how would you implement it without using any STL containers? In other words, using only arrays, linked-lists, or other data structure that you might invent.

A First Attempt

The algorithm goes like this:

Sample code:

A Slight Modification

We can take advantage of the subscript operator that is implemented by std::map. This has a couple of very handy features.

Only the loop is modified:

Given the knowledge of the subscript operator, we can do better at this point. We don't have to call find to locate the item. We can just add it with the subscript operator and it will find it for us:

Of course, we can go further and be more like C++:

Ironically, the code to print the contents of the map is more than the code needed to build of the map.

The final (so far) version:

Given a file containing this text:

We would get this (formatted with columns for the browser):

Future modifications:

  1 When,  2 a  1 advance  1 among  3 and  1 assume  1 becomes  1 causes  1 change.  1 course  1 decent  1 declare  1 earth,  1 entitle  1 equal  1 events,  
  1 for  1 from  1 god  1 have  1 hitherto  1 human  1 impel  2 in  1 independent  1 it  1 laws  1 mankind  1 nature  1 nature's  1 necessary  5 of  
  1 opinions  1 people  1 powers  1 remained,  1 requires  1 respect  1 should  1 station  1 subordination  2 that  8 the  1 them  1 them,  2 they  5 to  3 which  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote