0

In "Find" problem, something seems wrong with my binary search Algorithm, because in check50, helpers.c wouldn't compile, I tried to compile it, then it gave me this weird error:

 clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -
Werror -Wextra -Wno-sign-compare -Wshadow    helpers.c  -lcrypt -lcs50 -lm -
o helpers
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): 
relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): 
relocation 1 has invalid symbol index 12
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: 
In function `_start':
(.text+0x20): undefined reference to `main'
clang: error: linker command failed with exit code 1 (use -v to see 
 invocation)
 make: *** [helpers] Error

would someone give me a hint on what I did wrong??

bool search(int value, int values[], int n)
{
// implement a searching algorithm
int newArray[n];
int i,j=0;
int m=0;
int r=round((n)/2); //in case of an array with an odd number of elements
if(r%2==0)         //if r is even(considering that we start counting from index 0)
r=r-1;
int mid=r;      //the index of the number in the middle
if(n<0)
return false;
if(values[mid]==value)
    return true;
else if(value>values[mid]){     //Binary search
                      //start is the first index in the new array
    for(i=mid;i<n;i++){
        newArray[j]=values[i];
        j++;
    }
    if(newArray[0]==value)  //if I end up with an array of 1 element"needle"
       return true;
    else{

        n=n-r;
        m=search( value,  newArray, n);
    }}
else if(value<values[mid]){
            j=0;

            for(i=0;i<mid;i++){
                newArray[j]=values[i];
                j++;
    }
            if(newArray[0]==value)  //if I end up with an array of 1 element"needle"
               return true;
             else {
                n=n-r;
                m=search( value,  newArray, n); //recursion
    }


}
 return false;            


}
0

Did you try to compile with make helpers? It doesn't work this way. Compiling helpers fails because there is no main() function in helpers.c. Instead, helpers.c is part of a multiple source code file build of find. So, in order to produce the executable program find, you need to make find which will incorporate all of the needed files, including helpers.c and helpers.h. Perhaps a review of the course material on building executables from multiple source code files is in order, including the explanation of how make works?

After that, I'd need access to the full file to attempt to compile. I'll leave that to you.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • I tried: make find, but it always gives me "didn't find needle in the haystack!", even when the number exists
    – Raw
    Jul 13 '17 at 16:58
  • that would be a different issue. Once it compiles, you need to then debug the program to make it work right.
    – Cliff B
    Jul 13 '17 at 18:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .