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 to
search'
clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [find] Error 1
makefile
?