Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please help with Resize code for vectorADT, It suppose to follow thi

ID: 3710900 • Letter: C

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

Explanation / Answer

#include <iostream>

#include<vector>

using namespace std;

int n;//declaring vriable for newsize

vector<float> arr;//declaring vector

int capacity= 68;// initial size of vector i have taken 17 so capacity= 17*4

int size=17;//initial size of vector is 17

void re_size(int newSize)

{

double * temp;//declaring array

if (newSize > capacity)

{

capacity = newSize * 2;

}

temp = new double[capacity];

if (newSize < size)// if newsize =2

{

for (int i = 0; i < newSize; i++)

{

temp[i] = arr[i];//first two elements as it is and rest discarded

}

}

else if(newSize > size)

{

for (int i = 0; i < newSize; i++)

{

if(i<arr.size())//first 17 elements as it is

temp[i] = arr[i];

else//the rest elements are 0 after resizing

temp[i] = 0.0;

}

}

for(int i=0;i<newSize;i++)

{

cout<<temp[i]<<endl;// printing the new vector

}

}

int main()

{

int x=17;//initial size

for(int i=0;i<17;i++)//entering data in vector

{

arr.push_back(i);

}

cin>>n;//newsize

re_size(n);//calling resize function

}// just a slight change in your resize function ,the rest is ok