Answered step by step
Verified Expert Solution
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, as discussed
This task requires you to implement a C class that has some of the functions of a dynamic array or
vector, as discussed in the topic content. You will then use the new class in a couple of different ways
to complete each of the levels of this task.
Recall that a vector is a data structure in which we can store several elements, which can then be
accessed directly by their index number. We can iterate over all of the elements in the vector, and we
can insert or remove items from anywhere in the vector. The difference between a normal static
array and a vector also called a dynamic array is that a vector automatically increases in its size
when its storage becomes full, whereas a static array cannot. The class you will develop is called CPWordVector. We have provided the header file
CPWordVector.h as well as a client program main.cpp that makes use of the class. For level
you will complete CPWordVectors implementation in a separate file CPWordVector.cpp
and for level and you will add functions to main.cpp that make use of the class.
In this task, we will make use of the CPWordVector 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 CPWordVector 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
The CPWordVector.h file defines a simple interface with four methods. Your
CPWordVector.cpp will need to supply an implementation of these methods:
begin this returns a pointer to the start of the array of items stored in the vector.
end returns a pointer to the end of the stored items. Note that this is not the last item
in the array, but actually points to the first empty element after the last item. In other words,
if we keep inserting elements at the end of the array, then each new element would go into
the position indicated by end When there are no elements in the array, begin and
end both point to the location where the first element will go
insert this method takes two arguments: the first argument is a pointer to a position
in the array storage of the vector, and the second argument is the new item that will be
inserted into the position. The insert method will insert the item into the position in
the array that has been pointed to It is important to remember that position can point to
any position in the array, ie it could be pointing to a position somewhere in the middle of
the array, for example, and does not necessarily have to point to the end of the array. This
means that you need to be careful not to accidentally overwrite elements that are already in
the array. Instead, elements may need to be moved onwards by one position before the new
element is inserted into its place in the array.
size this simply returns the number of elements that have been stored in the vector.
Note that this is not the actual size of the internal array in this case but rather the
number of items that have been stored in the array by the client program ie by calling
insert
This definition of a vector is not complete, due to the limited scope of this task, and so we are not
really going to provide a full implementation of a vector here. Some other methods that would have
been included in a complete implementation of a vector would be: a delete method to remove
items, and a method to allow you to retrieve individual elements by their index, for example using
square brackets around an index, as you would do with an array and also for example with the STD
vector class We do not need to add these methods for this task.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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