I have an assignment and have been trying my best to figure this out The assignment says the following:
"Write a program that creates at least five objects and stores them in a SimpleVector. After the objects are stored in the SimpleVector , use a binary recursive search function to locate and then display the result"
My code so far:
#include <iostream>
#include <iomanip>
#include "Phonebook.h"
#include "C:\Users\John\Google Drive\C++ Projects\CECS2223\MyString\MyString.h"
#include "SimpleVector.h"
using namespace std;
const int SIZE = 5;
int main()
{
SimpleVector<Phonebook> Book(SIZE); // Create object inside the template
Book[0].number = "787-254-5874"; // Initialization of objects
Book[0].name = "Pepito Perez";
Book[1].number = "787-925-9872";
Book[1].name = "Rosa Lennis";
Book[2].number = "787-555-6981";
Book[2].name = "Juan E. Aristizabal";
Book[3].number = "787-659-3325";
Book[3].name = "Andrea Rivera";
Book[4].number = "787-148-6932";
Book[4].name = "Elsie Colon";
int i, j;
bool flag = 1;
MyString tempName, tempNumber;
for (i = 1; (i <= SIZE) && flag; i++) // Loop for bubble sorting objects
{
flag = 0;
for (j = 0; j < (SIZE - 1); j++)
{
if (Book[j + 1].name < Book[j].name)
{
tempName = Book[j].name;
Book[j].name = Book[j + 1].name;
Book[j + 1].name = tempName;
tempNumber = Book[j].number;
Book[j].number = Book[j + 1].number;
Book[j + 1].number = tempNumber;
flag = 1;
}
}
}
return 0;
}
So far I have already created the object inside the template and initialized it's values. The class Phonebook stores 2 MyString objects which are initialized at the beginning of the code and a bubble sort using the MyString operators to organize the objects from lowest to highest so the Binary Search function can work.
My issue starts when I need to make the binary search function, how do I do that exactly? I know that the idea is to use the template for it since it's storing the objects, but nothing in the book comes close to this exercise. I should also make the sorting a function of the Phonebook class, but I'm not sure of how to do that either
Here is the SimpleVector.h
#pragma once
#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.\n";
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