Modify the SimpleVector by doubling the arraysize limit. --------SimpleVector.h------- #ifndef SIMPLEVECTOR_H #define SIMPLEVECTOR_H // SimpleVector class template #include #include // Needed for...

90.2K

Verified Solution

Question

Programming

Modify the SimpleVector by doubling the arraysizelimit.

--------SimpleVector.h-------

#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H

// SimpleVector class template

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

// 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 &);

void pshFrnt (T);
void pshBack (T);
T popFrnt();
T popBack();
};

//***********************************************************
// 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);
}

//**************************************
// 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];
}

template
void SimpleVector::pshFrnt(T val){
//Allocate the new Array
int newSize = arraySize+1;
T* newArray = new T[newSize];
//Copy the old array into the array offset from front by 1
for (int i = 1;i< arraySize;i++){
newArray[i]= aptr[i];
}
//Place the new value at the front of the new array
newArray[0]= val;
//Increment the size property by 1
  
//Delete the old array
delete [] aptr;
aptr = NULL;
//Set the old array pointer to the new pointer
aptr = newArray;
}


template
T SimpleVector::popFrnt(){
//Allocate the new Array
int newSize = arraySize-1;
T* newArray = new T[newSize];
//Store the value at the front of array to later return
int frnt;
frnt = aptr[0];
//Copy the old array into the array offset from front by 1
arraySize = newSize;
for (int i = 0;i< arraySize;i++){
newArray[i]= aptr[i];
}
//Place the new value at the front of the new array
//newArray[0]= val;
//dec crement the size property by 1
  
//Delete the old array
delete [] aptr;
aptr = NULL;
//Set the old array pointer to the new pointer
aptr = newArray;
//Return the front value
return frnt;
  
}

template
void SimpleVector::pshBack(T val){
  
int newSize = arraySize + 1;
T* newArray = new T[newSize];
  
for(int i=0;i newArray[i]=aptr[i];
}
newArray[arraySize-1]= val;
  
delete [] aptr;
aptr = NULL;

aptr= newArray;
}

template
T SimpleVector::popBack(){
  
int newSize = arraySize - 1;
T* newArray = new T[newSize];
  

for(int i=0;i newArray[i]=aptr[i+1];
}
T tmpVal=aptr[0];
  
aptr= newArray;
return tmpVal;
}

#endif /* SIMPLEVECTOR_H */

----main.cpp-------

//System Libraries
#include
using namespace std;

//User Libraries
#include \"SimpleVector.h\"

//Global Constants

//Function prototypes
void fillVec(SimpleVector &);
void prntVec(SimpleVector &,int);

//Execution Begins Here
int main(int argc, char** argv) {
//Declare Variables
unsigned int size=200;
SimpleVector sv(size);
  
//Fill the Vector
fillVec(sv);
  
//Print the Vector
prntVec(sv,10);
  
//Copy the Vector
SimpleVector copysv(sv);
  
copysv.popBack();
copysv.popFrnt();
copysv.pshFrnt(14);
copysv.pshBack(12);
//Print the Vector
prntVec(copysv,10);

return 0;
}

void prntVec(SimpleVector &sv,int perLine){
cout< for(int i=0;i cout< if(i%perLine==(perLine-1))cout<}
cout< }

void fillVec(SimpleVector &sv){
for(int i=0;i sv[i]=rand()%26+65;
}
}

Answer & Explanation Solved by verified expert
3.6 Ratings (492 Votes)
Here is the completed code for this problem Comments are included go through it learn how things work and let me know if you have any doubts or if you need anything to change If you are satisfied with the solution please rate the answer If not PLEASE let me know before you rate Ill help you fix whatever issues Thanks ifndef SIMPLEVECTORH define SIMPLEVECTORH SimpleVector class template include include Needed for badalloc 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 int currCapacity current capacity void memError Handles memory allocation errors void subError Handles subscripts out of range public Default constructor SimpleVector currCapacity 10 using default capacity of 10    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students