Launguage: C Launguage: C 3. Recl from class that the floor function returns the
ID: 3897791 • Letter: L
Question
Launguage: C
Launguage: C
3. Recl from class that 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) 2: 3: int mid- (min max) / 2; 4: 5: f (. return-1; 7: return min; 9: 10: 11: if (target 12: 13 return floor(min, 14: 15: 16: return floor(. 17:) , arr, target) , arr, target);Explanation / Answer
int floor(int min, int max, int *arr, int target) {
int mid = (min + max) / 2;
if(arr[min] > target)
return -1;
if(min == max - 1) {
if(arr[max] <= target)
return max;
return min;
}
if(target < arr[mid])
return floor(min, mid, arr, target);
return floor(mid, max, arr, target);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.