#include /* * return the index of number x in array a or -1 if not found. * Array a must be sorted. */ int search(int x, int size, int *a) { int low = 0; int high = size; while (low < high) { int mid = (low + high) / 2; if (a[mid] == x) return mid; else if (a[mid] > x) high = mid; else low = mid + 1; } return -1; } int main() { int i, a[10], x; for (i = 0; i < 10; i++) scanf("%d", &a[i]); while (printf("Search for: "), scanf("%d", &x) == 1) printf("search for %d yields %d\n", x, search(x, 10, a)); return 0; }