Question: C++ Programming: I need help with this Dynamic Array project. I'm not receiving the correct output and would like to know what is incorrect in

C++ Programming:

I need help with this Dynamic Array project. I'm not receiving the correct output and would like to know what is incorrect in my code.

Note: main.cpp CANNOT be changed.

---------------------

//main.cpp

#include #include "dynarray.h" using namespace std;

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; }

-----------------------

?//dynarray.cpp

#include "dynarray.h" using namespace std;

DynArray::DynArray() { dataPtr = new int[Size]; Capacity = 2; Size = 0; }

DynArray::DynArray(int n) { dataPtr = new int[n]; Capacity = n; Size = 0; }

int DynArray::size() { return Size; }

int DynArray::capacity() { return this->Capacity; }

void DynArray::resize(int s) { if (s <= Capacity) { return; } int* newData = new int[s]; for (int n = 0; n < Capacity; ++n) { newData[n] = dataPtr[n]; } Capacity = s; delete[] dataPtr; dataPtr = newData; }

void DynArray::clear() { for (int n = 0; n < Size; ++n) { dataPtr[n] = 0; } Size = 0; }

void DynArray::push_back(int n) { if (Size >= Capacity) { resize(Capacity * 1.5); } dataPtr[Size] = n; Size++; }

void DynArray::pop_back() { dataPtr[Size] = 0; Size--; }

int DynArray::at(int n) { if (n > Size - 1) { throw runtime_error("invalid index"); } return dataPtr[n]; }

-------------------------

//dynarray.h

#include

class DynArray {

public: DynArray(); DynArray(int n); int size(); int capacity(); void clear(); void push_back(int n); void pop_back(); int at(int n); private: int Size; int Capacity; int* dataPtr; void resize(int s); };

-----------------------------------------------

?Program details and expected output:

Important note: Just to be clear, you may not use std::vector to do any of this you are creating your own vector class!

The DynArray Class

For efficiency of operation, DynArray has two notions of size:

(1) the current capacity, which is the size of the allocated array in the heap, and (2) 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:

- 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.

- A parameterized constructor receiving an integer, n, that creates a DynArray of capacity n. Its size will initially be zero.

- A destructor that deletes any dynamically allocated storage. This prevents a memory leak.

- A function size() that returns the current size of your DynArray instance. The size will change as integer values are added or removed.

- 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.

- 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.

- 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.

- A function pop_back(), that decrements the size of the DynArray by 1. No change is made to the allocation.

- 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.

Here is the expected output:

capacity = 2 Pushing three values into sam The values in sam are: 21 31 41 invalid index -------------- sam has been cleared. Sam's size is now 0 Sam's capacity is now 2 --------------- Push 12 values into sam. Sam's size is now 12 Sam's capacity is now 16 --------------- Test to see if contents are correct...0 1 2 3 4 5 6 7 8 9 10 11 -------------- Test pop_back...0 1 2 3 4 5 6 7 8 9 10 

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!