Question: Need help with resize function for this 2 unit test.(Failing expand or shorten size, but passing container resize?) vectorADT::resize() to a smaller size. calling resize()

Need help with resize function for this 2 unit test.(Failing expand or shorten size, but passing container resize?)

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

My code.

VectorADT.h

#include

using namespace std;

class VectorADT

{

private:

double *array; int size; int capacity;

public:

VectorADT(); ~VectorADT(); VectorADT(const VectorADT& copy); void push_back(double val); void resize(int newSize); void pop_back(); int length() const; int curr_capacity() const; VectorADT& operator=(VectorADT& rhs); double operator[] (int i) const; VectorADT operator+ (const VectorADT& rhs); friend std::ostream& operator<< (std::ostream& stream, const VectorADT& vector);

};

VectorADT.cpp

#include "VectorADT.h"

VectorADT::VectorADT() { size = 0; capacity = 10; array = new double[this->capacity]; for (int i = 0; i < capacity; i++) { array[i] = 0.0; } }

VectorADT::~VectorADT() { if (array != nullptr) { delete[] array; array = nullptr; } }

VectorADT::VectorADT(const VectorADT& copy) { size = copy.size; capacity = copy.capacity; array = new double[capacity]; for (int i = 0; i < capacity; i++) { array[i] = copy.array[i]; }

}

void VectorADT::push_back(double val) { if (size > capacity) { this->resize(size+1); } array[size] = val; this->size++;

}

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

int VectorADT::length() const { return size; }

int VectorADT::curr_capacity() const { return capacity; }

VectorADT & VectorADT::operator=(VectorADT & v2) { VectorADT v3; for (int i = 0; i < v2.length(); i++) { v3.push_back(v2[i]); } return v3; }

double VectorADT::operator[](int i) const

{ if (i < size) return array[i]; else return -1.0; }

VectorADT VectorADT::operator+(const VectorADT & v2) { VectorADT v3; if (this->length()== v2.length()) { for (int i = 0; i < this->length(); i++) { v3.push_back(this->array[i] + v2.array[i]); } } return v3; }

std::ostream & operator<<(std::ostream & sout, const VectorADT & vector) { for (int i = 0; i < vector.length(); i++) { sout << vector[i]; if (i < vector.length()-1) sout << ",";

} return sout;

}

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!