I was instructed to write a binary search function which would return true if an element, inputted by the user, was found in the array, and false if it was not. I'm not sure why, but my function always returns false. My code is as follows.


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//binary search function
bool search (int array[], int item)
{
int high=99;
int low=0;
while(high>low and high!=low)
{
int mid=((low+high)/2);
if (item==array[mid])
{
return true;
}
else if (item<array[mid])
{
high=(mid-1);
}
else if (item>array[mid])
{
low=(mid+1);
}
}
return false;
}

//main to test function
int main()
{
const int n=100;
int a[n];
srand ( time(0) );
for (int g=0;g<n;g++)//inputting random #s into the array, as instructed
{
int p = rand() % 1001;
if (p<0)
{
p=p*-1;
}
a[g]=p;
}
//bubble sort to make the binary search run properly
for(int x=0; x<n; x++)

{

for(int y=0; y<n-1; y++)

{

if(a[y]>a[y+1])

{

int temp = a[y+1];

a[y+1] = a[y];

a[y] = temp;

}

}

}
for (int r=0; r<n; r++) //outputting the array so I know that the things I ask it to search for are/are not in the array
{
cout << a[r] << " ";
}
int item;
cout << "Enter an item to search for: ";
cin >> item;
if (search(a, item))
cout << "The number being searched for was found in the array." << endl;
else
cout << "The number being searched for was not found in the array." << endl;
return 0;
}

Any words of wisdom as to why my code does not fulfill it's purpose would be appreciated.