I've updated search function in helpers.c with code below. I had to remove reference to helpers.h since that was causing even more errors.
#include <cs50.h>
// #include "helpers.h"
bool search (int value, int values[], int n)
{
for (int i = 0; i < n; i++)
{
if(values[i] == value)
{
return true;
}
else
{
return false;
}
}
}
void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm
return;
}
When compiling helpers.c I receive following error. I played with curly brackets by removing them but couldn't figure out what's the problem is. Please help.
jharvard@ubuntu (~/pset3/find): make helpers
clang -ggdb3 -O0 -std=c99 -Wall -Werror helpers.c -lcs50 -lm -o helpers
helpers.c:18:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^
1 error generated.
make: *** [helpers] Error 1
UPDATE 1:
#include <cs50.h>
// #include "helpers.h"
bool search (int value, int values[], int n)
{
for (int i = 0; i < n; i++)
{
if(values[i] == value)
{
return true;
}
}
return false;
}
void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm
return;
}
Now I receive following error:
jharvard@ubuntu (~/pset3/find): make helpers
clang -ggdb3 -O0 -std=c99 -Wall -Werror helpers.c -lcs50 -lm -o helpers
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 21 has invalid symbol index 22
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [helpers] Error 1
What's my mistake?
UPDATE 2:
Here are my code for:
helpers.h
#include <cs50.h>
bool search(int value, int values[], int n)
{
for (int i = 0; i < n; i++)
{
if(values[i] == value)
{
return true;
}
}
return false;
}
void sort(int values[], int n);
helpers.c
#include <cs50.h>
#include "helpers.h"
bool search (int value, int values[], int n)
{
for (int i = 0; i < n; i++)
{
if(values[i] == value)
{
return true;
}
}
return false;
}
void sort(int values[], int n)
{
return;
}
and finally 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[%d] = ", 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;
}
}
and I'm receiving following error, when compiling find.c
jharvard@ubuntu (~/pset3/find): make find
clang -ggdb3 -O0 -std=c99 -Wall -Werror -o find find.c helpers.c -lcs50 -lm
helpers.c:4:6: error: redefinition of 'search'
bool search (int value, int values[], int n)
^
./helpers.h:15:6: note: previous definition is here
bool search(int value, int values[], int n)
^
1 error generated.
make: *** [find] Error 1
make find
command, sincehelpers.c
is only a part of your code infind.c
.find.c
. I already tried and didn't succeed.find.c
references tohelpers.c
and sincehelpers.c
has faults, thenfind.c
won't work too.make find
command?helpers.c
andhelpers.h
?