Question: Using the starter code provided .........Students will implement the class's constructor, making sure to initialize all variables to reasonable values.C++ Students will implement the class's

Using the starter code provided .........Students will implement the class's constructor, making sure to initialize all variables to reasonable values.C++

Students will implement the class's destructor, ensuring no memory leaks occur.

Students will implement a method called resize() which will "resize" the dynamic array by creating a new array of a specified size, copying values over to the new array, and deleting the original array.

Students will not modify main. Students will not remove existing class data members.

//Modify the following starter code to create a dynamic array//do not modify main.

#include  using namespace std; class DynArray { public: // Construct a dynamic array of size sz DynArray(int sz) { // !!!IMPLEMENT THE CONSTRUCTOR, COMMENTS BELOW SERVE AS HINTS.!!! // Set the size // Create the internal "ordinary" array that holds our values // Initialize all values to 0 (0 is arbitrary, I just picked it) } // Function run when class is destroyed! (NO MEMORY LEAKS) ~DynArray(){ // !!!IMPLEMENT THE DESTRUCTOR, AVOID MEMORY LEAKS.!!! } // Function to resize the array! void resize(int newsize) { // !!!IMPLEMENT THE RESIZE FUNCTION, COMMENTS BELOW SERVE AS HINTS.!!! // If the new size is the same, the array doesn't actually // need to be resized // If new size is bigger, we grow the array (by creating a new one // and copying values) // Keep a (temporary) pointer to the old array, we'll delete it later... // Create a new array for _realArray to point to // Copy all the values using a loop // Since the array is becoming larger, there are new ints which // have not been initialized. They are at indices size to newsize-1 // Initialize these new integers using a loop. // Now that we've copied the values, delete the old array // If new size is smaller, shrink the array (truncate extra ints) // Start off the same as growing, create a temporary pointer // and a new array // Loop through ONLY the values you are copying (remember, i=size is // out of bounds since the new array is smaller) // Make sure not to leave any memory leaks // Update the "size" data member of the class } // Get a reference to integer at position i // Reference means they can update the value and it will update inside our // class's array as well! int &geti(int i) { // !!!THIS FUNCTION IS ALREADY IMPLEMENTED. DO NOT CHANGE.!!! return _realArray[i]; } // Get the current size (number of elements) the array has int length() { // !!!THIS FUNCTION IS ALREADY IMPLEMENTED. DO NOT CHANGE.!!! return size; } // Prints the array is a nice manner, with brackets and commas void print() { // !!!THIS FUNCTION IS ALREADY IMPLEMENTED. DO NOT CHANGE.!!! if (size > 0) { cout << "[ " << _realArray[0]; for (int i = 1; i < size; i++) { cout << ", " << _realArray[i]; } cout << " ] "; } } private: // The following data members MUST exist in the class. You must not remove // them. If you feel you need to, you may add additional variables. int *_realArray; int size; }; int main() { // !!!DO NOT MODIFY MAIN. YOUR OBJECTIVE IS TO IMPLEMENT THE ABOVE CLASS. // CHANGES TO MAIN DO NOT SATISFY THIS REQUIREMENT.!!! const int TEST_SIZE = 15; DynArray cool(TEST_SIZE); for (int i = 0; i < TEST_SIZE; i++) { cool.geti(i) = 2 * TEST_SIZE - 2 * i; } cool.print(); cool.resize(cool.length() / 2); cool.print(); cool.resize(cool.length() + 3); cool.print(); cool.resize(cool.length()); return 0; } 

The Expected output is

[ 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2 ] [ 30, 28, 26, 24, 22, 20, 18 ] [ 30, 28, 26, 24, 22, 20, 18, 0, 0, 0 ] Dynamic Array is already size 10 

If this it not your output then your code is wrong.

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!