Question: C + + please. Modify the StringArray class that was created in class to include three additional functions: - insert ( index , s )
C please.
Modify the StringArray class that was created in class to include three additional functions:
insertindex s This function should expand the size of the array by one, inserting the given string at the given index position. All prior values from position index through the end of the array are to be shifted up to the next position.
insertindex nelements This function should insert nelements empty strings beginning at position index. The size of the array increases by nelements.
removeindex nelements This function should remove nelements elements from the array beginning at postion index. Elements beyond those removed are to be shifted down accordingly. The size of the areay decreases by nelements.
Note that you will need to ensure that there is sufficient capacity for the insert functions. There is no need to consider capacity for the remove function.
The supplied stringarray.cpp file is the source code that was developed during class. The supplied stringarray.h file is modified from the class version which includes comments for all public member functions, and also includes prototypes for the functions to be added.
A main function is provided that generates random strings and inserts them into a StringArray object. Add code to the main function that demonstrates your three added functions work as expected.
stringarray.h:
#pragma once
#include
A Dynamic Array of String objects
class StringArray
private:
std::string parray;
int arraycapacity;
int arraysize;
void expandcapacityint newcapacity;
public:
StringArray;
StringArrayint initialsize : StringArray resizeinitialsize;
virtual ~StringArray delete parray;
@param s
void appendconst std::string& s;
@param index
@return
std::string getint index const;
void insertint index, const std::string& s;
void insertint index, int nelements;
void removeint index, int nelements;
@param newsize
void resizeint newsize;
@param index
@param s
void setint index, const std::string& s;
@return
int size const return arraysize;
;
stringarray.cpp:
#include "stringarray.h
using namespace std;
StringArray::StringArray
arraycapacity ;
arraysize ;
parray new stringarraycapacity;
void StringArray::appendconst string& s
if arraysize arraycapacity
expandcapacity;
parrayarraysizes;
void StringArray::expandcapacityint newcapacity
if newcapacity
newcapacity arraycapacity arraycapacity ;
if newcapacity arraycapacity
return;
string p new stringnewcapacity;
for int i; i arraycapacity; i
pi parrayi;
delete parray;
parray p;
arraycapacity newcapacity;
string StringArray::getint index const
if index index arraysize
throw "Index out of range";
return parrayindex;
void StringArray::resizeint newsize
if index index arraysize
throw "Index out of range";
parrayindex s
main.cpp:
#include "stringarray.h
#include
#include
#include
#include
using namespace std;
void printstringarrayconst StringArray& a;
string randomstringint size;
int main
StringArray array;
srandtime;
for int i; i ; i
array.appendrandomstring;
printstringarrayarray;
void printstringarrayconst StringArray& a
for int i; i asize; i
cout setw i: ageti endl;
string randomstringint size
string s;
for int i; i size; i
s a rand;
return s;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
