0
Can anyone help me to solve this?!!I want a binary search code to show where the searched number is in array but its not working
3 Respuestas
0
Please forget about conio.
Anyway in the prototype of bubble, the first parameter is an int but you want a pointer to int.
And if the input n is greater than 10, you try to access the array ar[] out of bond.
edid: also bsearch wasn't working. Fixed it.
https://code.sololearn.com/c63tdO4jQ1d4/?ref=app
0
It's simple IF you're taking the input of the number you're looking for BEFORE filling up the array. After the scanf, make an if the inputted number is the number you look for.
On the other hand, if you're taking the input after the array filled up, you have to sort before you do binary search. In your code, your bubble is wrong in both declaration and definition, it should be int[] or int*.
The reason your binary search doesn't work is because you set the parameter/argument wrong, in the function definition, you set it int x, int n, while in main, you send n, x.
Here's how if you don't actually need the array for something else aside just to find the number you're looking,
https://code.sololearn.com/c47FxxHZb657/?ref=app , if the array is necessary, just add it by changing val. But if you really want to do binary search, here's how (read carefully),
https://code.sololearn.com/co9dN2r646Ku/?ref=app , if you a question regarding my codes, just reply, I'll answer you back.
0
Straightforward answer, your binary search is actually kinda correct. The things you got wrong are (related to your binary search):
1. In main(), you call & pass bsearch(ar, n, x) but in the function definition, it's bsearch(ar, x, n).
2. Variable found is useless there.
3. Found or not, you print out ar[i] instead of variable index.
For the rest:
1. Don't use conio and its children (getch()).
2. In your bubble sort declaration, its 1st parameter is int, it should be int[] or int*.
3. Tidy up your code, indentations, everything, it's messy.