# pset3. binary search \ expected an exit code of 0, not 1

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;
}
``````

• No, not as command parameters. You execute the code with a single parameter, the number to find, `./find 42` will search for 42. Next, it will ask you to input the numbers in the array. Keep entering numbers, one at a time until you have entered your list of numbers. Once all the numbers are entered, hit CTRL-D to terminate the data input. The program will then execute. Dec 6, 2015 at 21:15