A message for Linux.com registered users: We are in the process of making changes to the Linux forums. From Monday, 8/13/18 at 6:00 PM PT to Tuesday, 8/14/18 at 4:00 PM PT, you will be unable to access the forums. They will re-launch as soon as possible on Tuesday, 8/14/18 with new features and improved functionality. Thank you for your patience and stay tuned for the new improved forums.

April 3, 2011

HELP... error in my simple binary search program

Could any help me with a runtime error in my program?

In the program of binary search, I am not getting output if I choose my searching element more than the middle element.
Could anyone please help me to debug it?


#include
using namespace std;
int main()
{
int a[100],n,ele,mid,pos=0;
cout<<"\nEnter the number of elements you want to enter in an array: ";
cin>>n;
cout<<"\nEnter the array: ";
for(int i=0;i
cin>>a[i];
cout<<"\nEnter the element you want to search: ";
cin>>ele;
int low=0,high=n-1;
while(low<=high)
{
mid=low+high/2;
if(ele==a[mid])
{
pos=mid+1;
break;
}
else if(ele
high=mid-1;
else
low=mid+1;
}
if(pos!=0)
cout<<"\nThe element you searched is "<
else
cout<<"\nThe element you were trying to search was not found !"<
return 0;
}

Click Here!