bool search(int value, int values[], int n)
{
// TODO: implement a searching algorithm
if (value < 0)
return false;
int s = 0;
int e = n - 1;
int m = ((s + e) / 2);
while (s <= e)
{
if (value == values [m])
return true;
else if (value > values [m])
{
s = m + 1;
m = ((s + e) / 2);
}
else
{
e = m - 1;
m = ((s + e) / 2);
}
}
return false;
}
"...haystack[998] =
haystack[999] =
haystack[1000] =
Didn't find needle in haystack.
~/workspace/pset3/find/ $ echo $? 1..."
The above is what I get after performing ./generate 1000 50 | ./find 127
as per CS50 testing instructions.
Above is my code for the search function where I think the issue is. Anyone had a similar problem?
I also seem to have issues with the sort function according to check50:
void sort(int values[], int n)
{
// TODO: implement a sorting algorithm
int m, c;
for (int i = 0; i < n - 1; i++)
{
m = i;
for (int j = i + 1; j < n; j++)
{
if (values [j] < values [m])
m = j;
}
if (m != i)
{
c = values [m];
values [i] = values [m];
values [m] = c;
}
}
return;
}