I'm trying to write a function that gets an array, it's size and the number I'm looking for. The function should be recursive.
That's what I've written, it doesn't work. I don' understand why. Suggestions are welcome.
#include <stdio.h>
int binary (int *a, int n, int num)
{
int temp = n/2;
int c = -1;
if (n==1)
{
if (*a == num)
return (*a-1);
else return c;
}
if (a[temp] < num)
return binary ((a+temp+1), (temp-1), num);
else return binary (a, (temp+1), num);
return (a + temp) ;
}
int main (void)
{
int a[5] = {1, 2, 3, 4, 5};
int n = 5, num =0;
printf ("%d", binary (a, n, num));
return 0;
}
Plus, I'm looking for programs that use backtracking, or some explanation on te subject of backtracking. Anyone know any helpful sites?
Thanks, S.u.s.h.i.