Q:

C Program to Find Ceiling & Floor of X given a Sorted Array & a value X

0

C Program to Find Ceiling & Floor of X given a Sorted Array & a value X

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
 

int ceilSearch(int arr[], int low, int high, int x)
{
     int i;
 
    
    if (x <= arr[low])
    return low;
 
  
   for (i = low; i < high; i++)
   {
       if (arr[i] == x)
          return i;
 
       
       if (arr[i] < x && arr[i + 1] >= x)
           return i + 1;
   }
 
  
   return -1;
}
 
int main()
{
    int arr[] = {1, 2, 8, 10, 10, 12, 19};
    int n = sizeof(arr)/sizeof(arr[0]);
    int x = 3;
    int index = ceilSearch(arr, 0, n-1, x);
    if (index == -1)
        printf("Ceiling of %d doesn't exist in array ", x);
    else
        printf("ceiling of %d is %d", x, arr[index]);
    getchar();
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now