0

This is my code

#include <spl/helpers.h>
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>

int main(void);

bool search(int value, int values[], int n)
{
  int lower = 0;
  int upper = n - 1;

  while (lower <= upper)
  {

    int middle = (lower + upper) / 2;

    if (values[middle] < value)
    {
      lower = middle + 1;
    }  

    if (values[middle] > value)
    {
      upper = middle - 1;
    }

    if (values[middle] == value)
    {
      return true;
    }
 }

 return false;

}

it says that helpers is not a found library.

Also, is there anything I can do to fix/improve this code?

1
  • Yes, you can improve this code by using else if clause instead of plain if at second and third time place. This will reduce some useless comparisons.
    – sinister
    Jul 19, 2015 at 15:13

2 Answers 2

1

Ani:

The problem, most likely, lies in your very first line:

int main(void);

What exactly are you trying to do there? helpers.c is a source code file that is complementary to find.c. main is in find.c and your functions in helpers.c are called from find. So start by removing that line from your code.

0

The helpers.h doesn't have anything to do with the spl library used for . Instead of

#include <spl/helpers.h>

you should use:

#include "helpers.h"

EDIT

Let's start from the beginning. First of all take it as advice to not just depend on others to solve your problems. You just pasted the code, and said there was an error, but you didn't say in what file, what you did to solve it, what steps you did right before the error occurred. Anyway. First of all I assume that the file you pasted above, is located in helpers.c, if not, it should. Then you should at top include only the helpers.h. The other libraries you have included should be included in helpers.h. In your find.c you should also include only helpers.h. The only function in find.c should be the main() function. All other functions should reside inside the helpers.c. When you have finished you should compile your program by make find and execute it by find. If this still doesn't answer your question. Improve your question with the suggestions I made at the beginning of this edit.

11
  • Chris, it still says helpers is not found, even with the quote marks.
    – Ani
    Jul 19, 2015 at 16:46
  • Could you paste the actual error or a screenshot of the error?
    – ChrisG
    Jul 19, 2015 at 18:38
  • findsearch.c:1:10: fatal error: 'helpers.h' file not found #include "helpers.h" and Irene, i removed the int main(void) and still gives the same result.
    – Ani
    Jul 20, 2015 at 13:59
  • Which file are you compiling and running?
    – ChrisG
    Jul 20, 2015 at 15:37
  • i am running search.c
    – Ani
    Jul 20, 2015 at 16:28

You must log in to answer this question.

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