Question: ?C++ Dynamic Array Program You will be creating the dynarray.h and dynarray.cpp files. The main.cpp is provided. Important note: Just to be clear, you maynotuse
You will be creating the dynarray.h and dynarray.cpp files. The main.cpp is provided.
Important note: Just to be clear, you maynotuse std::vector to do any of this you are creating your own vector class! You will write code that constitutes the beginning of the real vector class in the C++ library (except that DynArray will not be genericit will only hold integers). We will use the original name used by the C++ Standards Committee before they changed it to vector: DynArray (for \"dynamic array\").
?The DynArray Class
For efficiency of operation, DynArray has two notions of size: The currentcapacity, which is the size of the allocated array in the heap, and the actualsize, which is the number of elements in use.
If these values are the same, then the array is full and needs to be made larger before it can add more items. This is done by:
- allocating a new, larger array (typically 1.5-to-2 times the current capacity)
- copying the currently used data to the first \"size\" locations of the new array
- deleting the old array, thus returning its memory to the heap
- having your internal pointer point to the new heap array
In order to operate like a true vector, your class must contain at least the following functions:
1. A default constructor that creates a DynArray with a default capacity of 2. Its size will initially be zero. Remember that size refers to the number of elements currently stored by the user in the DynArray.
2. A parameterized constructor receiving an integer, n, that creates a DynArray of capacity n. Its size will initially be zero.
3. A destructor that deletes any dynamically allocated storage. This prevents a memory leak.
4. A functionsize()that returns the current size of your DynArray instance. The size will change as integer values are added or removed.
5. A functioncapacity()that returns the current allocated capacity of the DynArray. The capacity is defined as the number of integer values that can be stored in the DynArray instance. The capacity changes when the underlying array grows.
6. A function?clear()that returns the dynamic memory to the heap, and resets its size to zero and capacity to the default of two, allocating fresh heap memory.
7. A functionpush_back(int n)that adds the integer value n to the end of the DynArray. If it is not large enough to hold this additional value, you must increase the size of the backing array, as explained above.
8. A functionpop_back()that decrements the size of the DynArray by 1. No change is made to the allocation.
9. A functionat(int n)that returns the value of the integer stored at index n of the DynArray. If the index is outside the range of the vector (no element at that index), this function should throw a std::runtime_error with an appropriate message.
main.cpp
#include #include \"dynarray.h\" using namespace std;
int main() { // Create a default vector (cap = 2) DynArray sam; cout // push some data into sam cout sam.push_back(21); sam.push_back(31); sam.push_back(41);
cout
// test for out of bounds condition here for (int i = 0; i { try { cout } catch(runtime_error& x) { cout } } cout
// clear sam and display its size and capacity sam.clear(); cout cout cout cout
// Push 12 values into the vector - it should grow cout for (int i = 0; i sam.push_back(i);
cout cout cout
cout // display the values in the vector for (int i = 0; i { cout } cout
cout // display the values in the vector sam.pop_back(); for (int i = 0; i { cout } cout }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
