Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int poredi (const void *a, const void *b) {
  5. return *((int*)a) - *((int*)b);
  6. }
  7.  
  8. int main () {
  9.  
  10. int *a = NULL, b = NULL, n,i=0,x,k;
  11.  
  12. scanf ("%i", &n);
  13.  
  14. while (i<n) {
  15. a = realloc (a, (i+1) * sizeof(int));
  16.  
  17. if (a == NULL)
  18. return -1;
  19.  
  20. scanf ("%i", &k);
  21.  
  22. a[i] = k;
  23.  
  24. i++;
  25. }
  26.  
  27. b = a;
  28.  
  29. scanf ("%i", &x);
  30.  
  31. int *rez = bsearch (&x, b, n, sizeof (int), poredi);
  32.  
  33. if (rez == NULL)
  34. printf ("-1\n");
  35. else
  36. printf ("%i\n", rez-b);
  37.  
  38. free (b);
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement