Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I already have the code, but it needs some fixing I will include the 5 files below + the errors. Note: the program should be

I already have the code, but it needs some fixing

I will include the 5 files below + the errors.

Note: the program should be in C++.

1. VectorADT.h:

#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_ */

2. VectorADT.cpp:

#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() {

}

3. ListADT.h:

#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_ */

4. ListADT.cpp:

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

}

5. testVectorList.cpp:

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

}

Errors:

testVectorList.cpp: In function bool testPassed(): testVectorList.cpp:31:14: error: class VectorADT has no member named length size = v1.length();

^~~~~~

testVectorList.cpp:32:18: error: class VectorADT has no member named curr_capacity; did you mean capacity? capacity = v1.curr_capacity();

^~~~~~~~~~~~~

testVectorList.cpp:38:22: error: class VectorADT has no member named length sum_size = sum.length();

^~~~~~

testVectorList.cpp:39:26: error: class VectorADT has no member named curr_capacity; did you mean capacity? sum_capacity = sum.curr_capacity();

^~~~~~~~~~~~~

testVectorList.cpp:65:33: error: class VectorADT has no member named curr_capacity; did you mean capacity? if ( (size != uBound) || (v1.curr_capacity() != capacity) ){

^~~~~~~~~~~~~

testVectorList.cpp:75:7: warning: this if clause does not guard... [-Wmisleading-indentation] if ( (int) v1[ i ] != i*2 ) ^~ testVectorList.cpp:77:11: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the if break;

^~~~~

testVectorList.cpp: In function bool testPassed(): testVectorList.cpp:33:19: error: class VectorADT has no member named curr_capacity; did you mean capacity? capacity = v1.curr_capacity();

^~~~~~~~~~~~~

testVectorList.cpp:38:20: error: class VectorADT has no member named length post_size = v1.length();

^~~~~~

testVectorList.cpp:40:28: error: class VectorADT has no member named curr_capacity; did you mean capacity? if (post_size!=0 && v1.curr_capacity()!= capacity){

^~~~~~~~~~~~~

testVectorList.cpp:62:1: warning: control reaches end of non-void function [-Wreturn-type] } ^

testVectorList.cpp: In function bool testPassed(): testVectorList.cpp:41:13: error: class ListADT has no member named length len = l1.length();

^~~~~~

testVectorList.cpp: In function bool testPassed(): testVectorList.cpp:46:23: error: class ListADT has no member named length int len = lhs.length();

^~~~~~

testVectorList.cpp:81:13: error: class ListADT has no member named length if ( l1.length() != uBound1+uBound2 ){

^~~~~~

testVectorList.cpp:28:9: warning: unused variable len [-Wunused-variable] int len=0;

^~~

testVectorList.cpp:122:1: warning: control reaches end of non-void function [-Wreturn-type] } ^

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

Step: 3

blur-text-image

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

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions