/* * return the index of number x in array a or -1 if not found. * Array a must be sorted by student number. */ int search(int x, int nstudents, struct studentinfo *a) { int low = 0; int high = nstudents; while (low < high) { /* * invariant: if search item is in array, it is at an index i such * that low <= i < high. */ int mid = (low + high) / 2; if (a[mid].number == x) return mid; else if (a[mid].number > x) high = mid; else low = mid + 1; } return -1; }