Advertisement
Guest User

Untitled

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