I am testing by using ./generate 1000 50 | ./find 127
. The needle should be found but isn't.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
if (n<1)
{
return false;
}
// TODO: implement a detecting algorithm, if found end loop
int max=n-1;
int min=0;
while ((max-min)>0)
{
int mid = ((min+max)/2);
if (values[mid]==value)
return true;
if (values[mid]<value)
min = mid+1;
if (values[mid]>value)
max = mid-1;
}
return false; //if not found, return false
}