int location(int low,int high);
int main(void) {
int n, num, x, i = 0;
//int arr[3];
//printf ("Please enter in order");
//for (i = 0; i < 3; i++) {
// // scanf("%d", arr[i]);
//}
printf ("Please enter a number to search");
scanf("%d", &x);
printf("%d\n",location(0,2));
return 0;
} int location(int low,int high) {
int mid;
if (low > high)
return 0;
else {
mid = (low + high) / 2;
if (x == arr[mid])
return mid;
else if (x < arr[mid])
return location(low, mid - 1);
else
return location(mid + 1, high);
}
}
Only two values of low and high as parameters are taken as factors, and binary search is implemented in a recursively. I didn't get x and arr on the flymeter.How do I implement it? I want help.
c recursive binary-search
Inside the binary search recursive function, the x and arr values are somehow fixed. In the divisional conquest phase, the x and arr values are invariant and you don't have to hand them over because you just need to refer to them.
Then! It's pre-declared to the outside so that it can be referenced inside a recursive function.
P.S. But why do we have this problem...? Is it an extreme experience?
© 2024 OneMinuteCode. All rights reserved.