+ 1
Binary search using recursion
This program is not running on turbo c. When I enter a number to be searched which is not present in the array still it shows that the number is present in the array. https://code.sololearn.com/cj5oTh03Y1UE/?ref=app
12 Antworten
+ 8
You complain 2 isn't in the array as you use 0 as missing. Your if where n is set to -1 won't ever be entered as the only path that executes it is when the number was found. Delete that if, initialize n to -1, and test h against -1 in main.
+ 8
Turbo C is obsolete and I've never used it (only GNU) so I'm not sure I can help with it. However, you are returning an uninitialized n in bs, if l is greater than r, so that could be the issue.
+ 5
The x[m] != y is returned directly from the recursion so it can't get there. When y isn't in x, the recursion stops when l>r so never gets to that code as it is within l<=r if. The only code that executes the if is when x[m] = y, which won't enter it.
+ 4
it is working
+ 2
Thank you so much sir.
+ 1
but it is not working in turbo c
+ 1
John Wells sir, please see this also.
+ 1
Yes it is working perfectly.
But I don't understand why the if statement where n is set to -1 won't ever entered.
+ 1
Also: you should format your source and give variables meaningful names.
0
harshit which missing number you tried and it gave wrong output? For me also, it works for 3 to 4 missing numbers I tried
0
This code is not working in turbo c properly.