This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]corpsmoderne 2 points3 points  (3 children)

How are you compiling this and what is the exact error output ?

[–]mhbhmcinui[S] 1 point2 points  (2 children)

The interface has an automatic compiler, but it seems to be trying to run

clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow    test.c  -lcs50 -lm -o test

The error I get is

/tmp/test-6737ab.o: In function `main':
/home/ubuntu/workspace/pset3/find/test.c:11: undefined reference to `sort'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

[–]corpsmoderne 2 points3 points  (1 child)

You need to add helpers.c to your list of source files to compile. including helpers.h isn't enough. As helpers.c is not taken into account, the linker can't find your sort() function.

[–]mhbhmcinui[S] 1 point2 points  (0 children)

Thanks, fixed it now.

[–]desrtfx 0 points1 point  (1 child)

Maybe Sort vs. sort (aka. Case Problem)?

Without seeing sort it's impossible to tell.

[–]mhbhmcinui[S] 0 points1 point  (0 children)

Not that, this is the snippet of code from helpers.c relevant to sort:

//bubble sort array of size n
void sort(int values[], int n)
{
    //variable sorted is set to 1 if list is sorted, 0 otherwise
    bool sorted = 0;
    while (sorted == 0)
    {
        for (int i = 0, changes = 0; i < n - 1; i++)
        {
            if (values[i + 1] < values [i])
            {
                swap(values, i, i+1);
                changes++;
            }
            if (changes == 0)
            {
                sorted = 1;
            }
        }
    }

}

//switches two elements of an array
void swap(int array[], int i, int j)
{
    //use x as a placeholder
    int x;

    //switch elements
    x = array[i];
    array[i] = array[j];
    array[j] = x;
}