Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int c, first, last, middle, n, search, array[100];
  5. printf("Enter number of elements\n");
  6. scanf("%d",&n);
  7. printf("Enter %d integers\n", n);
  8. for (c = 0; c < n; c++)
  9. {
  10. scanf("%d",&array[c]);
  11. }
  12. printf("Enter value to find\n");
  13. scanf("%d", &search);
  14. first = 0;
  15. last = n - 1;
  16. middle = (first+last)/2;
  17. while (first <= last)
  18. {
  19. if (array[middle] < search)
  20. {
  21. first = middle + 1;
  22. }
  23. else if (array[middle] == search)
  24. {
  25. printf("%d found at location %d.\n", search, middle+1);
  26. break;
  27. }
  28. else
  29. {
  30. last = middle - 1;
  31. middle = (first + last)/2;
  32. }
  33. if (first > last)
  34. {
  35. printf("Not found! %d isn't present in the list.\n", search);
  36. return 0;
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement