Question: How is the following done ? implement the public member function void resize(int newsize) that will resize the *elements arrray., steps to be taken are

How is the following done ?

implement the public member function void resize(int newsize) that will resize the *elements arrray., steps to be taken are follwoing :

Allocate memory of the new array of size newsize, Copy elements from old *elements array, f the size of the new array is bigger than the current

*elements array, then fill the additional elements with 0 value, f the size of the new array is smaller than the current *elements array, then copy only newsize

elements from the current *elements array, Delete the memory currently assigned to *elements, and Remap *elements pointer to the new array

Below is my code so far, we have class smartarray wiht public member dynamic array int *elements and private int size;. my code doesnt ouput new size

void smartArray:: resize(int newsize){ int *newarray = new int [newsize]; if (newsize >= size){ for (int i =0 ; i< size;i++) { newarray[i] = elements[i];} for (int i=size;i < newsize;i++) newarray[i-size]= 0; } else { for (int i =0 ; i< size ;i++) newarray[i] = elements[i]; } delete [] elements; * elements =* newarray; }

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!