Question: For this lab you will implement a dynamic array by completing the class called MyDynamicArray. The MyDynamicArray class should manage the storage of an array

For this lab you will implement a dynamic array by completing the class called MyDynamicArray. The MyDynamicArray class should manage the storage of an array that can grow and shrink.

The public methods of your class should be the following:

MyDynamicArray(); Default Constructor. The array should be of size 2.

MyDynamicArray(int s); For this constructor the array should be of size s.

~MyDynamicArray(); Destructor for the class.

int& operator[](int i); Traditional [] operator. Should print a message if i is out of bounds and return a reference to a zero value.

void add(int v); increases the size of the array by 1 and stores v there. Should double the capacity when the new element doesn't fit.

void del(); reduces the size of the array by 1. Should shrink the capacity when only 25% of the array is in use after the delete.

int length(); returns the length of the array.

int clear(); Frees any space currently used and starts over with an array of size 2.

you should also add a copy constructor and a copy assignment operator for the class.

MyDynamicArray& operator= (const MyDynamicArray& src)

MyDynamicArray(const MyDynamicArray& src)

You should complete the class by writing the add and del methods. When you need to grow the array size, the add method should print out the message Doubling to : followed by the new size of the array. When the del method shrinks the size of the array, print out the message Reducing to : followed by the new size of the array.

The output below should result from running the main.cpp file with your MyDynamicArray class:

For this lab you will implement a dynamic array by completing the

Here is the given main.cpp:

#include using namespace std; #include "MyDynamicArray.cpp"

void foo(MyDynamicArray param) { for(int i=0; i

int main() { MyDynamicArray x; for (int i=0; i

This is also the code given to start MyDynamicArray.cpp :

#include using namespace std; class MyDynamicArray { private: int size, capacity, error, *a; public: MyDynamicArray() { a = new int[capacity = 2]; size = error = 0; } MyDynamicArray(int s) { a = new int[capacity = size = s]; error = 0; } ~MyDynamicArray() { delete[] a; } int& operator[](int i){ if (i>=size || i

Doubling to 4 Doubling to 8 Doubling to 16 Doubling to 32 Doubling to 64 Doubling to 128 Doubling to 256 The sum is : 8385 Reducing to: 128 Reducing to: 64 Reducing to: 32 Reducing to: 16 Out of bounds reference:10 Doubling to 200 Doubling to 400 The sum is : 171600 The sum is : 168133 10 Out of bounds reference: 2 2 The x sum is 110 The b sum is 10 The b sum is10

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!