Question
StringArray Ehnancements Modify the StringArray class that was created in class to include three additional functions: insert(index, s) This function should expand the size of
StringArray Ehnancements
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 position index. Elements beyond those removed are to be shifted down accordingly. The size of the array 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.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; | |
// Allocate a new array | |
string *p = new string[new_capacity]; | |
// Copy old array to new array | |
for (int i = 0; i < array_capacity; i++) | |
p[i] = p_array[i]; | |
// Deallocate old array (return to system free memory) | |
delete [] p_array; | |
// Set up pointer to new array, and set capacity | |
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 (new_size < 0) | |
throw "Invalid array resize"; | |
expand_capacity(new_size); | |
array_size = new_size; | |
} | |
void StringArray::set(int index, const string& s) { | |
if (index < 0 || index >= array_size) | |
throw "Index out of range"; | |
p_array[index] = s; | |
} |
stringarray.h
|
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)); | |
// Fill array with 15 random strings | |
for (int i = 0; i < 15; i++) | |
array.append(random_string()); | |
// Print the array of random strings | |
print_string_array(array); | |
// TODO: Add your testing code here | |
} | |
void print_string_array(const StringArray& a) { | |
// Prints the contents of the StringArray showing both index and value | |
for (int i = 0; i < a.size(); i++) | |
cout << setw(3) << i << ": " << a.get(i) << endl; | |
} | |
string random_string(int size) { | |
// Returns an random string consisting of *size* lowercase characters | |
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
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