0

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 127IT still doesn't find the 'needle'

3
  • I tested the code and it works fine. Have you changed something in find.c rather than helpers.c? I also assume you're working on find and not fifteen.
    – Cliff B
    Commented Oct 19, 2017 at 1:38
  • Either you forgot to compile (so code does not match programme), or problem in 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).
    – Blauelf
    Commented Oct 19, 2017 at 6:24
  • After reading the return true; part convinces me you probably did not save the file, or forgot to make find again.
    – Blauelf
    Commented Oct 19, 2017 at 10:05

1 Answer 1

0

Two issues here. First, if there is a problem, it isn't in the linear search() function. Or, the updated code isn't the code that's being compiled.

Second, there's no main function in the helpers.c file because there doesn't need to be. The main() function exists in the find.c source code file. Remember, this pset is also a lesson in compiling a single executable that is built from multiple source code files. You should be running make find, not make helpers.

If there are other issues with the code, perhaps its time for a new question. IF the questions asked have been answered, please click on the check mark to accept. Otherwise, this question will sit in the unanswered question pool forever. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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