## Recursive binary search related

Post about everything you want - even if it is not related to programming or this site.

Moderator: Board moderators

mainul07
New poster
Posts: 8
Joined: Fri Oct 05, 2012 1:35 pm
Location: Chittagong
Contact:

### Recursive binary search related

#include<iostream>
using namespace std;
int binsrch(int a[20],int i,int l,int x)
{
int mid;
if(x=a[mid])
{
cout<<mid;
}
else if(x<a[mid])
{
cout<<binsrch(a,i,mid-1,x);
}
else
{

cout<<binsrch(a,mid+1,l,x);

}
}
int main()
{
int a[20],n,i,j,l,x,mid;
cout<<"Enter the dimension:";
cin>>n;
cout<<"Enter the element of array a:";
for(j=0;j<=n;j++)
{
cin>>a[j];
}

cout<<"Enter the element to be search:";
cin>>x;
i=0;
l=n;
mid=(i+l)/2;
binsrch(a[j],mid+1,l,x);
return 0;
}

Mainul Hassan
Website:http://www.teronga.com/

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: Recursive binary search related

Change
if(x=a[mid])
to
if(x==a[mid])
http://www.cplusplus.com/reference/algo ... ry_search/
Check input and AC output for thousands of problems on uDebug!

mainul07
New poster
Posts: 8
Joined: Fri Oct 05, 2012 1:35 pm
Location: Chittagong
Contact:

### Re: Recursive binary search related

Wow that's great it works after modify..tnx
Mainul Hassan
Website:http://www.teronga.com/