Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

#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:
/**
* Default Constructor
*
* Creates an empty array of string objects.
*/
StringArray();
/**
* Construct StringArray of given size
*
* Creates an array of string objects of a pre-defined size. Each element
* created is an empty string.
*/
StringArray(int initial_size) : StringArray() { resize(initial_size); }
/**
* Destructor, frees memory used by the array
*/
virtual ~StringArray() { delete [] p_array; }
/**
* Add an element to the end of the array
*
* Expands the size of the array by one and sets the element to the
* value of the given string.
*
* @param s String to be added
*/
void append(const std::string& s);
/**
* Retrieve value of string by its index
* @param index Index of the string to be returned
* @return Value at the given index
*/
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);
/**
* Change the size of the array
*
* Changes the size of the array. If new size is larger than the current
* size, value in the newly created elements is unpredictable. If new size
* is smaller, element values beyond the new size are discarded.
*
* @param new_size Desired size of the array
*/
void resize(int new_size);
/**
* Set value at given index
* @param index Index of the element to be set
* @param s New value to be assigned at the given index
*/
void set(int index, const std::string& s);
/**
* Get size of the array
* @return Number of elements in the array
*/
int size() const { return array_size; }
};

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

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

Microsoft Outlook 2023

Authors: James Holler

1st Edition

B0BP9P1VWJ, 979-8367217322

More Books

Students also viewed these Databases questions

Question

3 Does it improve customer retention rates?

Answered: 1 week ago