"Oh, my sweet summer child."Originally Posted by abhi143
I am going to provide to you a text file containing English words, all in lowercase, one word per line. Your task is to write a C program to count the frequency with which each word appears in the file, and print to standard output the distinct words and their corresponding frequencies, sorted according to the words in lexicographical order.Originally Posted by abhi143
One approach to doing this is to read all the words into a big array, sort the array, and then make a single pass over the sorted array to count the distinct words. Now, tell me what is the size of this array that you are going to allocate at compile time?
This is, of course, a trick question: I control the content of the file yet I didn't tell you the maximum number of words, so whatever number you come up with I can gleefully tell you that you're wrong because the actual number of words in the file is +1 of the number that you came up with. So, in order to accommodate my power to control the number of words, you'll have to use dynamic memory allocation.
(Having said that, if you were permitted to use other approaches, you might be able to get away with something fixed size like a perfect hash table or a trie of a definitive list of words in English, but besides that being overkill if it turns out that the file only has a few words, it's also possible to change the requirements such that the language is not necessarily English, but possibly even a constructed regular language where "word" means something like "one or more alphabetic characters", in which case pre-constructing even a trie from that would be impossible.)