1
#include <stdio.h>

//int Binsearch(int,int,int,int);

int main() {
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int res;
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 7;

    res = Binsearch(arr,n-1,0,x);
   
    (res == -1) ? printf("element not found") : printf("element found at index :%d", res);

    return 0;
}

int Binsearch(int arr[], int h, int l, int p) {
    int mid;
    if (h >= l) {
         mid = ((h - l) + l) / 2;
    
         if (arr[mid] == p)
           return mid;
    
         if (p > arr[mid])
           return Binsearch(arr, h, mid + 1, p);
       
         return Binsearch(arr, mid - 1, l, p);
    } 
    return -1;    
}

// getting error plzz this code

warning: implicit declaration of function 'Binsearch' [-Wimplicit-function-declaration]    res= Binsearch(arr,n-1,0,x);
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    Please read the descriptions of the tags that you applied and then fix them. Also, provide a [mcve] and search online for the error message, just to form an understanding of what the error means. Lastly, take the [tour] and also read [ask], because you're not actually asking a question. – Ulrich Eckhardt May 23 '21 at 06:04
  • 1
    Don't tag as both C and C++. Tag as the compiler you're using. – tadman May 23 '21 at 06:14
  • This is warning not an error and your code works well. – Majid Hajibaba May 24 '21 at 06:24

1 Answers1

3

In both C and C++ you have to declare or define functions before you call them. Since the call to Binsearch in main comes before its definition, you need a declaration of Binsearch in scope first.

It looks like you tried to provide one with the line

//int Binsearch(int,int,int,int);

but you commented it out, probably because it caused other errors. It was almost right, but the first parameter is not an int but an array of int. So it should be changed to

int Binsearch(int[],int,int,int);

When you omit a declaration, some compilers will provide an "implicit" one, basically a default assumption of the parameters and return type of the function. This default is almost always wrong, so a warning about an implicit declaration usually means your code will fail unless you fix it. See warning: implicit declaration of function for more info.


Your program has another bug:

     mid=((h-l)+l)/2;

That is the same as mid=h/2; which is not what you want. In particular at some point you call Binsearch() with h=8 and l=5, which makes mid=4. Since p > arr[mid] you call Binsearch(arr,h,mid+1, p);, and mid+1 == 5 again, so your program makes no progress, performs infinite recursion, and crashes.

Rethink the math and decide what that line should be instead.

For general advice on debugging small programs like this, see https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. In particular, using a debugger made it very easy for me to see where the program failed, and which values of h and l caused it. I suggest practicing using a debugger for yourself.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • can you elaborate please and help me to correct same program but with an output. – Onkar Chougule May 23 '21 at 10:25
  • 1
    @OnkarChougule: I think I explained the compiler warning as completely as I can. I also found another bug, and have added some notes about it. I am not sure what you mean by "correct same program but with an output." If you have issues with your program besides the implicit declaration warning, you should ask that as a new question. – Nate Eldredge May 23 '21 at 15:33