If array is sorted just use STL
int *pos = lower_bound(arr, arr + n ) ;
print *pos;
-Dhyanesh
On 5/21/06, adak <[EMAIL PROTECTED]> wrote:
>
> This is a "normal" binary search. You just need to have it return the
> next lower number if a match is not found.
> And of course, get rid of the superflous database records. sr[] is the
> array of student records.
>
> /* Number_Search uses a binary search, to search for a record's student
>
> number. */
>
> int Number_Search(int target) {
> int low, mid, high;
> low = 1; high = Hi;
>
> while(!target) {
> printf("\n Enter the Student Number: ");
> scanf("%d", &target);
> }
> while(low <= high) {
> mid = (low + high) / 2;
> if(sr[mid].sn == target) { /* is it a match ? */
> /* PrintHeader(); */
>
> printf("\n %5d %-11s %c %2d %-22s", sr[mid].sn,
> sr[mid].name, sr[mid].sex, sr[mid].age, sr[mid].major);
>
> return mid;
> } else if(sr[mid].sn < target) /* is our guess too low? */
> low = mid + 1; /* then increase the mid point */
> else
> high = mid - 1; /* too high, so decrease range */
> }
> printf("\n No Match ");
>
> /* If no match, add your return to include the number in the array
> which is just less than the match previously requested. */
> return 0; /* no match here */
> }
>
> Adak
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---