Order Now  +1 678 648 4277 

Dynamic arrays homework solution in C++


Dynamic Arrays

Implement a templated Ordered Array class, which stores all items in line with their natural ordering. Your class must contain the following methods.

OrderedArray(int grow_size)

Constructor for an OrderedArray. The grow_size lets the user to stipulate by how much they want the ordered array to grow when the array needs to be resized.

OrderedArray()

Default constructor. The grow_size should default to any sensible value.

~OrderedArray()

Destructor. The OrderedArray must free all memory, including that which is dynamically allocated by it when it is destructed.

void push(const T&newElement)

Puts an element into the array in order. Your array should insert a copy of the element on pushing.

int length()

Returns the number of elements in the array that can be accessed publicly i.e., it should return the number of elements added to the array and not its capacity.

T getElement(int index)

Returns a copy of the element corresponding to the given index. A default instance should be returned for an out-of-range value.

bool remove(int index)

Takes one argument corresponding to the index of the element you want to remove and removes that element from the array. Out-of-range values should be dealt with by returning false. Successful removal should return true.

int search(const T&target)

Searches for a specific target value in the array. The search should return the index in the array containing the element. There are a number of standard search options here e.g., you may choose either a linear search or a binary search. If the element is not found, a negative integer should be returned.

void clear()

Empties the array and frees any memory used by the array in storing the objects.

Solution:

Dynamic Array



/* * Main.cpp * */ #include #include "OrderedArray.h" using namespace std; int main(int argc, char **argv) { OrderedArray testArray(5); cout << "Try to push data: 3 1 2 15 9" << endl; testArray.push(3); testArray.push(1); testArray.push(2); testArray.push(15); testArray.push(9); cout << "\tCurrent array: "; for (int i = 0; i < testArray.length(); i++) { cout << testArray.getElement(i) << " "; } cout << endl; cout << "Try to push more data: 4 8 6 2" << endl; testArray.push(4); testArray.push(8); testArray.push(6); testArray.push(2); cout << "\tCurrent array: "; for (int i = 0; i < testArray.length(); i++) { cout << testArray.getElement(i) << " "; } cout << endl; cout << "Try to search 3 in array: " << (testArray.search(3) ? "Exist" : " Not found") << endl; cout << "Try to search 100 in array: " << (testArray.search(100) ? "Exist" : " Not found") << endl; cout << "Try to search 15 in array: " << (testArray.search(15) ? "Exist" : " Not found") << endl; cout << "Try to remove index 4: " << (testArray.remove(4) ? "Successful" : "Failed") << endl; cout << "Try to remove index 0: " << (testArray.remove(0) ? "Successful" : "Failed") << endl; cout << "Try to remove index 20: " << (testArray.remove(20) ? "Successful" : "Failed") << endl; cout << "Try to remove index 8: " << (testArray.remove(8) ? "Successful" : "Failed") << endl; cout << "\tCurrent array: "; for (int i = 0; i < testArray.length(); i++) { cout << testArray.getElement(i) << " "; } cout << endl; cout << "Test clear()" << endl; testArray.clear(); cout << "\tCurrent array: "; for (int i = 0; i < testArray.length(); i++) { cout << testArray.getElement(i) << " "; } cout << endl; return 0; }