0

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
John
  • 55
  • 1
  • 8
  • Off topic: Before using that template for much, do yourself a favour and [read What is The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) You are violating the heck out of it, and that always ends badly. – user4581301 Mar 22 '16 at 00:27
  • 1
    Interestingly, your assignment doesn't seem to require you to _write_ the binary search. So you could technically use a pre-rolled one like [std::binary_search](http://en.cppreference.com/w/cpp/algorithm/binary_search). But it's probably a good idea for you to write your own and learn from the experience =) – paddy Mar 22 '16 at 00:27
  • 1
    An excellent point, @paddy . I'm going to suggest a slightly different dodge. Write your code using a canned and known-good search and test the stuffing out of it. Then throw in your own search algorithm. No sense testing two sets of new code at the same time. – user4581301 Mar 22 '16 at 00:29
  • @paddy I already have a recursive binary function in mind, the problem is I have no idea how to write it USING the obect – John Mar 22 '16 at 00:31
  • You seem to be confusing responsibilities. The binary search should not be provided by the `SimpleVector` class at all, because there is no guarantee that it is sorted. You have sorted it externally, and thus you should binary search it externally too. Use the same comparison criteria for searching that you used for the sorting. Even better, define `operator<` for your `PhoneBook` structure. In all other regards, the binary search is a standard binary search. – paddy Mar 22 '16 at 00:32
  • @paddy Problem begins when I try to do that I need to define operator [] and then < . And how exactly do I define that one object is bigger than the other since all they hold are 2 MyString values? – John Mar 22 '16 at 00:40
  • And if I were to sort it internally using SimpleVector.h how would I go about doing that? – John Mar 22 '16 at 00:49
  • You will need to provide a `const` overload of your `operator[]` function that returns `const T&`. Then you can define `bool operator<( const Phonebook &, const Phonebook &)` somewhere and use it externally from your `SimpleVector`. – paddy Mar 22 '16 at 01:29

0 Answers0