Answered step by step
Verified Expert Solution
Link Copied!

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 1: Simple Insertion
All you need to do to complete Level 1 of the Task is to complete the implementation of the CP2WordVector class. You must do this by creating a new file CP2WordVector.cpp, and submitting this file in your submission. You do not need to implement the level1() function in main.cpp, as this has been implemented for you already. Do not modify level1(), as this may cause the automated test to fail.
Once you have implemented the four methods of CP2WordVector, you can run the main program. If you pass in a command line argument of 1, the main program will call level1() and you will see the output. The level1() function simply adds a new WordInfo for every word it sees, with a counter value of 1. 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: 1
apple: 1
mouse: 1
dog: 1
Total entries: 4
Level 2: Keeping track of number of occurrences (weight: 20%)
In level 2, 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 CP2WordVector, but only by implementing the level2() 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: 2
apple: 1
dog: 1
Total entries: 3
It is important to keep in mind that the implementation of CP2WordVector should not change. You will submit only one CP2WordVector.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 (level2() function), using only the existing methods of CP2WordVector.
Your level2 code of course needs to read the words in from input again, just as level1 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 3: Sorting alphabetically (weight: 60%)
In level 3, you will implement the level3() function so that words are stored with their correct frequency as in level 2, 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: 1
dog: 1
mouse: 2
Total entries: 3
For level 2, the words were required to be sorted in order of first occurrence in the input. For level 3, 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 CP2WordVector.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 "CP2WordVector.h"
using namespace std;
// simple
void level1(CP2WordVector& vector){
string word;
while (cin >> word){
WordInfo info;
info.word = word; info.counter =1;
CP2WordVector.h
#include
#include "CP2WordVector.h"
using namespace std;
// simple
void level1(CP2WordVector& vector){
string word;
while (cin >> word){
WordInfo info;
info.word = word; info.counter =1;
vector.insert(vector.end(), info);
}
}
void level2(CP2WordVector& vector){// store frequency
// add your code for level 2 here
}
void level3(CP2WordVector& vector){// sorted alphabetically
// add your code for level 2 here
}
int main(int argc, char** argv)
{
char *end;
long which =0;
which=strtol(argv[1], &end, 10);
CP2WordVector vector;
switch(which){
case 1: level1(vector); break;
case 2: level2(vector); break;
case 3: level3(vector); break;
}
for (auto it = vector.begin(); it < vector.end(); it++){
cout << it->word <<": "<< it->counter << endl;
}
cout << "Total entries: "<< vector.size()<<'
';
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions