Hey guys, Once again, I need help from you guys regarding an assignment that I have. The assignment is about using templates in C++ for binary search of specific variables(integers, characters and strings). So far, what I have written below is what I have come up with, but I am getting a seg fault. Can someone please point me in the right direction?
if the value to be searched is less than mid than high needs to be assigned the value of mid. if value to be searched is more than mid, than low needs to be assigned the value of mid.
Look at your code closely. First and last don't ever change, since those don't change,you don't go further in your search.
int mid=(first+last\2)
mid-1 or mid+1 does nothing when you re-assign it with the same values.
Like this your program will run till it crashes because of an overflow on the stack.
template<class T>
void search(const T a[], int first , int last, T key, bool found, int& location);
// search (stringarray, first, 10, "nonsense", found, location);
search (stringarray, first, 10, std::string("nonsense"), found, location);
Or
1 2 3
template< class T, class U >
void search( const T a[], int first , int last, U key, bool found, int& location );
search (stringarray, first, 10, "nonsense", found, location);