idastan97

CSCI151 L27 P1

Nov 10th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef struct {
  5.     int id;
  6.     char name[41];
  7. } student;
  8.  
  9. const int size = 8;
  10.  
  11. const student roster[8] =
  12.         {{ 100001, "Ziggy Marley" },
  13.         { 123456, "Abe Lincoln" },
  14.         { 239484, "Jed Clampett" },
  15.         { 443892, "Marge Simpson" },
  16.         { 517840, "Betty Britain" },
  17.         { 780556, "David Bowie" },
  18.         { 900154, "Gore Vidal" },
  19.         { 999900, "Nursultan Jones" } };
  20.  
  21. int findStudent(int id) {
  22.     int min=0, max=7;
  23.     while (min<=max){
  24.         int mid=(max+min)/2;
  25.         if (roster[mid].id==id){
  26.             return mid;
  27.         } else if (id>roster[mid].id){
  28.             min=mid+1;
  29.         } else {
  30.             max=mid-1;
  31.         }
  32.     }
  33.     return -1;
  34. }
  35.  
  36. int main(void) {
  37.  
  38.     printf("Test 0: %i \n", findStudent(99));
  39.     printf("Test 1: %i \n", findStudent(100001));
  40.     printf("Test 2: %i \n", findStudent(123456));
  41.     printf("Test 3: %i \n", findStudent(300000));
  42.     printf("Test 4: %i \n", findStudent(780556));
  43.     printf("Test 5: %i \n", findStudent(999900));
  44.     printf("Test 6: %i \n", findStudent(999999));
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment