Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the code is completed, but it has a small error which is: Program end never reached (commonly due to an infinite loop or infinite recursion).

the code is completed, but it has a small error which is: Program end never reached (commonly due to an infinite loop or infinite recursion).

I don't know how to fix it

I will include the 5 files below + the error.

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;

int length() { return size; }

int curr_capacity() { return capacity; }

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

The file ListADT.h is as follows:

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

int length() { return size; }

return stream; }

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;

}

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

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions