Question Details:
I'm not sure if this is right or not. Can someone please check this for me?
Write a function to implement binary search on an array of doubles. The function should take at least the array and the target item as parameters (some others may be required).
Did you test it? How did it work out for you? I'm sure someone might be willing to critique the design and give you some improvement ideas but whether it works or not could easily be determined by some simple testing. I recommend that you write a main function with a unit test case that executes the function with a few different arrays and data sets. Try to test with data sets where each value is far apart as well as with data sets with values that are very close together. Try both odd and even sized arrays as well. The concept looks ok to me but here are some recommendations.
I'm not sure that the return value and first, last, params should be of type double. Shouldn't they just be int? I see implicit type conversions within the function. (first = mid + 1) which is why I am not sure that the other params should be of type double. I don't think it is a problem but it looks kind of confusing and unnecessary.
My only concern about it is what happens when something isn't found? What is (-(first+1))? Another recommendation is to implement the function with pointers to the first and last element to search within. The second pointer would actually be an "end" pointer or 1 past the end of the range. You could implement with 3 args and a return value where the return value will be equal to "end" pointer if nothing was found.