Question: Can someone please help with Resize code for vectorADT, It suppose to follow this unit test. vectorADT::resize() to a smaller size. calling resize() to shorten
Can someone please help with Resize code for vectorADT, It suppose to follow this unit test.
vectorADT::resize() to a smaller size.
calling resize() to shorten your container from 15 to 2 ...
Sorry, your resize() fails the test due to either or both of the following causes:
1. it doesn't change the size to 2
2. it changes the capacity of the container when it should remain the same
Test vectorADT::resize() to a size that's larger than the current capacity
calling resize() to expand your container from 17 to 31 ...
Sorry, your resize() fails the test due to either or both of the following causes:
1. it doesn't change the size to 31
2. it doesn't increase the capacity of the container to 62
(I think my code is only changing the capacity and not the size)This is my code:
void VectorADT::resize(int newSize) { double * temp; if (newSize > capacity) { capacity = newSize * 2; } temp = new double[capacity]; if (newSize < size) { for (int i = 0; i < capacity; i++) { if (i < newSize) temp[i] = array[i]; else temp[i] = 0.0; } } else if(newSize > size) { for (int i = 0; i < newSize; i++) { if(i temp[i] = array[i]; else temp[i] = 0.0; } } delete[] array; array = temp; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
