Advertisement
andruhovski

prog0110-array

Sep 11th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <search.h>
  2. void lsearch_demo();
  3. void lfind_demo();
  4. void bsearch_demo();
  5. void qsort_demo();
  6.  
  7. int values[] = { 10, 40, 100, 20, 90, 25, 0 };
  8.  
  9. int compare(const void * a, const void * b)
  10. {
  11.     return (*(int*)a - *(int*)b);
  12. }
  13.  
  14. int _tmain(void)
  15. {
  16.     lfind_demo();
  17.     lsearch_demo();
  18.     bsearch_demo();
  19.     return 0;
  20. }
  21.  
  22. void lsearch_demo()
  23. {
  24.     int * pItem;
  25.     int key = 140;
  26.     unsigned int num = 6;
  27.  
  28.     pItem = (int*)_lsearch(&key, values, &num, sizeof(int), compare);
  29.     if (pItem != NULL)
  30.         printf("%d is in the array\n", *pItem);
  31.     else
  32.         printf("%d is not in the array\n", key);
  33. }
  34.  
  35. void lfind_demo()
  36. {
  37.     int * pItem;
  38.     int key = 140;
  39.     unsigned int num = 6;
  40.  
  41.     pItem = (int*)_lfind(&key, values, &num, sizeof(int), compare);
  42.     if (pItem != NULL)
  43.         printf("%d is in the array\n", *pItem);
  44.     else
  45.         printf("%d is not in the array\n", key);
  46. }
  47.  
  48. void bsearch_demo()
  49. {
  50.     int * pItem;
  51.     int key = 40;
  52.     unsigned int num = 6;
  53.  
  54.     pItem = (int*)bsearch(&key, values, num, sizeof(int), compare);
  55.     if (pItem != NULL)
  56.         printf("%d is in the array\n", *pItem);
  57.     else
  58.         printf("%d is not in the array\n", key);
  59. }
  60.  
  61. void qsort_demo()
  62. {
  63.     int num = 6;
  64.     qsort(values, num, sizeof(int), compare);  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement