Question

In: Computer Science

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

Modify the SimpleVector by doubling the arraysize limit.

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

Solutions

Expert Solution

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, I’ll help you fix whatever issues. Thanks

#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H

// SimpleVector class template

#include <iostream>
#include <stdexcept> // Needed for bad_alloc exception
#include <cstdlib> // Needed for the exit function
using namespace std;

template <class T>
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
        aptr = new T[currCapacity];
        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 <class T>
SimpleVector<T>::SimpleVector(int s)
{
    currCapacity = s > 0 ? s : 10; //using s as capacity if s>0, else using 10
    // Allocate memory for the array.
    try {
        aptr = new T[currCapacity];
    }
    catch (bad_alloc) {
        memError();
    }

    // Initialize the array.
    for (int count = 0; count < currCapacity; count++)
        *(aptr + count) = 0;
    arraySize = currCapacity;
}

//*******************************************
// Copy Constructor for SimpleVector class. *
//*******************************************

template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector& obj)
{
    //copying capacity
    currCapacity = obj.currCapacity;
    // Copy the array size.
    arraySize = obj.arraySize;

    // Allocate memory for the array.
    aptr = new T[currCapacity];
    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 <class T>
SimpleVector<T>::~SimpleVector()
{
    if (currCapacity > 0)
        delete[] aptr;
}

//*******************************************************
// memError function. Displays an error message and *
// terminates the program when memory allocation fails. *
//*******************************************************

template <class T>
void SimpleVector<T>::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 <class T>
void SimpleVector<T>::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 <class T>
T SimpleVector<T>::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 <class T>
T& SimpleVector<T>::operator[](const int& sub)
{
    if (sub < 0 || sub >= arraySize)
        subError();
    return aptr[sub];
}

template <class T>
void SimpleVector<T>::pshFrnt(T val)
{
    //checking if there is enough space
    if (arraySize == currCapacity) {
        //array is full, doubling size of the array
        currCapacity = currCapacity * 2;
        T* newArray = new T[currCapacity];
        //copying elements to new array, leaving first location blank
        for (int i = 0; i < arraySize; i++) {
            newArray[i + 1] = aptr[i];
        }
        //adding val to first location
        newArray[0] = val;
        //Delete the old array
        delete[] aptr;
        aptr = NULL;
        //Set the old array pointer to the new pointer
        aptr = newArray;
    }
    else {
        //array is not full, shifting elements to one place right, adding new element to index 0
        for (int i = arraySize; i > 0; i--) {
            aptr[i] = aptr[i - 1];
        }
        //adding val to first location
        aptr[0] = val;
    }
    //updating size
    arraySize++;
}

template <class T>
T SimpleVector<T>::popFrnt()
{
    //fetching front value
    T front = aptr[0];
    //shifting remaining elements to left
    for (int i = 0; i < arraySize - 1; i++) {
        aptr[i] = aptr[i + 1];
    }
    //updating size, returning removed value
    arraySize--;
    return front;
}

template <class T>
void SimpleVector<T>::pshBack(T val)
{
    //checking if there is enough space or not
    if (arraySize == currCapacity) {
        //array is full, doubling size of the array
        currCapacity = currCapacity * 2;
        T* newArray = new T[currCapacity];
        //copying elements to new array
        for (int i = 0; i < arraySize; i++) {
            newArray[i] = aptr[i];
        }
        //replacing old array with new
        delete[] aptr;
        aptr = NULL;
        aptr = newArray;
    }
    //adding val to index=arraySize and then incrementing arraySize
    aptr[arraySize++] = val;
}

template <class T>
T SimpleVector<T>::popBack()
{
    //fetching value at arraySize-1, updating size, returning value
    T value = aptr[arraySize - 1];
    arraySize--;
    return value;
}

#endif /* SIMPLEVECTOR_H */
#include <iostream>
#include "SimpleVector.h"
using namespace std;



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

//Execution Begins Here
int main(int argc, char** argv)
{
    //Declare Variables
    unsigned int size = 200;
    SimpleVector<int> sv(size);

    //Fill the Vector
    fillVec(sv);

    //Print the Vector
    prntVec(sv, 10);

    //Copy the Vector
    SimpleVector<int> copysv(sv);

    copysv.popBack();
    copysv.popFrnt();
    copysv.pshFrnt(14);
    copysv.pshBack(12);
    //Print the Vector
    prntVec(copysv, 10);

    return 0;
}

void prntVec(SimpleVector<int>& sv, int perLine)
{
    for (int i = 0; i < sv.size(); i++) {
        cout << sv[i] << " ";
        if (i % perLine == (perLine - 1))
            cout << endl;
    }
    cout << endl;
}

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

/*OUTPUT*/

80 72 81 71 72 85 77 69 65 89
76 78 76 70 68 88 70 73 82 67
86 83 67 88 71 71 66 87 75 70
78 81 68 85 88 87 70 78 70 79
90 86 83 82 84 75 74 80 82 69
80 71 71 88 82 80 78 82 86 89
83 84 77 87 67 89 83 89 89 67
81 80 69 86 73 75 69 70 70 77
90 78 73 77 75 75 65 83 86 87
83 82 69 78 90 75 89 67 88 70
88 84 76 83 71 89 80 83 70 65
68 80 79 79 69 70 88 90 66 67
79 69 74 85 86 80 86 65 66 79
89 71 80 79 69 89 76 70 80 66
78 80 76 74 86 82 86 73 80 89
65 77 89 69 72 87 81 78 81 82
81 80 77 88 85 74 74 76 79 79
86 65 79 87 85 88 87 72 77 83
78 67 66 88 67 79 75 83 70 90
75 86 65 84 88 68 75 78 76 89

14 72 81 71 72 85 77 69 65 89
76 78 76 70 68 88 70 73 82 67
86 83 67 88 71 71 66 87 75 70
78 81 68 85 88 87 70 78 70 79
90 86 83 82 84 75 74 80 82 69
80 71 71 88 82 80 78 82 86 89
83 84 77 87 67 89 83 89 89 67
81 80 69 86 73 75 69 70 70 77
90 78 73 77 75 75 65 83 86 87
83 82 69 78 90 75 89 67 88 70
88 84 76 83 71 89 80 83 70 65
68 80 79 79 69 70 88 90 66 67
79 69 74 85 86 80 86 65 66 79
89 71 80 79 69 89 76 70 80 66
78 80 76 74 86 82 86 73 80 89
65 77 89 69 72 87 81 78 81 82
81 80 77 88 85 74 74 76 79 79
86 65 79 87 85 88 87 72 77 83
78 67 66 88 67 79 75 83 70 90
75 86 65 84 88 68 75 78 76 12

Related Solutions

#ifndef PROJ7_MYVECTOR #define PROJ7_MYVECTOR #include "proj7-ContainerIfc.h" template <class T> class MyVector : public ContainerIfc<T> { public:...
#ifndef PROJ7_MYVECTOR #define PROJ7_MYVECTOR #include "proj7-ContainerIfc.h" template <class T> class MyVector : public ContainerIfc<T> { public: /** * MyVector * * This is the default constructor that sets size equal * to 0 and capacity to 10. * * Parameters: none * * Output: * return: none * reference parameters: none * stream: none */ MyVector(); /** * ~MyVector * * This is the destructor that deletes memory * * Parameters: none * * Output: * return: none * reference...
#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double...
#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double value);     void    Clear();     void    Divide(double value);     double  GetValue() const;     void    Multiply(double value);     void    SetValue(double  newValue);     void    Subtract(double value);    private:     // data members     double  m_total; };    #endif // CCALC_HEADER int     main() {     CCalc       calculator;     char        choice;        // loop and let the user manipulate the calculator     do {         // display the menu and get the user selection         DisplayMenu();         cout <<...
#ifndef CONTAINER_H #define CONTAINER_H using namespace std; class container { public: // CONSTRUCTOR container(); ~container(); container(const...
#ifndef CONTAINER_H #define CONTAINER_H using namespace std; class container { public: // CONSTRUCTOR container(); ~container(); container(const container& c); container& operator=(const container& c) // MEMBER FUNCTIONS bool lookup(const int& target); void remove(const int& target); void add(const int& target); private: struct node{ int key; node *next; }; node* head; node* tail; }; #endif For a linked list based implementation of a container class shown above, implement the constructor, copy constructor, destructor and copy assignment functions.
This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start...
This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start of the marco guard // Try not to change this file #include <iostream> #include <cmath> using std::cout; using std::endl; using std::istream; using std::ostream; class Point { private:    double x, y, z; public:    // Constructors    Point();    Point(double inX, double inY, double inZ = 0);    Point(const Point& inPt);    // Get Functions    double getX() const;    double getY() const;   ...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha so the main function is working properly. #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class alpha { private: int data; public: //YOUR CODE }; //////////////////////////////////////////////////////////////// int main() { alpha a1(37); alpha a2; a2 = a1; cout << "\na2="; a2.display(); //display a2 alpha a3(a1); //invoke copy constructor cout << "\na3="; a3.display(); //display a3 alpha a4 = a1; cout << "\na4="; a4.display(); cout << endl; return 0;...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
Lower Class Limit Upper Class Limit Frequency 60 64 3 65 69 7 70 74 3...
Lower Class Limit Upper Class Limit Frequency 60 64 3 65 69 7 70 74 3 75 79 7 80 84 7 85 89 11 90 94 10 95 99 9 100 104 12 (a) What is the 11th percentile for the following Grouped Frequency Data Table? (b) What is the 4th decile for the following Grouped Frequency Data Table?
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
Double Linked Lists: Implement the PutItem and DeleteItem function // ItemType.h #ifndef ITEMTYPE_H #define ITEMTYPE_H enum...
Double Linked Lists: Implement the PutItem and DeleteItem function // ItemType.h #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void Initialize(int number);     int getValue() const;     RelationType ComparedTo(ItemType); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::Initialize(int number) {     value = number; } RelationType ItemType::ComparedTo(ItemType otherItem) {         if(value < otherItem.value)             return LESS;         else if...
//BEGIN--TABLE.H-- #ifndef TABLE_H #define TABLE_H #include        // Provide string #include // Provide hash #include...
//BEGIN--TABLE.H-- #ifndef TABLE_H #define TABLE_H #include        // Provide string #include // Provide hash #include        // Provide list using namespace std; class Table { public:    // MEMBER CONSTANT    static const unsigned int TABLE_SIZE = 13;    Table() { total_records = 0; }    virtual ~Table() { }    unsigned int get_total() { return total_records; }    virtual void insert(string key) =0;    virtual void print() =0;    protected:    unsigned int total_records;    // HELPER...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT