I am working on helpers.c in pset3. I have the following code. Find.c compiles with helpers.c, but when I run it with "./generate 1000 50 | ./find 127", it lists lines with "haystack[n] =" where n = every number between 1 and 1000. And then it says it didn't find a needle in the haystack. I am not sure what the listing of the "haystack[n]" is, or how to fix my code from here.
Here is the helpers.c code I am using:
#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)
{
// Set values for the top and bottom of the search
int lower = values[0];
int upper = values[n-1];
//Find middle
int middle = (upper + lower) / 2;
// Binary search
while (lower <= upper)
{
//Compare value to middle, cut search in half
if (values[middle] == n)
{
return true;
}
else if (values[middle] < n)
{
lower = middle + 1;
}
else if (values[middle] > n)
{
upper = middle - 1;
}
}
return false;
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm
// Cycle through array
for (int k = 0; k < n - 1; k++)
{
//Optimize; check if there are no swaps
int swaps = 0;
//Swap adjacent elements if out of order
//Iterate through the array
for (int i = 0; i < n - 1 - k; i++)
{
// Check if array[i] and array[i+1] are in order
if (values[i] > values[i+1])
{
int temp = values[i+1];
values[i+1] = values[i];
values[i] = temp;
swaps++;
}
else if (!swaps)
{
break;
}
}
return;
}
return;
}