0

I started CS50 online some time ago and due to a low amount of time I stopped midway through pset3. Finally I started working again but now my finished 'find' file gives an error I do not comprehend. It may be that I am missing something very basic so please forgive me. I did not try to make helpers.c on its own, but instead use the command make all as stated in the makefile. I also tested my search and sort alghorithms to know they are working, except not when used in find.c. Please help me :(

I don't know if this is strictly forbidden, but I put all of my code down below. If you do now have your own sorting and search alghorithems please don't look here.

My find.c

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include "helpers.h"


const int MAX = 65536;

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./find needle\n");
        return -1;
    }

    int needle = atoi(argv[1]);
    int size;
    int haystack[MAX];

    for (size = 0; size < MAX; size++)
    {
        printf("\nhaystack[%i] = ", size);
        int straw = GetInt();
        if (straw == INT_MAX)
        {
            break;
        }
        haystack[size] = straw;
    }
    printf("\n");

    sort(haystack, size);

    if (search(needle, haystack, size))
    {
        printf("\nFound needle in haystack!\n\n");
        return 0;
    }
    else
    {
        printf("\nDidn't find needle in haystack.\n\n");
        return 1;
    }
}

My helpers.c

#include <cs50.h>
#include "helpers.h"

bool search(int value, int values[], int n)
{
    int start;
    int end;
    int middle;

    start = 0;
    end = n - 1;

    while (start <= end)
    {
        middle = (start + end) / 2;
        if (value == values[middle])
        {
            return true;
        }
        else if (value > values[middle])
        {
            start = middle + 1;
        }
        else if (value < values[middle])
        {
            end = middle - 1;
        }
    }
    return false;
}

void sort(int values[], int n)
{
    for (int i = 1; i < n; i++)
    {
        int element = values[i];
        int j = i;
        while ( j > 0 && values[j - 1] > element)
        {
            values[j] = values[j - 1];
            j -= 1;
        }
        values[j] = element;
    }
    return;
}

My helpers.h

#include <cs50.h>

bool search(int value, int values[], int n);

void sort(int values[], int n);

The error code

clang -ggdb3 -O0 -std=c11 -Wall -Werror -o find find.c helpers.c -lcs50 -lm /tmp/find-fc6ed2.o: In function `main':

/home/ubuntu/workspace/pset3/find/find.c:54: undefined reference to sort' /home/ubuntu/workspace/pset3/find/find.c:57: undefined reference tosearch'

clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [find] Error 1

1
  • Do you have all of those files in the same directory, along with the file makefile?
    – Cliff B
    Commented May 25, 2016 at 23:36

1 Answer 1

0

Thank you everybody who took the time to view my code but I would like to apologize because as it turned out the problem was not with the code but with the virtual workspace instead. After going crazy last night trying to find the error in my code, it never occured to me to check the program itself (also because I never had problems with cloud 9 before). After going to sleep and checking my code again this morning I noticed that my whole helpers.c file had been wiped. That's why find.c couldn't find the linked functions. Very strange though since it was there yesterday, otherwise I wouldn't have been able to copy it to stackexchange. So I transferred my code from here back to cloud9 and now everythings works fine. Never had this problem before so I'm curious if other people experienced this.

1

You must log in to answer this question.

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