Answered step by step
Verified Expert Solution
Question
00
1 Approved Answer
Level 1 : Simple Insertion All you need to do to complete Level 1 of the Task is to complete the implementation of the CP
Level : Simple Insertion All you need to do to complete Level of the Task is to complete the implementation of the CPWordVector class. You must do this by creating a new file CPWordVector.cpp and submitting this file in your submission. You do not need to implement the level function in main.cpp as this has been implemented for you already. Do not modify level as this may cause the automated test to fail. Once you have implemented the four methods of CPWordVector, you can run the main program. If you pass in a command line argument of the main program will call level and you will see the output. The level function simply adds a new WordInfo for every word it sees, with a counter value of Even if a word is seen multiple times, this will not be reflected in the vector the repeated occurrences will simply have a separate entry each time. For example, if the input contained mouse apple mouse dog Then the output would be mouse: apple: mouse: dog: Total entries: Level : Keeping track of number of occurrences weight: In level we will now make proper use of the WordInfo class to keep track of how many times a word has been encountered. You will do this not by modifying CPWordVector, but only by implementing the level function in main.cpp The idea is that, if we encounter a word in the input that is already in the vector, we will not make a new WordInfo entry, but instead we will find the WordInfo entry for that word in the vector, and increment its counter. This means that for the same input as before, mouse apple mouse dog the output will now be mouse: apple: dog: Total entries: It is important to keep in mind that the implementation of CPWordVector should not change. You will submit only one CPWordVector.cpp file and a main.cpp file, so your implementation of the vector should work for all three levels. The work required to keep track of the occurrences will be done inside the client code level function using only the existing methods of CPWordVector. Your level code of course needs to read the words in from input again, just as level did. Note that iterating through the vector elements can be done using pointers, as demonstrated in the main function of main.cpp in the code that prints out the content. You will also probably need to make use of the functions provided in C to compare two strings together, when working out whether two words are the same or not. Level : Sorting alphabetically weight: In level you will implement the level function so that words are stored with their correct frequency as in level and also they are stored inside the vector in alphabetical order. So now with the same input as before mouse apple mouse dog The output will be: apple: dog: mouse: Total entries: For level the words were required to be sorted in order of first occurrence in the input. For level they should be sorted in alphabetical order. You will probably need to make use of the functions provided in C to compare two strings together in order to see which one comes first alphabetically. Note that you do not need to implement a complete sorting algorithm and sort the items each time! A simple solution would be to just iterate through the elements of the vector and find the correct position where a new word would go and insert the word in that position. If you have implemented insert correctly in CPWordVector.cpp this should already work correctly. If not, you will need to make sure that insert does not delete items that are already in a certain position, but instead moves the contents of the array onwards by one position see the lecture content on dynamic arrays for a reminder if you are not sure how this works main.cpp #include #include CPWordVector.h using namespace std; simple void levelCPWordVector& vector string word; while cin word WordInfo info; info.word word; info.counter ; CPWordVector.h #include #include CPWordVector.h using namespace std; simple void levelCPWordVector& vector string word; while cin word WordInfo info; info.word word; info.counter ; vector.insertvectorend info; void levelCPWordVector& vector store frequency add your code for level here void levelCPWordVector& vector sorted alphabetically add your code for level here int mainint argc, char argv char end; long which ; whichstrtolargv &end, ; CPWordVector vector; switchwhich case : levelvector; break; case : levelvector; break; case : levelvector; break; for auto it vector.begin; it vector.end; it cout itword : itcounter endl; cout "Total entries: vector.size ; return ;
Level : Simple Insertion
All you need to do to complete Level of the Task is to complete the implementation of the CPWordVector class. You must do this by creating a new file CPWordVector.cpp and submitting this file in your submission. You do not need to implement the level function in main.cpp as this has been implemented for you already. Do not modify level as this may cause the automated test to fail.
Once you have implemented the four methods of CPWordVector, you can run the main program. If you pass in a command line argument of the main program will call level and you will see the output. The level function simply adds a new WordInfo for every word it sees, with a counter value of Even if a word is seen multiple times, this will not be reflected in the vector the repeated occurrences will simply have a separate entry each time. For example, if the input contained
mouse apple mouse dog
Then the output would be
mouse:
apple:
mouse:
dog:
Total entries:
Level : Keeping track of number of occurrences weight:
In level we will now make proper use of the WordInfo class to keep track of how many times a word has been encountered. You will do this not by modifying CPWordVector, but only by implementing the level function in main.cpp
The idea is that, if we encounter a word in the input that is already in the vector, we will not make a new WordInfo entry, but instead we will find the WordInfo entry for that word in the vector, and increment its counter. This means that for the same input as before,
mouse apple mouse dog
the output will now be
mouse:
apple:
dog:
Total entries:
It is important to keep in mind that the implementation of CPWordVector should not change. You will submit only one CPWordVector.cpp file and a main.cpp file, so your implementation of the vector should work for all three levels. The work required to keep track of the occurrences will be done inside the client code level function using only the existing methods of CPWordVector.
Your level code of course needs to read the words in from input again, just as level did. Note that iterating through the vector elements can be done using pointers, as demonstrated in the main function of main.cpp in the code that prints out the content. You will also probably need to make use of the functions provided in C to compare two strings together, when working out whether two words are the same or not.
Level : Sorting alphabetically weight:
In level you will implement the level function so that words are stored with their correct frequency as in level and also they are stored inside the vector in alphabetical order.
So now with the same input as before
mouse apple mouse dog
The output will be:
apple:
dog:
mouse:
Total entries:
For level the words were required to be sorted in order of first occurrence in the input. For level they should be sorted in alphabetical order. You will probably need to make use of the functions provided in C to compare two strings together in order to see which one comes first alphabetically.
Note that you do not need to implement a complete sorting algorithm and sort the items each time! A simple solution would be to just iterate through the elements of the vector and find the correct position where a new word would go and insert the word in that position. If you have implemented insert correctly in CPWordVector.cpp this should already work correctly. If not, you will need to make sure that insert does not delete items that are already in a certain position, but
instead moves the contents of the array onwards by one position see the lecture content on dynamic arrays for a reminder if you are not sure how this works
main.cpp
#include
#include CPWordVector.h
using namespace std;
simple
void levelCPWordVector& vector
string word;
while cin word
WordInfo info;
info.word word; info.counter ;
CPWordVector.h
#include
#include CPWordVector.h
using namespace std;
simple
void levelCPWordVector& vector
string word;
while cin word
WordInfo info;
info.word word; info.counter ;
vector.insertvectorend info;
void levelCPWordVector& vector store frequency
add your code for level here
void levelCPWordVector& vector sorted alphabetically
add your code for level here
int mainint argc, char argv
char end;
long which ;
whichstrtolargv &end, ;
CPWordVector vector;
switchwhich
case : levelvector; break;
case : levelvector; break;
case : levelvector; break;
for auto it vector.begin; it vector.end; it
cout itword : itcounter endl;
cout "Total entries: vector.size
;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started