20
votes
Accepted
Pset3 Binary Search problems
This is an example of a recursive call. Even though it is calling itself correctly, it is missing the code necessary to recursively return the result. As it is written above, it will execute the ...
6
votes
Accepted
What's wrong with this binary search implemented using recursion?
To spot what's wrong with your algorithm, given the following sorted array
{1, 2, 3}
try with a pen and a paper finding these values
value = 1
value = 2
value = 3
value = 4
And try to figure out ...
5
votes
Implementation of Search Function
There are two basic ways for implementing a binary search algorithm.
using recursion.
using loops.
Given values, a global int array that we're searching for value in. min, the minimum index, and max,...
4
votes
Pset3 Binary Search problems
There was a question about this question on facebook, so instead of replying there, I thought I'd try to give some examples here, to supplement Cliff's amazing answer. It might be that none of this ...
4
votes
Accepted
pset3 - recursive binary search not compiling
Common problem. The compiler looks at all the possible paths through the code and checks to see that there is going to be a guaranteed, unconditional return to satisfy the need for a return value. But,...
3
votes
Accepted
Pset3: Helpers.c Binary Search Fails on checks that have 42 entered last
I inserted your search routine into my code and found an additional clue. When the needle is either the smallest or the largest number in the sorted list, it would fail. This should be recognized as a ...
3
votes
Accepted
Pset3 Segmentation Fault
One of the skills that are indirectly taught in this class, as well as being one of the most important skills you must learn as a programmer is how to debug a program. You will run into seg faults ...
3
votes
Accepted
Pset3 Search/Find problems
I don't want to sound cruel here, but your sort routine has serious problems. It is incomplete, it has code that appears to serve no purpose ( an if statement with no code to process if true), and ...
3
votes
pset 3 binary search
You have declared an array called values.
When you say 'values[middle]', it refers to the middle element of array. On the other hand, 'values' refers to the address of the beginning of that array. ...
3
votes
Accepted
Need help with recursive binary search
When you call a recursive function, you actually need to deal with the return value of that function. You are currently just calling BinarySearch(key, array, min, midpoint-1) for example and then ...
3
votes
Accepted
How do I set a pointer to access the left half of an array? [PSET 3 - Binary search]
For a binary search, you keep only one range, not two. That one could be specified by either a pointer to the start of the range and a length, or a left and right index limiting the range (the version ...
3
votes
Accepted
Question about binary search. Please help!
Even or odd, it doesn't really matter. The point is that you are dividing the list approximately in half, give or take. If the split doesn't occur exactly at the middle (odd number of elements), it ...
2
votes
Is it okay to check my code against a reference book?
The main moto of the course is to increase your skills. If you think you have made a wrong implementation of any standard algorithm or feel a bit confusing in any topic, so you are free to search for ...
2
votes
Accepted
Pset3, After haystack values typed, just blank on the Command line
The code could exhibit a type of behaviour called an infinite loop. This means it never reaches an expected state where the loop would end, and so continues to run forever. Tip: To halt a program ...
2
votes
Accepted
Slice a list for recursive function in pset3 search
Make a function declared as bool binsearch(int array[], int start, int end, int ele), with start being the starting index and end being the last index(exclusive), and ele is the element to be search ...
2
votes
Accepted
Binary Search Problems
You have a naming conflict with the previously defined function bsearch() in stdlib. Try changing the name of your bsearch() function to something like binsearch() and recompile.
If this answers ...
2
votes
Accepted
Recursive find.c - not returning
regardless of the correctness of the algorithm, generally speaking, if you want a function to return the value of an expression, you may return the expression itself. for example:
return 16 + 26; // ...
2
votes
Accepted
pset3 Binary search error message : Control may reach end of non-void function
To the compiler, it is possible that all of the if conditions could be false and none of the returns would ever execute. So, it's not happy. (Also, I believe this is a warning, but all warnings are ...
2
votes
Accepted
Do While loop not executing for pset3
Right before bool sorted = false;, you have return true;. You give a return value before anything else happens, so your function does exactly that: it returns that value and stops executing any ...
2
votes
Accepted
how effective would it have been to use an array for the dictionary?
suppose we are to implement speller using an array instead of a hash-table or a trie.
a main problem that we have is that we don't know the number of words in the dictionary in advance (yes, we know ...
2
votes
Accepted
find.c compiles but fails check50 - I believe binary search is to blame
The reason that the other questions aren't helping is that you've come up with a problem that appears to be unique. Take a look at the following code, particularly the test condition:
if (values[...
2
votes
Accepted
return -1 does not end function execution
It does return, but you have to understand where it returns to. When a return executes, it returns control to the function that called it. In this case, since BinarySearch() recursively calls itself, ...
2
votes
Accepted
Binary Search pset3
Q1: Remember this: COMPILERS ARE DUMB! While it may be logically impossible to not execute a return, compilers aren't capable of interpreting all of the complex logic possibilities to know. So, they ...
2
votes
C: Pset 3: Binary Search - 'expected an exit code of 0, not 1' (not finding n)
Actually what you had written is not a binary search as in each move its not changing the size of array to half of original size.
By the way mistake in your code is in line no. 19 and 30 as you are ...
1
vote
Accepted
check50 fails to compile helpers.c - throws implicit function declaration error
Header files act as a snippet informing programs that use it, "Okay. I've prepared these functions for you. You don't have to know how it works under the hood. It's not your business if I'm secretly ...
1
vote
Odd bug for Find
Your problem lies in these lines:
for (int i = 0; i < n; i++)
{
if (values[i] > values[i+1])
If there are n elements in array values[], what happens when i=n-1 and you try to ...
1
vote
Accepted
Having trouble with pset3 helpers.c binary search
It's going into an infinite loop when the needle is the last number in the stack. If you were to print out the values for min, mid and max, you'd be surprised. I did so, along with a pass counter and ...
1
vote
Accepted
Bug in my Pset3 Binary Search helpers.c
Take a look at the following three lines of code and tell me what's different?
if (value == values[middle])
else if (value > middle)
else if (value < middle)
Why is the first one ...
1
vote
Accepted
What's wrong with my binary search code?
You're setting "low" and "high" to values[middle - 1]. That is the value stored in that array location, and NOT the index of the array. So, suppose you have this case:
values = {42, 43, 44}
looking ...
1
vote
Accepted
Find.c (Binary Search)
I tested your search code and it's working fine. Binary search is dependent on the list being a sorted list. If the list isn't sorted, the search may or may not work.
You should verify that your ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
binary-search × 95pset3 × 81
find × 24
helpers × 10
cs50x × 9
search × 8
c × 6
recursion × 6
check50 × 5
bubble-sort × 5
sort × 4
pset3-helper.c × 4
binary × 3
counting-sort × 3
segmentation-fault × 2
array × 2
bug × 2
return × 2
week3 × 2
pset5 × 1
error × 1
trie × 1
pointers × 1
hash-table × 1
compilation-error × 1