Advertisement
Rehan_Rahman26

array problem 2

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