The floor function returns the index of the greatest value in an array whose res
ID: 3894057 • Letter: T
Question
The floor function returns the index of the greatest value in an array whose resulting value is still less than or equal to the given value. The following segment of code finds the floor value’s index (in a sorted array) for a value. The segment itself uses a binary search, returns -1 when a value does not have a floor value, searches inclusively on both the min and max, and is recursive. Finish the code.
1: int floor (int min, int max, int * arr, int target) 3: int mid- (min + max) 2; 6 return -1; 1 9 10: 11: if (target 12: 13: 14: 15: 16: return floor( 17: return min return floor (min, , arr, target); , arr, target);Explanation / Answer
Answer is as follows :
1)
target < arr[0] // return -1 if target is not present in list
2)
target = arr[min] // if target present at min of array means at starting location of array
3)
target < mid // check if target is less than mid
4) if yes
return floor(min, mid-1 , arr, target) // if target is less than mid
5)
return floor( mid + 1 , max, arr, target) // if target is greater than mid
if there is any query please ask in comments...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.