I have no clue why this search isn't working.
Two majors problems
(1) As is it always returns false
(2) It would recognize valid IDs if instead of setting found to true, I just returned true, but I don't want to do it like that, and if there was an invalid account number it would go into an infinite loop
I understand the algorithm but somthing in the lopic of the code is eluding me.
bool binarySearch(int *validAccounts, int length, int account)
{
int first = 0;
int last = length;
int middle = (first + last) / 2;
bool found = false;
while(!found && first <= last )
{
middle = (first + last) / 2;
if(validAccounts[middle] == account)
{
found = true;
}
else if(account < validAccounts[middle])
{
last = middle - 1;
}
else
{
first = middle + 1;
}
}
return false;
}