Nayyab F.
asked 05/27/21i want to get unique solution of the question and i am mentioning question in description box
Take a sorted array of 15 elements of type int and a value x from user, you need to find the ceiling
and floor of 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. Assume than the array is sorted in
ascending order. Write functions to find floor and ceiling of x from the given array.
Examples :
For example, let the input array be {1, 2, 8, 10, 10, 12, 19}
For x = 0: floor doesn't exist in array, ceil = 1
For x = 1: floor = 1, ceil = 1
For x = 5: floor = 2, ceil = 8
For x = 20: floor = 19, ceil doesn't exist in array
2 Answers By Expert Tutors
Biruk A. answered 05/28/21
A Programing enthusiastic and a self taught web developer
arr = [1, 2, 8, 10, 10, 12, 19] # the array
# initialize a continues loop
while True:
x = int(input()) # accepting the input
# initialize the value as None
ceil, floor = None, None
# finding the floor
for i in arr:
if i <= x:
floor = i
else:
break
# finding the ceilling
for i in arr:
if i >= x:
ceil = i
break
# displaying the result
print(f"For x = {x};", end=', ')
if floor:
print(f'floor = {floor}', end=', ')
else:
print("floor doesn't exist in array", end=', ')
if ceil:
print(f'ceil = {ceil}')
else:
print("ceil doesn't exist in array")

Patrick B. answered 05/27/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <limits.h>
#define MAX (15)
Input(int * A, int * x)
{
for (int iLoop=0; iLoop<MAX; iLoop++)
{
cout << "input # :>";
cin >> A[iLoop];
}
cout << "input # x:>";
cin >> *x;
}
int Ceiling( int * A, int x)
{
int iReturn = INT_MAX;
int i=MAX-1;
while (i>=0)
{
if (A[i]<x)
{
break;
}
i--;
}
cout << " index stopped at " << i << endl;
if (i>=0)
{
iReturn = A[i+1];
}
return(iReturn);
}
int Floor(int * A, int x)
{
int iReturn = INT_MIN;
int i=0;
while (i<MAX)
{
if (A[i]>x)
{
break;
}
i++;
}
cout << " index stopped at " << i << endl;
if (i<MAX)
{
iReturn = A[i-1];
}
return(iReturn);
}
main()
{
int x;
int A[MAX];
Input(A,&x);
int iReturn;
if ((iReturn=Ceiling(A,x))<INT_MAX)
{
cout << "Ceiling is " << iReturn << endl;
}
else
{
cout << "Ceiling does not exist " << endl;
}
if ((iReturn=Floor(A,x))>INT_MIN)
{
cout << " Floor is " << iReturn << endl;
}
else
{
cout << "Floor does not exist " << endl;
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Nayyab F.
I want solution in c++ language05/27/21