Binary Search in C, infinite looping?
Go to solution
Solved by reniat,
20 minutes ago, bgibbz said:You shouldn't declare M inside of a while loop. That may be the issue.
18 minutes ago, gtx1060=value said:int m should be declared outside the loop and at the beginning of the program - c isn't very enjoyable to learn
that's good practice, and they should do that, but that's not causing the infinite loop here.
@DeadEyePsycho consider the case where L = R. M should be equal to both of them, since if they are pointing at the same part, then this is the last element to check. Instead, you've got M = (L-R)/2 = 0/2 = 0. In other words, when this happens your M is going to be incorrect. What might you do to correct this? can you see the issue?
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now