Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify SearchableVector header to include a binary search. Use the binary search to search for the number of points scored by a soccer player, pero

Modify SearchableVector header to include a binary search. Use the binary search to search for the number of points scored by a soccer player, pero class Soccer.

//SearchableVector

#ifndef SearchableVect_h #define SearchableVect_h #include "SimpleVector.h"

template class SearchableVector : public SimpleVector { public: // Default constructor SearchableVector() : SimpleVector() {} // Constructor SearchableVector(int size) : SimpleVector(size) {}

// Copy constructor SearchableVector(const SearchableVector&);

// Accessor to find an item int findItem(const T); int binarySearch(const T); };

//******************************************************* // Copy constructor * //*******************************************************

template SearchableVector::SearchableVector(const SearchableVector& obj) :SimpleVector(obj.size()) { for (int count = 0; count < this->size(); count++) this->operator[](count) = obj[count]; }

//******************************************************** // findItem function * // This function searches for item. If item is found * // the subscript is returned. Otherwise 1 is returned. * //********************************************************

template int SearchableVector::findItem(const T item) { for (int count = 0; count <= this->getSize(); count++) { if (SimpleVector::getElementAt(count) == item) return count; } return -1; } #endif

//Simple Vector

#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; int arraySize; 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 getSize() const { return arraySize; }

// Accessor to return a specific element T getElementAt(int position);

// Overloaded [] operator declaration T& operator[](const int&);

void setValue(T value, int index); };

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. "; 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. "; exit(EXIT_FAILURE); }

//******************************************************* // getElementAt function. The argument is a subscript. * // This function returns the value stored at the * // subcript 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::setValue(T value, int index) { aptr[index] = value; }

#endif

//Soccer Class

#include #include using namespace std; class Soccer { friend ostream& operator<<(ostream&, const Soccer&); friend istream& operator>>(istream&, Soccer&); private: string name; // Player's name int number; // Player's number int points; // Points scored by a player public: Soccer(); Soccer(string aName, int aNumber, int p); Soccer(const Soccer&); ~Soccer(); void setName(string); void setNumber(int); void setPoints(int); string getName() const; int getNumber() const; int getPoints() const; Soccer& operator=(const Soccer&); Soccer& operator[](const Soccer& obj); }; char temp2[20] = "No player assigned";

Soccer::Soccer() : name(temp2), number(0), points(0) { int number = 10; int points = 30; } Soccer::Soccer(string aName, int aNumber, int p) : name(aName), number(aNumber), points(p) { setNumber(30); setPoints(20); } Soccer::Soccer(const Soccer& obj) : name(obj.name), number(obj.number), points(obj.points) { }

Soccer::~Soccer() {

}

void Soccer::setName(string aName) { name = aName; }

void Soccer::setNumber(int aNumber) { number = aNumber; }

void Soccer::setPoints(int p) { points = p; }

string Soccer::getName() const { return this->name; }

int Soccer::getNumber() const { return this->number; }

int Soccer::getPoints() const { return this->points; }

Soccer& Soccer::operator=(const Soccer& obj) { this->name = obj.getName(); this->number = obj.getNumber(); this->points = obj.getPoints(); return *this; }

Soccer& Soccer::operator[](const Soccer& obj) { this->name = obj.getName(); this->number = obj.getNumber(); this->points = obj.getPoints(); return *this; }

ostream& operator<<(ostream& output, const Soccer& obj) { output << "Player's name: " << obj.name << endl; output << "Player's number: " << obj.number << endl; output << "Player's scored points: " << obj.points << endl; return output; }

istream& operator>>(istream& input, Soccer& obj) { cin.ignore(); cout << "Nombre: "; input >> obj.name; cout << "Numero: "; input >> obj.number; cout << "Puntos acumulados: "; input >> obj.points; return input; }

MAIN.CPP

HERE, YOU SHOULD DECLARE A CLASS VIA:

int n;

cin >> n;

Soccer tmpPlayer;

SearchableVector SoccerTable(n);

for (int count = 0; count < n; count++) { cin >> tmpPlayer; SoccerTable.setValue(tmpPlayer, count); } //end for

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Use the following method for a binary search.

template int SearchableVector::findItem(const T item) { int low = 0; int high = this->getItemCount() - 1; int mid;

while (low <= high) { mid = (low + high) / 2;

if (this->getElementAt(mid) == item) return mid; else if (this->getElementAt(mid) > item) high = mid - 1; else low = mid + 1; } return -1; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions