I try inputting ./find 31 and then the list 30, 31, 32 and it still says it can't find the needle in the haystack. :( I don't know if it's my search function, my sort function, or both. Help! The program compiles correctly, so I don't know what the problem is...
Here's my code, please let me know what I'm doing wrong.
bool search(int value, int values[], int n)
{
// implement a searching algorithm
int beginning = 0;
int ending = n - 1;
while (ending >= beginning)
{
int midpoint = (beginning + ending)/2;
if (values[midpoint] == value)
{
return true;
}
else if (values[midpoint] > value)
{
ending = midpoint - 1;
}
else
{
beginning = midpoint + 1;
}
}
return false;
}
void sort(int values[], int n)
{
// implements a bubble sort sorting algorithm
int counter;
do
{
counter = 0;
for (int i = 0; i < n; i++)
{
if (values[i] > values[i+1])
{
values[i] = values[i+1];
values[i+1] = values[i];
counter = counter + 1;
}
}
}
while (counter > 0);
}