Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Important note: You may not use std::vector to do any of this -- you are creating your own vector class! We will use the original

Important note: You may not use std::vector to do any of this -- you are creating your own vector class! We will use the original name used by the C++ Standards Committee before they changed it to vector: DynArray (for "dynamic array").

Objectives:

Students will gain confidence in the use of pointers and dynamically allocated storage.

The DynArray Class

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

the current "capacity", which is the size of the dynamic array on the heap at the moment, and

The actual "size", which is the number of elements currently in use.

If these values are the same, then the array is full and needs to be made larger so it can hold more items. This is done by:

allocating a new, larger array (typically 1.5 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.

A function capacity( ) that returns the 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 deletes all of the elements from the DynArray and resets its size to zero and capacity to the default of two. This must be 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 make 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 runtime_error with an appropriate message.

Your Program

Here is a driver that will test your DynArray class. Use it without change. After developing your DynArray class, add the driver to your project and compile and link to create your executable.

Format and document your code in accordance with the course style guidelines. Include a file prologue identifying you as the author. Submit your project using the instructions outlined in the Course Syllabus, Programming Projects section.

Driver:

#include "DynArray2.h" #include  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 << " -------------- "; cout << " Driver Complete..."; cout << endl; cin.get(); }student submitted image, transcription available below 

Important note: You may not tese std vector to do my of this you are creating yotr own vector class! We willuse the oniginal name used by tie C++Standards Commitee before they changed it to vector DymAsray (for "dynamic aray Important note: You may use std vector to do ay of tis you ae creating yo ow vector class We the original nae used by the C++ Standards Coitee before they changed it to vector DynAay (for "dynamic atay Objectives: Students will gaun confidence in the use of pounters and dynanuically allocated storage. The DynArray Class For efficieney of aperation, DynATay has two notions of size the curent "capacity", which is the size of the dynamic anay on the heap at the monent and The actual "size". which is the number of elements currently . use. If these vahues are the same, then the array is fill and needs to be made larger so it can hold more ites. This is done by . allocating a new, larger array (typically 1.5 times the current capacity) copying the currently used data to the fust size" locations of the new array deleting the old array, thus returning ts memory to the heagp having your intemal pointer poin to the new heap aay In order to operate like a tue vector. your class must contain at least the following fnctions 1. A default coustructor that creates a DynAmay with a default capacit of 2. Its size will initially be zero. Remember that size refers to the number of elements cusrently stored by the user in the DynArray. 2. A parameterized constructor receiving an integer. n, that creates a DynArray of capacity . Its sizewll initially be zero 3. A destucto that deletes any dynamically allacated stoage. This pevents a memory leak 4. A function sizer that returns the current size of your DynArray instance. The size will change as integer values are added. 5 A nction ca act that returns the allocated capacity of the vn m v e capacity 15 de uned as the number of mteger value that ca be stored in the vu rr instance he capacity changes T en the under yug array grows 6. A function clearlthat deletes all of the elements fro he DynAray and resets its size to zeso and capacity to the default of two. This must be fresh heap memory 7. A function push backntn 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 make increase the size of the backing array, as explained above. 8. A functionpop back that deents thez" of the DynAray by 1 No change is made to the allocation 9. A functioa at (n thatreuns the value of the integer stored at index of the DynAsray. If the index is outside the range of the vector (no element at that index), this function should thsow a untime eor with an appropriate message auctor that deletes any d Implementing DynArray Read the information here to see how to implement a vector. One of the best things that you can do as you develop and test your DynArray code is to draw pichures as I have done here. You should also read this nage to see how to implement a destructor. Your Program Here is a der that will test your DynArray class. Use it without change. After developing your DynArray class, add the dniver to your project and compile and link to create your executable

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions