#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) { if (size > 0) { int mid = size / 2; if (a[mid] == x) return mid; else if (a[mid] > x) return search(x, mid, a); else { int subsearch = search(x, size - (mid + 1), a + mid + 1); if (subsearch == -1) return subsearch; else return subsearch + 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; }