Question: 1 . This assignment allocates the word table in a region of memory called the heap, which permits a new and larger table to be

1. This assignment allocates the word table in a region of memory called the heap, which permits a new and larger table to be allocated elsewhere on the heap when the old table fills up. The contents of the old table are copied over to the new table, and the old table's memory is recycled. Because the word table's implementation is hidden from client code (main() in this case), this change does not impact client code. Allowing the word table to grow gives you some insight into how the C++ std::vector container is implemented. This change permits the number of unique words to be bounded only by the available memory. Read all the provided code before you move on to the next step. Note that the mWordTable data member is now defined as a pointer (initialized to nullptr) in WordCount.h.
2. Implement the WordCount constructor and destructor in WordCount.cpp. The constructor should use the new operator to allocate the mWordTable with initialUniqueWords entries, which is defined by the constructor's parameter and has a default value of 16. The destructor should use the array form of the delete operator to deallocate the mWordTable. The WordCount constructor is called when the mWordCount object is created in main(), and the WordCount destructor is implicitly called when the mWordCount object goes out of scope in main(). A memory leak would occur if the WordCount destructor did not free the memory allocated.
3. A private growTable() member function is now defined in the WordCount class. This function has private access because it is not part of the public WordCount API. The growTable() function is called in addWord() when there is no table entry available for a new word. Implement the growTable() member function. The comments preceding growTable() provide guidance.
4. implementthe addWord(), getWord(), getCount(), numWords() and lookupWord() member functions. The addWord() function now calls growTable() if the mWordTable is already full prior to adding a word to the table.
5. Use the selection sortLinks to an external site. algorithm to implement the sortAlphabetically() and sortDescendingByCount() member functions. The sort member functions should call the private swapTableEntries() helper function to exchange two mWordTable entries, which you also need to implement.
6. As in Assignment 11, implement the main() function in the main.cpp according to the comments that precede main(). Do not override the WordCount object's default initial table size of 16. Display the number of unique words, call sortAlphabetically() before calling displayUniqueWords(), and then call sortDescendingByCount() before again calling displayUniqueWords().
7. Test your code with the provided test.txt and usdeclar.txt files.
8. After you complete this assignment, please make sure to commit and push your solution, followed by confirmation with a browser.
9. Note you may have to edit convertWord() to handle white spaces when adding words.
Wordcount.cpp
#include "WordCount.h"
#include
#include
#include
void WordCount::displayUniqueWords() const
{
for (int i{0}; i mNumUniqueWords; i++){
std::cout std::left std::setw(25) mWordTable[i].word
std::right std::setw(5) mWordTable[i].count std::endl;
}
}
.
void WordCount::convertWord(std::string& word) const
{
int i{0};
while (i word.length()){
char c{ word[i]};
if (ispunct(c)){
word.erase(i,1);
}
else {
if (isupper(c)){
word[i]= tolower(c);
}
i++;
}
}
}
// Return the mWordTable index of the word argument.
// If the word is not found in the mWordTable then -1 is returned.
// TODO: Implement the lookupWord() member function.
// Increase the size of the mWordTable by: (1) doubling mMaxUniqueWords
// and allocating a new mWordTable with this many entries; (2) copying the
// old mWordTable entries to the new mWordTable; and (3) deleting the old
// mWordTable and reassigning the mWordTable pointer to the new mWordTable.
// TODO: Implement the growTable() member function.
// Exchange the mWordTable entries at the specified indices.
// Used by the sort member functions.
// TODO: Implement the swapEntries() member function.
1 . This assignment allocates the word table in a

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!