Question: VectorADT.h Can someone help fix this code: /* * VectorADT.h * * Created on: Nov 18, 2017 * Author: TastyCitrus */ #ifndef VECTORADT_H_ #define VECTORADT_H_

VectorADT.h

Can someone help fix this code:

/*

* VectorADT.h

*

* Created on: Nov 18, 2017

* Author: TastyCitrus

*/

#ifndef VECTORADT_H_

#define VECTORADT_H_

#include

#include

#include

#include

#include

#include

using namespace std;

class VectorADT {

private:

/*

* List of variables:

* array = pointer to a dynamic array

* size = the current number of doubles stored within the array

* capacity = the maximum amount of doubles that can be stored within the array

*/

double *array;

int size;

int capacity;

public:

/*

* Default constructor

*/

explicit VectorADT();

/*

* Constructor

*/

explicit VectorADT(int size, int capacity);

/*

* Class destructor

* deallocates memory for the dynamic array

*/

~VectorADT();

/*

* Copy constructor

*/

VectorADT(const VectorADT& copy);

/*

* Methods

* push_back(double val): adds val to the end of the array

* resize(int newSize): resizes the array to newSize

* pop_back(): removes the item at the end of the array

*/

void push_back(double val);

void resize(int newSize);

void pop_back();

/*

* getter functions

*/

int& getSize() const;

int& getCapacity() const;

/*

* Overloaded operators:

* [ ]: returns the i-th value of the array

* +: adds two VectorADT objects and returns the sum. it does not change either of its operands

* <<: friend function that prints out all values stored in VectorADT object, separated by commas

*/

double operator[] (int i);

VectorADT operator+ (const VectorADT& rhs);

friend std::ostream& operator<< (std::ostream& stream, const VectorADT& vector);

VectorADT& operator= (const VectorADT& rhs);

};

#endif /* VECTORADT_H_ */

VectorADT.cpp

/*

* VectorADT.cpp

*

* Created on: Nov 18, 2017

* Author: TastyCitrus

*/

#include "VectorADT.h"

/*

* Default constructor for VectorADT object.

* size is initialized to 0.

* capacity is initialized to 10.

* array is allocated memory to store up to 10 doubles, all of which are initialized to 0.0.

*/

VectorADT::VectorADT() {

size = 0;

capacity = 10;

try {

array = new double[capacity];

} catch (const bad_alloc& e) {

cerr << "Bad memory allocation";

exit(EXIT_FAILURE);

}

for (int i = 0; i < capacity; i++) {

array[i] = 0.0;

}

}

VectorADT::VectorADT(int size, int capacity) {

this->size = size;

this->capacity = capacity;

try {

array = new double[this->capacity];

} catch (const bad_alloc& e) {

cerr << "Bad memory allocation";

exit(EXIT_FAILURE);

}

for (int i = 0; i < this->size; i++) {

array[i] = 0.0;

}

}

/*

* Destructor for VectorADT object.

* array has allocated memory deallocated if it is not null, and is then assigned nullptr.

*/

VectorADT::~VectorADT() {

if (array != nullptr) {

delete [] array;

array = nullptr;

}

}

VectorADT::VectorADT(const VectorADT& copy) {

size = copy.size;

capacity = copy.capacity;

try {

array = new double[capacity];

} catch (const bad_alloc& e) {

cerr << "Bad memory allocation";

exit(EXIT_FAILURE);

}

for (int i = 0; i < capacity; i++) {

array[i] = copy.array[i];

}

}

void VectorADT::push_back(double val) {

this->size++;

if (size > capacity) {

this->resize(size);

}

}

void VectorADT::resize(int newSize) {

if (newSize != size && newSize > 0) { //check if newSize is actually new, and if it is more than 0

if (newSize > capacity) { //if newSize will be over capacity, we increase capacity to 2 times newSize

capacity = newSize*2;

}

double * temp; //declare a new pointer for a temporary array

try {

temp = new double[capacity]; //attempt to allocate memory for temporary array

} catch (const bad_alloc & e) {

cerr << "Bad memory allocation";

exit(EXIT_FAILURE);

}

if (newSize < size) { //if newSize will be smaller than size

for (int i = 0; i < newSize; i++) { //we transfer over newSize as many doubles from original array

temp[i] = array[i];

}

}

else { //otherwise (i.e. newSize is greater than or equal to size)

for (int i = 0; i < size; i++) { //we transfer over all doubles from original array

temp[i] = array[i];

}

for (int i = size; i < newSize; i++) { //and initialize up to newSize elements to 0.0

temp[i] = 0.0;

}

}

size = newSize;

delete [] array;

array = temp;

}

}

void VectorADT::pop_back() {

}

testVectorList.cpp

/*

* testVectorList.cpp

*

* Created on: Nov 18, 2017

* Author: TastyCitrus

*/

#include "ListADT.h"

#include "VectorADT.h"

int main() {

ListADT aaaa;

aaaa.push_front(1);

aaaa.push_front(0);

aaaa.push_back(2);

aaaa.push_back(3);

return 0;

}

ListADT.h

/*

* ListADT.h

*

* Created on: Nov 18, 2017

* Author: TastyCitrus

*/

#ifndef LISTADT_H_

