C Exercises: Find the ceiling in a sorted array

C Array: Exercise-40 with Solution

Write a program in C to find the ceiling in a sorted array.
N.B.: Given a sorted array in ascending order and a value x, the ceiling of x is the smallest element in array greater than or equal to x, and the floor is the greatest element smaller than or equal to x.

Pictorial Presentation:

C Exercises: Find the ceiling in a sorted array

Sample Solution:

C Code:

#include <stdio.h>
int findCeiling(int arr1[], int low, int high, int x)
     int i;
    if (x <= arr1[low])
    return low;
   for (i = low; i < high; i++)
       if (arr1[i] == x)
          return i;
       if (arr1[i] < x && arr1[i + 1] >= x)
           return i + 1;
   return -1;

int main()
    int arr1[] = {1, 3, 4, 7, 8, 9, 9, 10};
    int ctr = sizeof(arr1)/sizeof(arr1[0]);
    int x = 5,i;
//------------- print original array ------------------	
	printf("The given array is :  ");
	for(i = 0; i < ctr; i++)
	printf("%d  ", arr1[i]);
    int index = findCeiling(arr1, 0, ctr-1, x);
    if (index == -1)
        printf("No ceiling for the number %d  exists in array. ", x);
        printf("The ceiling of %d is: %d", x, arr1[index]);
    return 0;

Sample Output:

The given array is :  1  3  4  7  8  9  9  10  
The ceiling of 5 is: 7 


Flowchart: Find the ceiling in a sorted array.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to rotate an array by N positions.
Next: Write a program in C to find the Floor and Ceil of the number 0 to 10 from a sroted array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP