Binary Search- Squrare root problem

when i am giving precision for 10 digits it is not giving output
eg i have given number = 500 and precision = 6
then output is = 22.3607
why it is not giving upto 6 decimal places?

Hey, can you share your code’s link here, so that we can help you to resolve the issue.

https://online.codingblocks.com/player/13601/content/78?s=2583

when i am giving precision for 10 digits it is not giving output
eg i have given number = 500 and precision = 6
then output is = 22.3607
why it is not giving upto 6 decimal places?

https://online.codingblocks.com/player/13601/content/78?s=2583

when i am giving start=0 and end = sizeof(arr)/sizeof(arr[0]) as a parameter then the program is not working properly means it is not giving output

Hi Vijay, please save your code on https://ide.codingblocks.com and share the link here please.

Hi Vijay, let’s dry run your code on a testcase

Consider if the element you are searching for is not in the list and is greater than all of the elements in list for example:
arr = { 1, 2, 3, 4}
element to be searched is 5;
now

//First iteration
lo = 0
hi = 4 . // size of the array as you did
mid = 0 + 4 / 2 = 2
arr[mid] = 3 which is < 5 so
lo = mid + 1 = 3

//Second Iteration
lo = 3
hi = 4
mid = 3
arr[mid] = 4 which is < 5 so
lo = mid + 1 = 4

//Third Iteration
lo = 4
hi = 4
mid = 4
NOW, we know arr is of size 4 i.e. it does not contain arr[4] as the index get’s out of range. But this iteration is demanding arr[4].

Hence, the hi should always be set to the (size of array) - 1.

So if

int n = sizeof(arr)/sizeof(arr[0]) ;

then the boundaries for the binary search at start should be:

start = 0
end = n - 1
this is because the indices in your array will range from 0 -> n-1 (both inclusive).

I hope this clears your doubt. I am marking your doubt as Resolved for now. Re-open it if required