Here is my helpers.c file, I have been scouring the message boards and nothing is making sense. I use the pset3 input of ./generate 1000 50 | ./find 127. I get a Didn't find needle in haystack message. Please help. I apologize for the poor format, this is my first time posting to stackexchange.
/**
* helpers.c
*
* Computer Science 50
* Problem Set 3
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include "helpers.h"
bool BinarySearch(int key, int array[], int min, int max);
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
return BinarySearch(value, values, n-n, n-1);
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
int i, element, j;
for(i=1;i <n;i++)
{
element = values[i];
j=i;
while (j > 0 && values[j-1] > element)
{
values[j]=values[j-1];
j=j-1;
}
values[j]=element;
}
// TODO: implement an O(n^2) sorting algorithm
return;
}
bool BinarySearch(int key, int array[], int min, int max)
{
//find midpoint
int midpoint = (max-min)/2;
// check if input is valid
if(min > max || midpoint > max || midpoint < min)
{
return false;
}
//check if midpoint=key
if(array[midpoint]==key || array[min] == key||array[max]==key)
{
return true;
}else if(array[midpoint] < key) // check if midpoint is larger than the key
{
BinarySearch(key, array, midpoint+1, max);
}else if(array[midpoint] > key) //check if midpoint is larger than key
{
BinarySearch(key, array, min, midpoint -1);
}
return false;
}