Question
Can someone help me fix this code: This is the actual assignment: The main goal of this coding project is to practice on and then
Can someone help me fix this code:
This is the actual assignment:
The main goal of this coding project is to practice on and then fully grasp the following knowledge points in the context of ADT implementation: (This project will be graded for a total of 80 points.)
use separate files to separate a class's declaration from its implementation
fully understand the necessity of including the big-3 member functions if a class includes a pointer-based dynamic data structure
learn to implement the big-3
learn to overload different operators
learn to implement read-only accessors and mutators for a class
learn to manipulate a singly linked list at both the node-level and list-level
Test cases will be made available in the next few days.
This project will include the following five files:
VectorADT.h: declare a class VectorADT to manage a dynamic array of doubles
VectorADT.cpp: implement VectorADT's member and non-member functions as declared in VectorADT.h
ListADT.h: declare a class ListADT to manage a linked list of integers
ListADT.cpp: implement ListADT's member and non-member functions as declared in ListADT.h
testVectorList.cpp: test the above member and non-member functions.
1. VectorADT.h
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 size() const; This read-only member function returns the current size of the array (i.e., the number of doubles in the container). int 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). 2. VectorADT.cpp This program file implements the above list of functions declared in VectorADT.h 3. ListADT.h Declare a class of the name ListADT to manage a singly linked list of integers. Specifically, include the following data members: ` The interface of ListADT is required to include the following functions: a default constructor to initialize the linked list: 0->size, nullptr->head the "big-3": destructor, copy constructor and overloaded assignment operator void push_back(int val ); This member function inserts the value 'val' to the end of the linked list. void push_front(int val); This member function inserts the value 'val' to the front of the linked list. void pop_back(); This member function deletes the last number from the linked list. void pop_front(); This member function deletes the first number from the linked list. Overload the operator[ ] as a read-only member function to return the i-th element on the linked list. Assume list1 is a ListADTobject, this operator allows one to retrieve the i-th element on the list if i is valid using the statement list1[ i ];. Note that this operator is not included in the STL list. We include it here to showcase how flexible operator overloading can be. an overloaded put operator (i.e., operator<<) as a friend to print out all the data items on the linked list as a comma-separated list. For example, if a list l1 contains the following list of numbers 2->4->-1->10. The statement cout< int size() const; This read-only member function returns the current size of the list container (i.e., the number of integers in the container). 4. ListADT.cpp This program file implements the above list of functions declared in ListrADT.h 5. testVectorList.cpp: Include the main() function in this program file to test all the functions you have implemented in this project. And here is my code : // // VectorADT.h // //Created by Harold Pedroso //on 11/16/2017 // // #ifndef VectorADT_h #define VectorADT_h #include #include #include #include #include #include #include #include using namespace std; class VectorADT{ public: double *array; int sizeArray; //the number of doubles stored in array int capacity; //the maximum number of doubles that can be stored in array VectorADT(); // Default constructor for VectorADT. // VectorADT destructor ~VectorADT(); VectorADT(const VectorADT & );//copy constructor bool operator==( const VectorADT& rhs ) const; //Accessors int size() const; //This read-only member function returns the current size of the array. int capacity() const; //This read-only member function returns the current capacity of the array. void push_back(int val ); void resize(int newSize); //This member function Resizes the container so that it contains newSize elements. void pop_back(); //This member function deletes the last number from the array, i.e., it decreases the size of the container by 1. }; #endif /* VectorADT_h */ // // Created by Harold Pedroso // // on 11/16/2017 // // // #include "VectorADT.h" VectorADT::VectorADT():size(0), capacity(10), array(nullptr),sizeArray(10) { // Default constructor for VectorADT. /* size = 0; capacity = 10; */ } VectorADT::~VectorADT() //Destructor for Vector ADT { if (array!=nullptr){ delete [] array; array = nullptr; } } VectorADT::VectorADT(int size):size(size), array(nullptr),sizeArray(10) { array = new double[ 10]; }catch(bad_alloc &e){ cerr << e.what(); } for (int i=0; i<10; i++) array[ i ] = 0.0; } VectorADT::VectorADT(const VectorADT& clone) { //to be implemeted later } int VectorADT::capacity() const { return capacity; } int VectorADT::size() const { return size; } const VectorADT VectorADT::operator++(int dummy) { VectorADT clone(*this); capacity +=1; return clone; } // // Created by Harold Pedroso // on 11/16/17 // // // #include "VectorADT.h" #include "ListADT.h" #include #include int main(){ ListADT list1; for(int i=0; i< 10; i++) list1.push_front(i); return 0; } // // Created By Harold Pedroso // on 11/16/17 // // // // #ifndef ListADT_h #define ListADT_h #include #include #include #include #include #include #include #include using namespace std; class ListADT{ class Node { public: Node(){ }; //implement this default constructor as an inline function using an initialization section. 0->value, nullptr->next Node(int val) { } ; //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 ListADT(); // Default constructor for ListADT. ~ListADT(); // // ListADT destructor //Copy Contructor ListADT( const ListADT &clone ); void push_back(int val ); //This member function inserts the value 'val' to the end of the linked list. void push_front(int val); void pop_back(); //This member function deletes the last number from the linked list void pop_front(); //This member function deletes the first number from the linked list. int size() const; //This read-only member function returns the current size of the list container (i.e., the number of integers in the container). }; #endif /* ListADT_h */ // // Created by Harold Pedroso // on 11/16/17 // // // #include "VectorADT.h" #include "ListADT.h" ListADT::~ListADT() //Destructor for Vector ADT { if (head!=nullptr){ delete [] head; head = nullptr; } } ListADT::ListADT():head(nullptr), size(0){ //a default constructor to initialize the linked list: 0->size, nullptr->head } //This function inserts the value 'val' to the beginning of the linked list. void ListADT::push_front(int val ){ Node *newNode = newNode(val); if(head == nullptr) head = newNode; else{ newNode -> next = head; head = newNode; } size +=1; } //This function inserts the value 'val' to the end of the linked list. void ListADT::push_back(int val ){ Node *newNode = newNode(val); newNode -> next = head; newnode ->next = nullptr; //Node *currNode = head; if(head == nullptr) head = currNode; while(currNode != nullptr){ cout<< currNode ->val << " "; currNode = currNode ->next; currNode -> next = newNode; } cout<< endl; } //This function deletes the first number from the linked list. void ListADT::pop_front() { Node *newNode = newNode(val); if(head == nullptr) head = newNode; else{ head = head ->next; delete newNode; } } //This function deletes the last number from the linked list. void ListADT::pop_back(){ //* if(preNode != nullptr) return; else{ Node *prevNode; if(head == prevNode){ head = nullptr; prevNode = nullptr; }else{ Node *prevNode = head; while(newprevNode ->next != prevNode) newprevNode = newprevNode -> next; prevNode = newprevNode; prevNode-> next = nullptr; } delete prevNode; } size -=1; } class Node { public: Node(){ }; //implement this default constructor as an inline function using an initialization section. 0->value, nullptr->next Node(int val) { } ; //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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started