First, thanks for helping me on this!
My search function work with the tests: ./generate 1000 50 | ./find 127 -> "Found needle in haystack!" ./generate 1000 50 | ./find 128 -> "Didn't find needle in haystack."
However, it fails the check50 2014.fall.pset3.find helpers.c:
:) helpers.c exists
:) helpers.c compiles
:) finds 42 in {42,43,44}
:( finds 42 in {41,42,43} \ expected an exit code of 0, not 1
:( finds 42 in {40,41,42} \ expected an exit code of 0, not 1
:( finds 42 in {41,42,43,44} \ expected an exit code of 0, not 1
:( finds 42 in {40,41,42,43} \ expected an exit code of 0, not 1
:( finds 42 in {39,40,41,42} \ expected an exit code of 0, not 1
:) doesn't find 42 in {39,40,41}
:) doesn't find 42 in {39,40,41,43}
This is my search function - any ideas?
bool search(int value, int values[], int n)
{
// false if n is negative
if (n < 0)
{
return false;
}
// binary search
int min = 0;
int max = n-1;
int mid;
// check as long as there is a list
while (min <= max)
{
mid = (min + max) / 2;
if (values[mid] == value)
{
return true;
}
else if (value < values[mid])
{
max = mid - 1;
}
else
{
min = mid + 1;
}
}
return false;
}
void sort(int values[], int n)
{
// bubble sort
int swap = true;
// go through the values at least once
do
{
swap = false;
int new_i;
for (int i = 0; i <= n; i++)
{
// switch values, if left is bigger than right
if(values[i] > values[i+1])
{
new_i = values[i+1];
values[i+1] = values[i];
values[i] = new_i;
swap = true;
}
}
}
// if there was a swap, go again
while (swap == true);
return;
}