I am taking the Harvard ap cs5o class, and I just started the pset3's fifteen. I followed the directions to get the skeleton code from here: Problem scope
After reading and following along I tried the linear search function for 127 with seed 50. I used the following command ./generate 1000 50 | ./find 127
copied from the website. It does not find the number with my linear search algorithm included below.
bool search(int value, int values[], int n)
{
for (int i = 0; i < n; i += 1){
if (values[i] == value) {
return true;
}
}
return false;
}
I can't find an error in my code. I also have tried just having it just read return true;
and it still doesn't find it.
When it prints the generate and find function combo the numbers don't appear either, I don't know if this is related though. It just prints
...
haystack[995] =
haystack[996] =
haystack[997] =
haystack[998] =
haystack[999] =
haystack[1000] =
Didn't find needle in haystack.
EDIT:
After implementing some of your comments, I can't make the program. It does not have a main function because it doesn't need one (I did not take one out It came like that). It also doesn't compile because there is another function that imports variable what we have to work in, but I just commented it out because they were unused variables and making it not compile. Can I write an int main function that does nothing just for compiling purposes?
EDIT
I made the following int main function:
int main(void) {
return 0;
}
Now it compiles but when I run ./generate 1000 50 | ./find 127
IT still doesn't find the 'needle'
sort
. If rebuilding doesn't help, print the array before and after sorting, and search for missing, duplicated or extra numbers (use small arrays for that).return true;
part convinces me you probably did not save the file, or forgot tomake find
again.