Page 1 of 1

Recursive binary search related

Posted: Tue Oct 09, 2012 9:01 am
by mainul07
#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;
}


please help me i can't complete this code.....

Re: Recursive binary search related

Posted: Tue Oct 09, 2012 8:39 pm
by brianfry713
Change
if(x=a[mid])
to
if(x==a[mid])
http://www.cplusplus.com/reference/algo ... ry_search/

Re: Recursive binary search related

Posted: Sat Oct 13, 2012 1:24 pm
by mainul07
Wow that's great it works after modify..tnx