SlideShare a Scribd company logo
1 of 17
can you do this for sure and if so how will you start doing i just
want make sure you will be able to do it because a lot people
here say they can than after i accept the bid before the due date
by few hours they say sorry i cant do the assignment it one will
be based on array and with size dynamically expanding and
shrinking Another one based on linkedlist.
Use a simple vector you created before to create two other more
complex vectors with
1) Memory allocation that doubles size of memory when end
reached, and 1/2's memory when the size reaches 1/4.
2) Implemented with a singularly linked list.
Main.cpp
#include
#include "SimpleVector.h"
//System Libraries
#include //Input/Output Library
using namespace std;
//User Libraries
//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e,
etc...
//Function Prototypes
void fillVec(SimpleVector &);
void addVec(SimpleVector &);
void delVec(SimpleVector &);
void prntVec(SimpleVector &,int);
//Execution Begins Here!
int main(int argc, char** argv) {
//Declare Variables
int size;
//Read in the size
cout<<"What size vector to test?"<
cin>>size;
SimpleVector sv(size);
//Initialize or input i.e. set variable values
fillVec(sv);
//Display the outputs
prntVec(sv,10);
//Add and subtract from the vector
addVec(sv);
//Display the outputs
prntVec(sv,10);
//Add and subtract from the vector
delVec(sv);
//Display the outputs
prntVec(sv,10);
//Exit stage right or left!
return 0;
}
void addVec(SimpleVector &sv){
int add=sv.size()*0.1;
for(int i=1;i<=add;i++){
sv.push_front(i+add-1);
sv.push_back(i-add);
}
}
void delVec(SimpleVector &sv){
int del=sv.size()*0.2;
for(int i=1;i<=del;i++){
sv.pop_front();
sv.pop_back();
}
}
void fillVec(SimpleVector &sv){
for(int i=0;i
sv[i]=i%10;
}
}
void prntVec(SimpleVector &sv,int n){
cout<
for(int i=0;i
cout<
if(i%n==(n-1))cout<
}
cout<
}
SimpleVector.h
// SimpleVector class template
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include
#include // Needed for bad_alloc exception
#include // Needed for the exit function
using namespace std;
template
class SimpleVector
{
private:
T *aptr; // To point to the allocated array
int arraySize; // Number of elements in the array
void memError(); // Handles memory allocation errors
void subError(); // Handles subscripts out of range
public:
// Default constructor
SimpleVector()
{ aptr = 0; arraySize = 0;}
// Constructor declaration
SimpleVector(int);
// Copy constructor declaration
SimpleVector(const SimpleVector &);
// Destructor declaration
~SimpleVector();
//Adding and subtracting from the Vector
void push_front(T);
void push_back(T);
T pop_front();
T pop_back();
// Accessor to return the array size
int size() const
{ return arraySize; }
// Accessor to return a specific element
T getElementAt(int position);
// Overloaded [] operator declaration
T &operator[](const int &);
};
// Constructor for SimpleVector class. Sets the size of the *
// array and allocates memory for it. *
template
SimpleVector::SimpleVector(int s)
{
arraySize = s;
// Allocate memory for the array.
try
{
aptr = new T [s];
}
catch (bad_alloc)
{
memError();
}
// Initialize the array.
for (int count = 0; count < arraySize; count++)
*(aptr + count) = 0;
}
// Copy Constructor for SimpleVector class. *
template
SimpleVector::SimpleVector(const SimpleVector &obj)
{
// Copy the array size.
arraySize = obj.arraySize;
// Allocate memory for the array.
aptr = new T [arraySize];
if (aptr == 0)
memError();
// Copy the elements of obj's array.
for(int count = 0; count < arraySize; count++)
*(aptr + count) = *(obj.aptr + count);
}
// Add 1 or Delete 1 front or back for SimpleVector class. *
template
void SimpleVector::push_front(T val){
// Allocate memory for the array.
T *newarr = 0;
try
{
newarr = new T [arraySize + 1];
}
catch (bad_alloc)
{
memError();
}
*(newarr) = val;//Add value to the front of the new array
// Copy previous array contents.
arraySize++;//Increment array size
for (int count = 1; count < arraySize; count++)
*(newarr + count) = *(aptr + count - 1);
delete aptr;//Delete previous array
aptr = newarr;
}
template
void SimpleVector::push_back(T val){
// Allocate memory for the array.
T *newarr = 0;
try
{
newarr = new T [arraySize + 1];
}
catch (bad_alloc)
{
memError();
}
// Copy previous array contents.
for (int count = 0; count < arraySize; count++)
*(newarr + count) = *(aptr + count);
*(newarr + arraySize) = val;//Add value at back of the
array
arraySize++;//Increment array size
delete aptr;//Delete previous array
aptr = newarr;
}
template
T SimpleVector::pop_front(){
T dummy = 0;
if(arraySize != 0)//If array is not empty then only pop
{
dummy = *aptr;
if(arraySize == 1){
delete aptr;
aptr = 0;
}
else {
// Allocate memory for the array.
T *newarr = 0;
try {
newarr = new T[arraySize - 1];
}
catch (bad_alloc) {
memError();
}
// Copy previous array contents.
for (int count = 1; count < arraySize; count++)
*(newarr + count - 1) = *(aptr + count);
delete aptr;//Delete previous array
aptr = newarr;
}
arraySize--;//Decrease array size
}
return dummy;//Return popped value
}
template
T SimpleVector::pop_back(){
T dummy = 0;
if(arraySize != 0)//If array is not empty then only pop
{
dummy = *(aptr + arraySize - 1);
if(arraySize == 1){
delete aptr;
aptr = 0;
}
else {
// Allocate memory for the array.
T *newarr = 0;
try {
newarr = new T[arraySize - 1];
}
catch (bad_alloc) {
memError();
}
// Copy previous array contents.
for (int count = 0; count < arraySize - 1; count++)
*(newarr + count) = *(aptr + count);
delete aptr;//Delete previous array
aptr = newarr;
}
arraySize--;//Decrease array size
}
return dummy;//Return popped value
}
//**************************************
// Destructor for SimpleVector class. *
//**************************************
template
SimpleVector::~SimpleVector()
{
if (arraySize > 0)
delete [] aptr;
}
// memError function. Displays an error message and
// terminates the program when memory allocation fails.
template
void SimpleVector::memError()
{
cout << "ERROR:Cannot allocate memory. n";
exit(EXIT_FAILURE);
}
// subError function. Displays an error message and *
// terminates the program when a subscript is out of range. *
template
void SimpleVector::subError()
{
cout << "ERROR: Subscript out of range.n";
exit(EXIT_FAILURE);
}
// getElementAt function. The argument is a subscript. *
// This function returns the value stored at the sub- *
// cript in the array. *
template
T SimpleVector::getElementAt(int sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
// Overloaded [] operator. The argument is a subscript. *
// This function returns a reference to the element *
// in the array indexed by the subscript. *
template
T &SimpleVector::operator[](const int &sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
#endif

More Related Content

More from DinahShipman862

Choose a scene from a movie.Watch the scene with the sound on, the.docx
Choose a scene from a movie.Watch the scene with the sound on, the.docxChoose a scene from a movie.Watch the scene with the sound on, the.docx
Choose a scene from a movie.Watch the scene with the sound on, the.docx
DinahShipman862
 
Child Observation Report Total Points 5 Community Observations  S.docx
Child Observation Report Total Points 5 Community Observations  S.docxChild Observation Report Total Points 5 Community Observations  S.docx
Child Observation Report Total Points 5 Community Observations  S.docx
DinahShipman862
 
CHFD 348Forum 4 Family Therapy Case Need 300 words and .docx
CHFD 348Forum 4 Family Therapy Case Need 300 words and .docxCHFD 348Forum 4 Family Therapy Case Need 300 words and .docx
CHFD 348Forum 4 Family Therapy Case Need 300 words and .docx
DinahShipman862
 
Checkpoint Instructions New System Proposal (Preparation for Week 4 .docx
Checkpoint Instructions New System Proposal (Preparation for Week 4 .docxCheckpoint Instructions New System Proposal (Preparation for Week 4 .docx
Checkpoint Instructions New System Proposal (Preparation for Week 4 .docx
DinahShipman862
 

More from DinahShipman862 (20)

Choose a scene from a movie.Watch the scene with the sound on, the.docx
Choose a scene from a movie.Watch the scene with the sound on, the.docxChoose a scene from a movie.Watch the scene with the sound on, the.docx
Choose a scene from a movie.Watch the scene with the sound on, the.docx
 
Choose a resident microbe from the FAQ Human Microbiome document th.docx
Choose a resident microbe from the FAQ Human Microbiome document th.docxChoose a resident microbe from the FAQ Human Microbiome document th.docx
Choose a resident microbe from the FAQ Human Microbiome document th.docx
 
Choose a personality theory that justifiably aligns with your perspe.docx
Choose a personality theory that justifiably aligns with your perspe.docxChoose a personality theory that justifiably aligns with your perspe.docx
Choose a personality theory that justifiably aligns with your perspe.docx
 
choose a role researcher, consultant, or organizational leader. In .docx
choose a role researcher, consultant, or organizational leader. In .docxchoose a role researcher, consultant, or organizational leader. In .docx
choose a role researcher, consultant, or organizational leader. In .docx
 
Choose a personality disorder (e.g., borderline, antisocial, narciss.docx
Choose a personality disorder (e.g., borderline, antisocial, narciss.docxChoose a personality disorder (e.g., borderline, antisocial, narciss.docx
Choose a personality disorder (e.g., borderline, antisocial, narciss.docx
 
Choose a single toxicant, and explain how it can impact the immune s.docx
Choose a single toxicant, and explain how it can impact the immune s.docxChoose a single toxicant, and explain how it can impact the immune s.docx
Choose a single toxicant, and explain how it can impact the immune s.docx
 
Choose a non-communicable disease such as cancer, diabetes, cardiova.docx
Choose a non-communicable disease such as cancer, diabetes, cardiova.docxChoose a non-communicable disease such as cancer, diabetes, cardiova.docx
Choose a non-communicable disease such as cancer, diabetes, cardiova.docx
 
Choose a musical artist (from any music era) and provide a 1-2 page .docx
Choose a musical artist (from any music era) and provide a 1-2 page .docxChoose a musical artist (from any music era) and provide a 1-2 page .docx
Choose a musical artist (from any music era) and provide a 1-2 page .docx
 
Choose a news article from either a physical newspaper or online new.docx
Choose a news article from either a physical newspaper or online new.docxChoose a news article from either a physical newspaper or online new.docx
Choose a news article from either a physical newspaper or online new.docx
 
Choose a president from this unit (George Washington to John Quincy .docx
Choose a president from this unit (George Washington to John Quincy .docxChoose a president from this unit (George Washington to John Quincy .docx
Choose a president from this unit (George Washington to John Quincy .docx
 
Choose a control system that has direct impact on your daily life. E.docx
Choose a control system that has direct impact on your daily life. E.docxChoose a control system that has direct impact on your daily life. E.docx
Choose a control system that has direct impact on your daily life. E.docx
 
Choose a culture that interests you or one that you want to learn mo.docx
Choose a culture that interests you or one that you want to learn mo.docxChoose a culture that interests you or one that you want to learn mo.docx
Choose a culture that interests you or one that you want to learn mo.docx
 
Choose 2 inventions from either the ancient world or medieval world .docx
Choose 2 inventions from either the ancient world or medieval world .docxChoose 2 inventions from either the ancient world or medieval world .docx
Choose 2 inventions from either the ancient world or medieval world .docx
 
China transformed into a vast multiethnic and multicultural Eurasian.docx
China transformed into a vast multiethnic and multicultural Eurasian.docxChina transformed into a vast multiethnic and multicultural Eurasian.docx
China transformed into a vast multiethnic and multicultural Eurasian.docx
 
Child Observation Report Total Points 5 Community Observations  S.docx
Child Observation Report Total Points 5 Community Observations  S.docxChild Observation Report Total Points 5 Community Observations  S.docx
Child Observation Report Total Points 5 Community Observations  S.docx
 
Choose 1 of the 3 researchers (Kurt Lewin, Floyd Henry Allport, or L.docx
Choose 1 of the 3 researchers (Kurt Lewin, Floyd Henry Allport, or L.docxChoose 1 of the 3 researchers (Kurt Lewin, Floyd Henry Allport, or L.docx
Choose 1 of the 3 researchers (Kurt Lewin, Floyd Henry Allport, or L.docx
 
Chicago or MLA format, 6 pagesChina transformed into a vast mult.docx
Chicago or MLA format, 6 pagesChina transformed into a vast mult.docxChicago or MLA format, 6 pagesChina transformed into a vast mult.docx
Chicago or MLA format, 6 pagesChina transformed into a vast mult.docx
 
Chief executive officer (CEO) Beranger wants to know more details ab.docx
Chief executive officer (CEO) Beranger wants to know more details ab.docxChief executive officer (CEO) Beranger wants to know more details ab.docx
Chief executive officer (CEO) Beranger wants to know more details ab.docx
 
CHFD 348Forum 4 Family Therapy Case Need 300 words and .docx
CHFD 348Forum 4 Family Therapy Case Need 300 words and .docxCHFD 348Forum 4 Family Therapy Case Need 300 words and .docx
CHFD 348Forum 4 Family Therapy Case Need 300 words and .docx
 
Checkpoint Instructions New System Proposal (Preparation for Week 4 .docx
Checkpoint Instructions New System Proposal (Preparation for Week 4 .docxCheckpoint Instructions New System Proposal (Preparation for Week 4 .docx
Checkpoint Instructions New System Proposal (Preparation for Week 4 .docx
 

can you do this for sure and if so how will you start doing i just w

  • 1. can you do this for sure and if so how will you start doing i just want make sure you will be able to do it because a lot people here say they can than after i accept the bid before the due date by few hours they say sorry i cant do the assignment it one will be based on array and with size dynamically expanding and shrinking Another one based on linkedlist. Use a simple vector you created before to create two other more complex vectors with 1) Memory allocation that doubles size of memory when end reached, and 1/2's memory when the size reaches 1/4. 2) Implemented with a singularly linked list. Main.cpp #include #include "SimpleVector.h" //System Libraries #include //Input/Output Library using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
  • 2. //Function Prototypes void fillVec(SimpleVector &); void addVec(SimpleVector &); void delVec(SimpleVector &); void prntVec(SimpleVector &,int); //Execution Begins Here! int main(int argc, char** argv) { //Declare Variables int size; //Read in the size cout<<"What size vector to test?"< cin>>size; SimpleVector sv(size); //Initialize or input i.e. set variable values fillVec(sv); //Display the outputs prntVec(sv,10); //Add and subtract from the vector
  • 3. addVec(sv); //Display the outputs prntVec(sv,10); //Add and subtract from the vector delVec(sv); //Display the outputs prntVec(sv,10); //Exit stage right or left! return 0; } void addVec(SimpleVector &sv){ int add=sv.size()*0.1; for(int i=1;i<=add;i++){ sv.push_front(i+add-1); sv.push_back(i-add); } } void delVec(SimpleVector &sv){
  • 4. int del=sv.size()*0.2; for(int i=1;i<=del;i++){ sv.pop_front(); sv.pop_back(); } } void fillVec(SimpleVector &sv){ for(int i=0;i sv[i]=i%10; } } void prntVec(SimpleVector &sv,int n){ cout< for(int i=0;i cout< if(i%n==(n-1))cout< } cout<
  • 5. } SimpleVector.h // SimpleVector class template #ifndef SIMPLEVECTOR_H #define SIMPLEVECTOR_H #include #include // Needed for bad_alloc exception #include // Needed for the exit function using namespace std; template class SimpleVector { private: T *aptr; // To point to the allocated array int arraySize; // Number of elements in the array void memError(); // Handles memory allocation errors void subError(); // Handles subscripts out of range public:
  • 6. // Default constructor SimpleVector() { aptr = 0; arraySize = 0;} // Constructor declaration SimpleVector(int); // Copy constructor declaration SimpleVector(const SimpleVector &); // Destructor declaration ~SimpleVector(); //Adding and subtracting from the Vector void push_front(T); void push_back(T); T pop_front(); T pop_back(); // Accessor to return the array size int size() const { return arraySize; } // Accessor to return a specific element
  • 7. T getElementAt(int position); // Overloaded [] operator declaration T &operator[](const int &); }; // Constructor for SimpleVector class. Sets the size of the * // array and allocates memory for it. * template SimpleVector::SimpleVector(int s) { arraySize = s; // Allocate memory for the array. try { aptr = new T [s]; } catch (bad_alloc) { memError();
  • 8. } // Initialize the array. for (int count = 0; count < arraySize; count++) *(aptr + count) = 0; } // Copy Constructor for SimpleVector class. * template SimpleVector::SimpleVector(const SimpleVector &obj) { // Copy the array size. arraySize = obj.arraySize; // Allocate memory for the array. aptr = new T [arraySize]; if (aptr == 0) memError(); // Copy the elements of obj's array. for(int count = 0; count < arraySize; count++)
  • 9. *(aptr + count) = *(obj.aptr + count); } // Add 1 or Delete 1 front or back for SimpleVector class. * template void SimpleVector::push_front(T val){ // Allocate memory for the array. T *newarr = 0; try { newarr = new T [arraySize + 1]; } catch (bad_alloc) { memError(); } *(newarr) = val;//Add value to the front of the new array // Copy previous array contents. arraySize++;//Increment array size
  • 10. for (int count = 1; count < arraySize; count++) *(newarr + count) = *(aptr + count - 1); delete aptr;//Delete previous array aptr = newarr; } template void SimpleVector::push_back(T val){ // Allocate memory for the array. T *newarr = 0; try { newarr = new T [arraySize + 1]; } catch (bad_alloc) { memError(); } // Copy previous array contents.
  • 11. for (int count = 0; count < arraySize; count++) *(newarr + count) = *(aptr + count); *(newarr + arraySize) = val;//Add value at back of the array arraySize++;//Increment array size delete aptr;//Delete previous array aptr = newarr; } template T SimpleVector::pop_front(){ T dummy = 0; if(arraySize != 0)//If array is not empty then only pop { dummy = *aptr; if(arraySize == 1){ delete aptr; aptr = 0; } else {
  • 12. // Allocate memory for the array. T *newarr = 0; try { newarr = new T[arraySize - 1]; } catch (bad_alloc) { memError(); } // Copy previous array contents. for (int count = 1; count < arraySize; count++) *(newarr + count - 1) = *(aptr + count); delete aptr;//Delete previous array aptr = newarr; } arraySize--;//Decrease array size } return dummy;//Return popped value }
  • 13. template T SimpleVector::pop_back(){ T dummy = 0; if(arraySize != 0)//If array is not empty then only pop { dummy = *(aptr + arraySize - 1); if(arraySize == 1){ delete aptr; aptr = 0; } else { // Allocate memory for the array. T *newarr = 0; try { newarr = new T[arraySize - 1]; } catch (bad_alloc) { memError();
  • 14. } // Copy previous array contents. for (int count = 0; count < arraySize - 1; count++) *(newarr + count) = *(aptr + count); delete aptr;//Delete previous array aptr = newarr; } arraySize--;//Decrease array size } return dummy;//Return popped value } //************************************** // Destructor for SimpleVector class. * //************************************** template SimpleVector::~SimpleVector() { if (arraySize > 0)
  • 15. delete [] aptr; } // memError function. Displays an error message and // terminates the program when memory allocation fails. template void SimpleVector::memError() { cout << "ERROR:Cannot allocate memory. n"; exit(EXIT_FAILURE); } // subError function. Displays an error message and * // terminates the program when a subscript is out of range. * template void SimpleVector::subError() { cout << "ERROR: Subscript out of range.n"; exit(EXIT_FAILURE); }
  • 16. // getElementAt function. The argument is a subscript. * // This function returns the value stored at the sub- * // cript in the array. * template T SimpleVector::getElementAt(int sub) { if (sub < 0 || sub >= arraySize) subError(); return aptr[sub]; } // Overloaded [] operator. The argument is a subscript. * // This function returns a reference to the element * // in the array indexed by the subscript. * template T &SimpleVector::operator[](const int &sub) { if (sub < 0 || sub >= arraySize) subError();