This is pset3 and I've tried making it work for numerous hours. It always returns false whether the 'needle' is found in the 'haystack' of arrays or not.
When typing this in the command window: ./generate 1000 50 | ./find 2008
It says "Didn't find needle in haystack." when actually, 2008 does exist in the haystack.
bool search(int value, int values[], int n)
{
//Binary Search with Recursion
int start;
int end;
int middle = n/2;
if(n==1)
{
if(value==values[middle])
{
return true;
}
else
{
return false;
}
}
else if(value<values[middle])
{
start = 0;
end = middle-1;
n = n/2;
int new_values[n];
//Add relevant values to the new array
for(int i=0; i<n; i++)
{
new_values[i]=values[start];
start++;
}
//Recurse
search(value, new_values, n);
return false;
}
else if(value>values[middle-1])
{
start = middle;
end = n;
n = n/2;
//Add relevant values to the new array
int new_values[n];
for(int i=0; i<n; i++)
{
new_values[i]=values[start];
start++;
}
//Recurse
search(value, new_values, n);
return false;
}
else
{
return false;
}
}