I am working on Pset3 for a binary search algorithm. Below is my code (NOTE: the logic is not finished):
bool search(int value, int values[], int n)
{
//binary search algorithm
int min = 0;
int max = n-1;
int midpoint;
int midpoint2 = 0;
midpoint2 += 0;
if ((n-1) % 2 == 0) {
midpoint = (max+min)/2;
}
else {
midpoint = (int) (max+min)/2;
midpoint2 = ((int) (max+min)/2) + 1;
}
printf("min: %i, max: %i, mid: %i\n", min,max,midpoint);
return true;
bool sorted = false;
do {
printf("beginning loop");
if (values[midpoint] == value || values[midpoint2] == value) {
return true;
printf("1");
}
else if (n == 1) {
return false;
printf("2");
}
else{
for (int c = midpoint - 1 ; c < n - 1 ; c++ ) {
int b = values[c];
values[c] = values[c+1];
printf("value index %i used to be %i\n",c,b);
printf("value index %i is now %i\n",c,values[c]);
}
}
}
while (sorted == false);
}
When I run find.c, my do-while loop never actually executes. I have tested this by placing the debug print statement "beginning loop", which never prints to the screen. I never see 1, 2, or "value index.. " print statements either. Please help!