Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This task requires you to implement a C + + class that has some of the functions of a dynamic array or vector. to complete

This task requires you to implement a C++ class that has some of the functions of a dynamic array or
vector.
to complete each of the levels of this task.
The class you will develop is called CP2WordVector. We have provided the header file
CP2WordVector.h, as well as a client program main.cpp that makes use of the class. For level 1
you will complete CP2WordVectors implementation in a separate file CP2WordVector.cpp,
and for level 2 and 3 you will add functions to main.cpp that make use of the class.
In this task, we will make use of the CP2WordVector to store information about words and the
number of times that they occur in the input to our main program. The main.cpp program reads
words from standard input, and stores all the words into a CP2WordVector one by one as it reads
them. When input is finished, main.cpp iterates over the vector and prints out all the stored words
and the number of words that have been stored (the size of the vector). The code to print out the
vector contents has already been provided; you can view this inside the main function of
main.cpp. You will probably find it easier to develop your program if you redirect input so it comes from a text
file (as we have done for other tasks this semester). In CLion, you do this by Run>Edit
Configurations>Redirect Input From. On the command line, you can use the < symbol to redirect
from your text file.
The main.cpp program checks the command line arguments in order to choose which of the three
levels to execute. Depending on the command line argument that has been passed in, it will call
either the level1(), level2() or level3() function. Only level1() has been implemented
for you already. The argument is a number: 1,2 or 3.
For level 1, you implement CP2WordVector.cpp, and then test it by running with argument 1,
which will automatically call the level1() function. For levels 2 and 3, you leave
CP2WordVector.cpp as it is, and implement each of level2() and level3() in main.cpp.
In order to develop and test your program, you simply pass in the argument 1,2 or 3 to your
program. In CLion, you do this by Run>Edit Configurations>Program Arguments. On the command
line, if you have compiled your program for example to an executable called vector.exe, you can
run the program using for example
vector 2< test.txt
to test level 2, where test.txt is the file containing the input. Level 1: Simple Insertion (weight: 20%)
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 main.cpp code below: #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;
} CP2WordVector.h code below: #ifndef PRACSUPP1_CP2WORDVECTOR_H
#define PRACSUPP1_CP2WORDVECTOR_H
#include
class WordInfo {
public:
std::string word;
int counter;
};
class CP2WordVector {
public:
WordInfo* begin();
WordInfo* end();
int size();
WordInfo* insert (WordInfo* position, const WordInfo& item);
private:
WordInfo items[1000];
int number_items =0;
};
#endif //PRACSUPP1_CP2WORDVECTOR_H

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

More Books

Students also viewed these Databases questions