Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with a couple of the programs. I currently don't have the full points, only 60/100. I need a couple more functions fixed. Please

Need help with a couple of the programs. I currently don't have the full points, only 60/100. I need a couple more functions fixed. Please help! Need to fix: -Insert function (don't know problem) - Resize function: resize(25) called on IntVector of capacity 20, but capacity is now 25 -Reserve function: Error in `./a.out': free(): invalid next size (fast) -assign function: assign(15, 5) called on an IntVector of a capacity > 15, but capacity changed. DIRECTIONS:

You are going to build a smaller, simpler version of the STL vector class that is capable of only storing integers. Appropriately, it will be called IntVector. This class will encapsulate a dynamically allocated array of integers.

You are required to come up with a header file (IntVector.h) that declares the class interface and a separate implementation file (IntVector.cpp) that implements the member functions. You will also create a test harness (main.cpp) that you use to test each function you are currently developing.

Never implement more than 1 or 2 functions without compiling and unit testing them before moving on.

The final test harness (main.cpp) file should include all of the tests you performed on each member function. Feel free to comment out previous tests while you are working on later member functions. However, the final test harness should test ALL of your member functions thoroughly. zyBooks will not grade your main function. However, we will spot check the assignments and take off points for any PROGRAM that does not have this test harness in the main.cpp file.

Encapsulated (Private) Data Fields

unsigned sz: stores the size of the IntVector (the number of elements currently being used).

unsigned cap: store the size of the array

int *data: stores the address of the dynamically-allocated array of integers

Public Interface (Public Member Functions)

IntVector()

IntVector(unsigned size, int value = 0)

~IntVector()

unsigned size() const

unsigned capacity() const

bool empty() const

const int & at(unsigned index) const

int & at(unsigned index)

void insert(unsigned index, int value)

void erase(unsigned index)

const int & front() const

int & front()

const int & back() const

int & back()

void assign(unsigned n, int value)

void push_back(int value)

void pop_back()

void clear()

void resize(unsigned size, int value = 0)

void reserve(unsigned n)

You may NOT add any functions to the public interface nor any member variables at all, public or private.

Private Helper Functions

void expand()

void expand(unsigned amount)

You will not be implementing iterators. On the most part, though, the above functions should work just like the STL vector class member function with the same name. Please see the following pages to get a better idea of what each function should do: STL Vector

Constructors and Accessors

IntVector() - the Default constructor

This function should set both the size and capacity of the IntVector to 0 and will not allocate any memory to store integers. (Make sure you do not have a dangling pointer.)

IntVector(unsigned size, int value = 0)

Sets both the size and capacity of the IntVector to the value of the parameter passed in and dynamically allocates an array of that size as well. This function should initialize all elements of the array to the value of the 2nd parameter.

unsigned size() const

This function returns the current size (not the capacity) of the IntVector object, which is the values stored in the sz data field.

unsigned capacity() const

This function returns the current capacity (not the size) of the IntVector object, which is the value stored in the cap data field.

bool empty() const

Returns whether the IntVector is empty (the sz field is 0).

const int & at(unsigned index) const

This function will return the value stored in the element at the passed in index position.

This function should throw an out_of_range exception, passing it the string "IntVector::at_range_check" if the index is beyond the size of the IntVector. See bottom of specifications for more details on how to throw an exception and how to test that it works properly.

const int & front() const

This function will return the value stored in the first element.

const int & back() const

This function will return the value stored in the last element.

Destructor, Mutators, & Private Helper Functions

~IntVector()

The destructor is used to clean up (delete) any remaining dynamically-allocated memory. For this class, that will be the array pointed to by the int pointer called data.

void expand()

This function will double the capacity of the vector. This function should reallocate memory for the dynamically allocated array and update the value of capacity. Be careful to properly handle the case when capacity is 0 before calling expand().

Make sure you don't create a memory leak here.

void expand(unsigned amount)

This function will expand the capacity of the vector by the amount passed in. This function should reallocate memory for the dynamically allocated array and update the value of capacity.

Make sure you don't create a memory leak here.

void insert(unsigned index, int value)

This function inserts data at position index. To do this, all values currently at position index and greater are shifted to the right by 1 (to the element right after its current position).

Size will be increased by 1. However, If size will become larger than capacity, this function needs to first double the capacity. In other words, if capacity and size are both 20 when this function is called, this function must first increase the capacity to 40 and then it will be able to increase the size to 21.

Since other functions will also potentially need to expand (double) the capacity, call the private helper function named expand (see above) to do this for you.

This function should throw an out_of_range exception, passing it the string "IntVector::insert_range_check" if the index is too large. See bottom of specifications for more details on how to throw an exception and how to test that it works properly.

void erase(unsigned index)

This function removes the value at position index and shifts all of the values at positions greater than index to the left by one (to the element right before its current position).

Size is decreased by 1.

This function should throw an out_of_range exception, passing it the string "IntVector::erase_range_check" if the index is too large. See bottom of specifications for more details on how to throw an exception and how to test that it works properly.

void push_back(int value)

This function inserts a value at the back end of the array.

Size will be increased by 1. However, If size will become larger than capacity, this function needs to first increase the capacity. In other words, if capacity and size are both 20 when this function is called, capacity must first be increased to 40 and then size can be increased to 21.

Since other functions will also potentially need to expand (double) the capacity, call the private helper function named expand (see above) to do this for you.

void pop_back()

This function just needs to decrease size by 1.

This function will never thrown an exception. Calling pop_back on an empty vector will cause undefined behavior.

void clear()

This function reduces the size of the vector to 0.

void resize(unsigned size, int value = 0)

This function resizes the vector to contain size elements.

If size is smaller than the current size(sz), this function just reduces the value of sz to size.

If size is greater than the current size(sz), then the appropriate number of elements are inserted at the end of the vector, giving all of the new elements the value passed in through the 2nd parameter (value).

If the new value of size will be larger than capacity, then the capacity must first be expanded by either doubling (expand()) or by increasing the capacity by a specific amount (expand(size - cap)), whichever results in the largest capacity. Then, this function can then increase the size appropriately.

void reserve(unsigned n)

This function requests that the capacity (the size of the dynamically allocated array) be set to n at minimum. This informs the vector of a planned increase in size.

This function should NOT ever reduce the size of the vector. If n is larger than the current capacity then the capacity should be expanded to n.

A call to this function never affects the elements contained in the vector, nor the vector's size.

void assign(unsigned n, int value)

Assigns new content to the vector object, dropping all the elements contained in the vector before the call and replacing them by those specified by the parameters. The new value of size will be n and all values stored in the vector will have the value of the 2nd parameter.

If the new value of size will be larger than capacity, then this function must first expand capacity by either double (expand()) or by increasing the capacity by a specific amount (expand(n - cap)), whichever results in the largest capacity.

int & at(unsigned index)

This function does exactly the same thing the accessor (const) version of at does.

int & front( )

This function does exactly the same thing the accessor (const) version of front does.

int & back( )

This function does exactly the same thing the accessor (const) version of back does.

Throwing an exception

You will need to include the standard library stdexcept. Then, when the index is out of range, execute the following statement:

throw out_of_range("IntVector::at_range_check"); 

To test if this worked, declare an IntVector of size 10 and then call the at function passing it an index argument of 10 or larger. You should see the following output: ___________________________________________________________________ Here is my code for IntVector.cpp: IF YOU NEED MY OHTER CODES LET ME KNOW!!!!

#include

#include

#include "IntVector.h"

using namespace std;

IntVector::IntVector() {

sz = 0;

cap = 0;

data = 0;

}

IntVector::IntVector(unsigned size, int value) {

sz = size;

cap = size;

data = new int[size];

for (unsigned i = 0; i < size; ++i) {

data[i] = value;

}

}

unsigned IntVector::size() const {

return sz;

}

unsigned IntVector::capacity() const {

return cap;

}

bool IntVector::empty() const {

if (sz == 0) {

return true;

}

else {

return false;

}

}

const int & IntVector::at(unsigned index) const {

if (empty() || (sz <= index)) {

throw out_of_range("IntVector::at_range_check");

}

else {

return data[index];

}

}

const int & IntVector::front() const {

return data[0];

}

const int & IntVector::back() const {

return data[sz - 1];

}

IntVector::~IntVector() {

delete[]data;

}

void IntVector::expand() {

if (cap == 0) {

cap = 1;

data = new int[cap];

data[0] = 0;

}

else {

cap = (cap * 2);

int* ptr = data;

data = new int[cap];

for (unsigned i = 0; i< sz; ++i) {

data[i] = ptr[i];

}

for (unsigned j = sz; j < cap; ++j) {

data[j] = 0;

}

delete[] ptr;

return;

}

}

void IntVector::expand(unsigned ammount) {

cap = cap + ammount;

int* ptr = data;

data = new int[cap];

for (unsigned i = 0; i < sz; ++i) {

data[i] = ptr[i];

}

for (unsigned j = sz; j < cap; ++j) {

data[j] = 0;

}

delete[]ptr;

return;

}

void IntVector::insert(unsigned index, int value) {

++sz;

if (sz > cap) {

expand(sz - cap);

}

if (sz >= cap) {

expand();

}

if (index >= sz) {

throw out_of_range("IntVector::insert_range_check");

}

int *ptr = data;

data = new int[cap];

for (unsigned i = 0; i < index; ++i) {

data[i] = ptr[i];

}

data[index] = value;

for (unsigned i = index; i < sz; ++i) {

data[i + 1] = ptr[i];

}

for (unsigned i = sz; i < cap; ++i) {

data[i] = 0;

}

delete[] ptr;

return;

}

void IntVector::erase(unsigned index) {

if (index >= sz) {

throw out_of_range("IntVector::erase_range_check");

}

int* ptr = new int[sz];

for (unsigned i = 0; i < sz; ++i) {

ptr[i] = data[i];

}

unsigned i = 0;

for (i = index; i < sz; ++i) {

data[i] = ptr[i + 1];

}

--sz;

delete[] ptr;

return;

}

void IntVector::push_back(int value) {

++sz;

if (sz >= cap) {

expand();

}

data[sz - 1] = value;

}

void IntVector::pop_back() {

sz--;

}

void IntVector::clear() {

sz = 0;

}

void IntVector::resize(unsigned size, int value) {

if (size < sz) {

sz = size;

}

if (cap < size) {

expand(size - cap);

}

if (size > cap) {

expand();

}

int* ptr = data;

data = new int[size];

for (unsigned j = 0; j < sz; ++j) {

data[j] = ptr[j];

}

for (unsigned k = sz; k < size; ++k) {

data[k] = value;

}

sz = size;

delete[] ptr;

return;

}

void IntVector::reserve(unsigned n) {

int *ptr = data;

data = new int[n];

for (unsigned i = 0; i < sz; ++i) {

data[i] = ptr[i];

}

for (unsigned k = sz; k < cap; ++k) {

data[k] = 0;

}

cap = n;

delete[] ptr;

return;

}

void IntVector::assign(unsigned n, int value) {

if (n > sz) {

expand(n - cap);

}

int* ptr = data;

data = new int[n];

for (unsigned i = 0; i < n; ++i) {

data[i] = value;

}

sz = n;

delete[] ptr;

return;

}

int & IntVector::at(unsigned index) {

if (empty() || (sz <= index)) {

throw out_of_range("IntVector::at_range_check");

}

else {

return data[index];

}

}

int & IntVector::front() {

return data[0];

}

int & IntVector::back() {

return data[sz - 1];

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Write Hund's rule?

Answered: 1 week ago

Question

What are the purposes of collection messages? (Objective 5)

Answered: 1 week ago