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:
- insert(index, 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.
- insert(index, n_elements) This function should insert n_elements empty strings beginning at position index. The size of the array increases by n_elements.
- remove(index, n_elements) This function should remove n_elements 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 n_elements.
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 15 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 *p_array;
int array_capacity;
int array_size;
void expand_capacity(int new_capacity=0);
public:
StringArray();
StringArray(int initial_size) : StringArray(){ resize(initial_size); }
virtual ~StringArray(){ delete [] p_array; }
@param s
void append(const std::string& s);
@param index
@return
std::string get(int index) const;
void insert(int index, const std::string& s);
void insert(int index, int n_elements=1);
void remove(int index, int n_elements=1);
@param new_size
void resize(int new_size);
@param index
@param s
void set(int index, const std::string& s);
@return
int size() const { return array_size; }
};
stringarray.cpp:
#include "stringarray.h"
using namespace std;
StringArray::StringArray (){
array_capacity =10;
array_size =0;
p_array = new string[array_capacity];
}
void StringArray::append(const string& s){
if (array_size ==array_capacity)
expand_capacity();
p_array[array_size++]=s;
}
void StringArray::expand_capacity(int new_capacity){
if (new_capacity ==0)
new_capacity =array_capacity + array_capacity /2;
if (new_capacity <=array_capacity)
return;
string *p = new string[new_capacity];
for (int i=0; i < array_capacity; i++)
p[i]= p_array[i];
delete [] p_array;
p_array =p;
array_capacity =new_capacity;
}
string StringArray::get(int index) const {
if (index <0|| index >= array_size)
throw "Index out of range";
return p_array[index];
}
void StringArray::resize(int new_size){
if (index <0|| index >= array_size)
throw "Index out of range";
p_array[index]= s4
}
main.cpp:
#include "stringarray.h"
#include
#include
#include
#include
using namespace std;
void print_string_array(const StringArray& a);
string random_string(int size=8);
int main(){
StringArray array;
srand(time(0));
for (int i=0; i <15; i++)
array.append(random_string());
print_string_array(array);
}
void print_string_array(const StringArray& a){
for (int i=0; i< a.size(); i++)
cout << setw(3)<< i<<": "<< a.get(i)<< endl;
}
string random_string(int size){
string s;
for (int i=0; i < size; i++)
s +='a'+ rand()%26;
return s;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!