#define LISTADT_H_

#include

#include

#include

#include

#include

#include

using namespace std;

class ListADT {

private:

class Node {

public:

Node() {

value = 0;

next = nullptr;

}

;

//implement this default constructor as an inline function using an initialization section. 0->value, nullptr->next

Node(int val) {

value = val;

next = nullptr;

}

;

//implement this constructor as an inline function using an initialization section. val->value, nullptr->next

int value;

Node *next;

};

Node *head; //point to the first node on the linked list

int size; //number of nodes on the linked list

public:

ListADT(); //default constructor

~ListADT(); //destructor

ListADT(const ListADT& copy); //copy constructor

void push_front (int val);

void push_back (int val);

void pop_front();

void pop_back();

double operator[] (int i);

friend std::ostream& operator<< (std::ostream& stream, const ListADT& list);

ListADT& operator=(ListADT& rhs);

int getSize() const;

};

#endif /* LISTADT_H_ */

ListADT.cpp

/*

* ListADT.cpp

*

* Created on: Nov 18, 2017

* Author: TastyCitrus

*/

#include "ListADT.h"

ListADT::ListADT():head(nullptr),size(0) {

}

ListADT::~ListADT() {

Node* curr = head;

Node* next = head->next;

while (curr != nullptr) {

next = curr->next;

free(curr);

curr = next;

}

head = nullptr;

}

ListADT::ListADT(const ListADT& copy) {

head = new Node(copy.head->value); //create copy of node for head

size = copy.size; //copy size over

Node* curr = copy.head->next; //set pointer to the "current" node being transfered after head

Node* newCurr = head; //set pointer to head

while (curr != nullptr) { //while current node is not null

newCurr->next = new Node(curr->value); //

curr = curr->next;

newCurr = newCurr->next;

}

}

void ListADT::push_front(int val) {

Node* newNode = new Node(val);

newNode->next = head;

head = newNode;

size++;

}

void ListADT::push_back(int val) {

Node* newNode = new Node(val);

Node* curr = head;

while (curr->next != nullptr) {

curr = curr->next;

}

curr->next = newNode;

size++;

}

void ListADT::pop_front() {

Node* after = head->next;

free(head);

head = after;

size--;

}

void ListADT::pop_back() {

Node* curr = head;

while (curr->next != nullptr) {

curr = curr->next;

}

free(curr);

size--;

}

double ListADT::operator[] (int i) {

if (i > size) {

cerr << "Out of range";

exit(EXIT_FAILURE);

}

int count = 0;

Node* curr = head;

while (count < i && count < size) {

curr = curr->next;

count++;

}

return curr->value;

}

int ListADT::getSize() const {

return size;

}

//friend std::ostream &operator<< (std::ostream &stream, const ListADT& list) {

ostream &operator<< (std::ostream &stream, ListADT& list) {

stream << list[0];

int i = 1;

while (i < list.getSize()) {

stream << ", " << list[i];

i++;

}

return stream;

}

In specifically can you fix VectorADT file so it would do this

Declare a class of the name VectorADT to manage a dynamic array of doubles. Specifically, include the following data members:

double * array; int size; //the number of doubles stored in array int capacity; //the maximum number of doubles that can be stored in array 

The definition of size and capacity are similar to those used in the vector STL. Refer to this page for more information about size. Refer to this page for more information about capacity .

The interface (i.e., the public section) of VectorADT is required to include the following functions:

a default constructor to initialize the data members as follows: 0-->size, 10->capacity, and allocating a space of 10 doubles to array (of course). Don't forget to initialize each element on array to 0.00.

the "big-3": destructor, copy constructor and overloaded assignment operator

void push_back(double val ); This member function inserts the value 'val' to the end of the dynamic array

void resize(int newSize); This member function Resizes the container so that it contains newSize elements. If newSize is smaller than the current container size, the content is reduced to its first newSize elements. You don't have to reduce the capacity in this case. If newSize is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of newSize. You are required to initialize all the new elements to 0.00. If newSize is also greater than the current container capacity, make sure you increase the capacity to 2*newSize, i.e., double the value of newSize. For example, if the current value of capacity is 100 and newSize is 150, your program is going to reallocate memory to increase the capacity of arrayto 300.

void pop_back(); This member function deletes the last number from the array, i.e., it decreases the size of the container by 1.

Overload the operator[ ] as a read-only member function to return the i-th element in array. Assume v1 is a VectorADT object, this operator allows one to retrieve the i-th element on array if i is valid using the statement v1[ i ];.

Overload the addition operator (operator+) as a member function to add two VectorADT objects, say v1 and v2 if they are of the same size. This member function is not allowed to change either of its two operands. It returns a VectorADT object corresponding to the sum of v1 and v2. With this operator, one can add v1 and v2 as follows: v3 = v1+v2;

Overload the put operator (i.e., operator<<) as a friend function to print out all the elements stored in a VectorADT object. For example, assume the VectorADT object v1 contains the following numbers 1.10, 21.12, -0.81. One can write cout<

int length() const; This read-only member function returns the current size of the array (i.e., the number of doubles in the container).

int curr_capacity() const; This read-only member function returns the current capacity of the array (i.e., the maximum number of doubles that can be stored in the container).

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!