Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simple Vector Modify the program below to include the member functions push_back

ID: 3621589 • Letter: S

Question

Simple Vector

Modify the program below to include the member functions push_back and pop_back. These functions should emulate the STL vector class member functions of the same name.

The push_back functon should accept an argument, and insert its value at the end of the array. The pop_back function should accept no argument, and remove the last element from the array. Test the class with a driver program.

program:
// This program demonstrates the SimpleVector template.
#include <iostream>
#include "SimpleVector.h"
using namespace std;

int main()
{
const int SIZE = 10;

SimpleVector<int> intTable(SIZE);
SimpleVector<double> doubleTable(SIZE);

// Store values in the arrays.
for (int x = 0; x < SIZE; x++)
{
intTable[x] = (x * 2);
doubleTable[x] = (x * 2.14);
}

// Display the values in the arrays.
cout << "These values are in intTable: ";
intTable.print();
cout << "These values are in doubleTable: ";

doubleTable.print();

// Use the built-in + operator on array elements.
for (int x = 0; x < SIZE; x++)
{
intTable[x] = intTable[x] + 5;
doubleTable[x] = doubleTable[x] + 1.5;
}
// Display the values in the array.
cout << "These values are in intTable: ";
intTable.print();
cout << "These values are in doubleTable: ";
doubleTable.print();

// Use the built-in ++ operator on array elements.
for (int x = 0; x < SIZE; x++)
{
intTable[x]++;
doubleTable[x]++;
}
// Display the values in the array.
cout << "These values are in intTable: ";
intTable.print();
cout << "These values are in the doubleTable: ";
doubleTable.print();
cout << endl;
return 0;
}

header:
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H

#include <iostream>
#include <cstdlib>
using namespace std;

template <class T>
class SimpleVector
{
private:
T *aptr;
int arraySize;
void subError(); // Handles subscripts out of range
public:
SimpleVector(int); // Constructor
SimpleVector(const SimpleVector &); // Copy constructor
~SimpleVector(); // Destructor
int size()
{ return arraySize; }
T &operator[](int); // Overloaded [] operator
void print(); // outputs the array elements.
};

//*******************************************************
// Constructor for SimpleVector class. Sets the size *
// of the array and allocates memory for it. *
//*******************************************************
template <class T>
SimpleVector<T>::SimpleVector(int s)
{
arraySize = s;
aptr = new T [s];
for (int count = 0; count < arraySize; count++)
aptr[count] = T();
}
//******************************************************
// Copy Constructor for SimpleVector class. *
//******************************************************
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector &obj)
{
arraySize = obj.arraySize;
aptr = new T [arraySize];
for(int count = 0; count < arraySize; count++)
aptr[count] = obj[count];
}
//*****************************************************
// Destructor for SimpleVector class. *
//*****************************************************
template <class T>
SimpleVector<T>::~SimpleVector()
{
if (arraySize > 0)
delete [] aptr;
}

//******************************************************
// 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. ";
exit(0);
}
//*******************************************************
// 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[](int sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
//********************************************************
// prints all the entries is the array. *
//********************************************************
template <class T>
void SimpleVector<T>::print( )
{
for (int k = 0; k < arraySize; k++ )
cout << aptr[k] << " ";
cout << endl;
}
#endif

Explanation / Answer

May be similar enough to help you: http://www.cramster.com/answers-nov-10/computer-science/data-structure-write-program-implement-stack-algorithm-1-initial_1049747.aspx

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote