Your Binary Search Function's logic is right but You have some simple errors :-
Firstly in the function since u intending using Recursion then u have to return the value that is resulted from the backtracking to a Variable and return this Variable to the Main function.
int bsearch(int a[],int lb,int ub,int target)
{
int result; //Here is the variable that accepts the result of the backtracking
int mid;
if(lb<=ub)
{
mid=(lb+ub)/2;
if(a[mid]==target)
return mid;
else if(a[mid]>target)
result=bsearch(a,lb,mid-1,target);
else
result=bsearch(a,mid+1,ub,target);
}
else
return -1;
return result;
}
Secondly:-
In the main function in case '2' when the function returns the index of the first value of the array to variable i the index that will be returned is 0
and since u made a check if i>0 only it will print "Value is not found"
here is the correction
case 2:
{
printf("\n Enter valu to be search : ");
scanf("%d",&i);
i=bsearch(a,0,n-1,i);
if(i>=0) //Here is the correction just add the "=" sign
printf("\n Value is found at index %d ",i);
else
printf("\n Value is not found ");
break;
}
Thirdly:-
MAKE SURE that the values stored in the array is SORTED .
I'll be happy of Any critique .