Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview of Program Behavior The program is called jumble and requires a single command-line argument specifying a dictionary file. For example: ./jumble dictionary-small.txt (You have

Overview of Program Behavior 

The program is called jumble and requires a single command-line argument specifying a dictionary file. For example:

./jumble dictionary-small.txt

(You have been given two dictionary files dictionary-small.txt and dictionary-big.txt; each of them contains just a bunch of English words -- over 250k words in the case of dictionary-big.txt).

After the program sets up its internal data structures, it enters an simple interactive loop which behaves as follows:

The user does one of the following:

enter a string of characters (presumably a jumbled version of one or more English words).

or types ctrl-d to terminate the interactive loop.

If the user enters a string (1a above), a list of all English words in the given dictionary that are rearrangements (anagrams) of the user input. The list can appear in any order. If there are no such English words, the program reports no matches. The user is then prompted for another input.

When the user terminates the interactive loop (1b above), the program produces a report with the following information and then the program terminates:

The number of words read from the input file.

The number of "equivalence-classes": every word in the input file belongs to exactly one subset of the dictionary where all words in the subset are rearrangements of each other. For instance, "stake", "skate", "steak", "takes" all belong to the same equivalence class. This part of the report gives the number equivalence classes formed by the words in the dictionary.

The size of the largest equivalence class.

The "key" associated with the largest equivalence class (see the Algorithm section).

Finally, the members of the largest equivalence class. The ordering of the members can be arbitrary as long as all members are listed.

 #include  #include  #include  #include  #include  #include  #include  /** This file contains some toy code which illustrate example usage of the standard template library unordered_map class among other things. Some other C++ concepts illustrated include: the std::sort function command-line arguments opening and reading from files the "foreach" construct to iterate over elements in an STL "container" The program itself reads a text file and builds a "frequency-count" data structure using an unordered_map. For each ditsinct string in the input file, the map keeps track of the number of time that string appears in the file. **/ /* * this function rearranged the characters in a string * so that they are sorted (according to their ASCII * value). * * Resource: https://en.cppreference.com/w/cpp/algorithm/sort * * Note: this function is not actually used the the program * in this file. But you might find it useful... :) */ void ssort(std::string &s) { /* strings are "iterable" objects and so have the begin() and end() functions. These functions (in a pretty abstract way) return "iterators" which specify the "beginning" and "end" of the associated object). Thus, this call is asking the sort function to sort the entire string s. */ std::sort(s.begin(), s.end()); } /* * usage: ./freq  * * example: ./freq words.txt * */ int main(int argc, char *argv[]){ /* the variable word2freq is an unordered_map from strings to integers. */ std::unordered_map word2freq; std::ifstream file; std::string word; if(argc != 2) { std::cout << "usage: ./freq  "; std::cout << "goodbye "; return 1; } /* * argv[1] is a C-string which is the filname specified * by the user. Let's try to open it. */ file.open(argv[1], std::ios::in); if(!file.is_open()){ std::cout << "Error: could not open file '" << argv[1] << "' "; std::cout << "goodbye "; return 1; } std::cout << "reading input file... "; while(!file.eof()) { file >> word; if(word2freq.count(word) == 0) word2freq[word] = 1; else { word2freq[word]++; } } std::cout << "enter a word and I will tell you what I know about it "; std::cout << " when you are done, type ctrl-d (ctrl-z on windows) "; std::cout << "> "; while(std::cin >> word) { if(word2freq.count(word)==0) { std::cout << "sorry, '" << word << "' does not appear in the input file '" << argv[1] << "' "; } else { std::cout << "'" << word << "' appears " << word2freq[word] << " times in the file '" << argv[1] << "' "; } } // user has quit. Let's dump the map contents std::cout << "word2freq contents: "; printf(" WORD #OCCURRENCES "); printf("------------------------------------------ "); for(std::pair entry : word2freq) { printf(" %10s %10d ", entry.first.c_str(), entry.second); } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

Students also viewed these Databases questions