I wrote helpers.c according to Zamyla's walkthroughs. My code seems quite well to me. I checked it with check50 and it passes.
~/workspace/pset3/find/ $ check50 2016.find.less helpers.c
:) helpers.c exists
:) helpers.c compiles
:) finds 42 in {42,43,44}
:) finds 42 in {41,42,43}
:) finds 42 in {40,41,42}
:) finds 42 in {41,42,43,44}
:) finds 42 in {40,41,42,43}
:) finds 42 in {39,40,41,42}
:) doesn't find 42 in {39,40,41}
:) doesn't find 42 in {39,40,41,43}
:) finds 42 in {42,40,39,41}
https://sandbox.cs50.net/checks/1e923dc608884ac0a28322482042a5b5
But, when I run it with
$ ./find 42
50
42
^d
it shows Didn't find needle in haystack.
and so with ./generate 1000 50 | ./find 127
too. It simply doesn't work. Is there anything I missed?
My helpers.c code is
/**
* helpers.c
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
// My binary search algorithm
int start = 0;
int end = n - 1;
while (end >= start)
{
int mid = (start + end) / 2;
if (values[mid] == value)
return true;
else if (value > values[mid])
start = mid + 1;
else if (value < values[mid])
end = mid - 1;
}
return false;
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
// My selection sort algorithm - O(n^2)
int min_index, swap;
for (int i = 0; i < n - 2; i++)
{
for (int j = i + 1; j < n - 1; j++)
{
if (values[j + 1] < values[j])
min_index = j + 1;
else if (values[j] < values[j + 1])
min_index = j;
}
if (values[min_index] < values[i])
{
swap = values[min_index];
values[min_index] = values[i];
values[i] = swap;
}
}
}