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

bool search(int needle, int haystack[], int size)
    int min = 0;
    int max = size;    

    while(max >= min)
        int mid = (max+min)/2;

        if(haystack[mid] < needle)
            min = mid + 1;
        else if(haystack[mid] > needle)
            max = mid - 1;
            return true;
    return false;

// Sorts array of n values.

void sort(int values[], int n)
        int i,j,temp;
        for (i = 0; i < n; i++)

            for (j = i + 1; j < n; j++) {
               if (values[i] > values[j]) 
                temp = values[i];
                values[i] = values[j];
                values[j] = temp;
  • If Your question is about Makefile, please paste it here. Nov 5 '15 at 20:11
  • I would like you to comment you to comment on the code please. I know that search and sort work, at least individually. I understand that it is only to to type make in the terminal and then the code will combine and run generate, find, helpers.c and helpers.h. I could not paste the code because these comment boxes will not accept the amount. I am really frustrated with this issue and as I understand it I need only to right two routines and put them together and type 'make' and all would be well. It is not and I have spent way too much time on this. Nov 6 '15 at 2:06
  • Yes, but what is your problem exactly?
    – Irene
    Nov 6 '15 at 2:42

I am sorry this pset has been such a struggle for you. I think you are really close to the finish line. Take a deep breath and let's go through this step by step. The most important thing is that you do not lose the code you have worked so hard on.

The plan:

  1. Backup your work. There is no such thing as too much backup.
  2. Restore Makefile, find.c and helpers.h from the distro code
  3. make the "find" executable
  4. Whistle a happy tune

The execution:

  1. In a new terminal window:

    cd  pset3
    cp -r find SAVEfind  

    This will create a copy of your find directory in a new directory called SAVEfind.

  2. Follow the instructions in the Problem Set 3 document to download the distro code, but DO NOT UNZIP. We are going to restore only the 3 files mentioned above. unzip allows you to select the files you wish to retrieve. From your workspace directory:

    unzip pset3.zip pset3/find/Makefile
    unzip pset3.zip pset3/find/find.c
    unzip pset3.zip pset3/find/helpers.h

Now you have fresh copies of those three files.

  1. Navigate to pset3/find directory and execute make clean. This will ensure that make starts with a clean slate. Now execute make.

I hope I hear whistling! :)

For the record, here is my thinking from reading through your posts. You originally wrote your routines in helpers.c; you added a "main" loop and variables, so it would be a stand-alone program that you could test. That made it difficult to integrate it back into the find application and it was downhill from there. By now you are very frustrated and it makes it difficult to find your way back to where you were when your routines were working.

There's a great discussion of libraries in Week 1 Shorts.


  • Thanks for the help. I got the search and sort working and pasted them into helpers.c. Here I assume that is the way to do it; however, it keeps crashing I think, because the variables are not matching. I thought you could use other names for the variables. Also, I am having real trouble with passing arrays. Last run had 19 errors. There has got to be a better process. What do you think? Nov 8 '15 at 0:27
  • I was just logging off when the noticed flashed. Good timing. I did not recommend pasting, because that sometimes causes more problems that it fixes, and you may be suffering the consequences of that. You mention at least three different things in your comment. Let's go one at a time. Did your program compile? By that I mean did make run with no errors. Let's start there. Nov 8 '15 at 0:31
  • I am sorry it generated these. helpers.c:13:9: error: implicit declaration of function 'search' is invalid in C99 [-Werror,-Wimplicit-function-declaration] if (search(n, numbers, SIZE)) ^ helpers.c:13:9: note: did you mean 'bsearch'? /usr/include/stdlib.h:755:14: note: 'bsearch' declared here extern void *bsearch (const void *__key, const void *__base, ^ helpers.c:19:6: error: conflicting types for 'search' bool search(int needle, int haystack[], int size) ^ helpers.c:13:9: note: previous implicit declaration is here if (search(n, numbers, SIZE)) Nov 8 '15 at 4:57
  • Is there a way I can format the code when I answer. Also you should know that I reworked helpers a bit which may have added to the problem. Nov 8 '15 at 5:02
  • If you use helpers.h, find.c and Makefile from the original distro code AND in helpers.c declare your search function like this: "bool search(int value, int values[], int n)" you will not have this particular problem. Reread the answer for a strategy to restore helpers.h, find.c and Makefile to their original versions. Don't forget to backup your work! Nov 8 '15 at 12:38

You must log in to answer this question.

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