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
?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 may not use 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 current capacity, which is the size of the allocated array in the heap, and the actual size, 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 function size() that returns the current size of your DynArray instance. The size will change as integer values are added or removed.
5. A function capacity() 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 function push_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 function pop_back() that decrements the size of the DynArray by 1. No change is made to the allocation.
9. A function at(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
int main() { // Create a default vector (cap = 2) DynArray sam; cout << "capacity = " << sam.capacity() << endl; // push some data into sam cout << " Pushing three values into sam"; sam.push_back(21); sam.push_back(31); sam.push_back(41);
cout << " The values in sam are: ";
// test for out of bounds condition here for (int i = 0; i < sam.size() + 1; i++) { try { cout << sam.at(i) << " "; } catch(runtime_error& x) { cout << x.what() << endl; } } cout << " -------------- ";
// clear sam and display its size and capacity sam.clear(); cout << " sam has been cleared."; cout << " Sam's size is now " << sam.size(); cout << " Sam's capacity is now " << sam.capacity() << endl; cout << "--------------- ";
// Push 12 values into the vector - it should grow cout << " Push 12 values into sam."; for (int i = 0; i < 12; i++) sam.push_back(i);
cout << " Sam's size is now " << sam.size(); cout << " Sam's capacity is now " << sam.capacity() << endl; cout << "--------------- ";
cout << " Test to see if contents are correct..."; // display the values in the vector for (int i = 0; i < sam.size( ); i++) { cout << sam.at(i) << " "; } cout << " -------------- ";
cout << " Test pop_back..."; // display the values in the vector sam.pop_back(); for (int i = 0; i < sam.size( ); i++) { cout << sam.at(i) << " "; } cout << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
