Question
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
// Copy constructor SearchableVector(const SearchableVector&);
// Accessor to find an item int findItem(const T); int binarySearch(const T); };
//******************************************************* // Copy constructor * //*******************************************************
template
//******************************************************** // findItem function * // This function searches for item. If item is found * // the subscript is returned. Otherwise 1 is returned. * //********************************************************
template
//Simple Vector
#ifndef SimpleVector_h #define SimpleVector_h // SimpleVector class template #include
template
// 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
// Initialize the array. //for (int count = 0; count < arraySize; count++) // *(aptr + count) = 0; }
//******************************************* // Copy Constructor for SimpleVector class. * //*******************************************
template
// 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
//******************************************************** // memError function. Displays an error message and * // terminates the program when memory allocation fails. * //********************************************************
template
//************************************************************ // subError function. Displays an error message and * // terminates the program when a subscript is out of range. * //************************************************************
template
//******************************************************* // getElementAt function. The argument is a subscript. * // This function returns the value stored at the * // subcript in the array. * //*******************************************************
template
//******************************************************** // Overloaded [] operator. The argument is a subscript. * // This function returns a reference to the element * // in the array indexed by the subscript. * //********************************************************
template
template
#endif
//Soccer Class
#include
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
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started