I've seen this code and it's supposed to be fine, I don't understand why if for example "left + right" is an odd number, doing "a[middle] == 0" doesn't give error because it doesn't exist in arrays decimal positions. Thank you.
/** Searches the value 0 within a[left..right].
* Precondition: 0 <= left, right < a.length,
* a[left..right] is sorted in ascending order. */
public static int intercepts(double[] a, int left, int right) {
if (left > right) { return -1; }
else {
int middle = (left + right) / 2;
if (a[middle] == 0) { return middle; }
else if (a[middle] > 0) {
return intercepts(a, left, middle - 1);
} else {
return intercepts(a, middle + 1, right); }
}
}