Skip to main content
1 of 2

Binary Search Problems

When attempting to implement binary search, I have run into problems when compiling. If you see any problems in sort, please point them out as well. Any help would be appreciated. Here are the errors:

clang -ggdb3 -O0 -std=c99 -Wall -Werror -o find find.c helpers.c -lcs50 -lm

helpers.c:20:6: error: conflicting types for 'bsearch'
bool bsearch(int key, int array[], int min, int max)
     ^
/usr/include/stdlib.h:755:14: note: previous declaration is here
extern void *bsearch (const void *__key, const void *__base,
         ^
helpers.c:34:52: error: too few arguments to function call, expected 5, have         4
        return bsearch(key, array, mid + 1, max);    
               ~~~~~~~                         ^
/usr/include/stdlib.h:755:1: note: 'bsearch' declared here

extern void *bsearch (const void *__key, const void *__base,

^
helpers.c:38:52: error: too few arguments to function call, expected 5, have    4
            return bsearch(key, array, min, mid - 1);
                   ~~~~~~~                         ^
/usr/include/stdlib.h:755:1: note: 'bsearch' declared here
extern void *bsearch (const void *__key, const void *__base,
^
 helpers.c:52:39: error: too few arguments to function call, expected 5,  have 4
   bsearch(value, values, values[0], n);     
   ~~~~~~~                            ^
/usr/include/stdlib.h:755:1: note: 'bsearch' declared here
extern void *bsearch (const void *__key, const void *__base,
^
4 errors generated.
make: *** [find] Error 1

Here is my code:

/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 */
   
#include <cs50.h>
#include <math.h>

#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
 //Recursive Search

bool bsearch(int key, int array[], int min, int max)
{
    int mid1 = 0;
    int mid = 0;
    if(max < min)
    {
        return false;
    }
    else
    {
        mid1 = max / 2;
        mid = round(mid1);
        if(array[mid] < key)
        {
            return bsearch(key, array, mid + 1, max);    
        }
        else if(array[mid] < key)
        {
             return bsearch(key, array, min, mid - 1);
        }
        else
        {
            return true;
        }
    }
} 
 /**
 * Returns true if value is in array of n values, else false.
 */

bool search(int value, int values[], int n)
{   
   bsearch(value, values, values[0], n);     
}
/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{   
    int j = 0;
    int k = n;
    for(int i = 0; i < n; i++)
    {
    if(values[i] == 0)
    {

    }
    else if(values[i] < j)   
    {
    j = values[i];
    values[k] = j;
    k = k - 1;
    }