hey,
Using old code(non-ANSI)
The search detects elements in all positions in the 1-d array, except the last one
pastebin- *pastebin.com/f4db70a8b
Does anyone see my mistake? debugging is not my strong suit
Using old code(non-ANSI)
The search detects elements in all positions in the 1-d array, except the last one
pastebin- *pastebin.com/f4db70a8b
//program to run binary search
#include <iostream.h>
int main ()
{
int a[10],n,x,pos=-1,i,first,last,mid;
cout<<" Enter the number of elements into the array ";
cin>>n;
for(i=0;i<n;++i)
{
cout<<"\n Enter element number "<<i+1<<" ";
cin>>a;
}
cout<<"Enter the number to be searched \t";
cin>>x;
first=0;
last=n-1;
while((first<=last)&&(pos==-1))
{
mid=(first+last)/2;
if(a[mid]==x)
pos=mid;
else
last=mid-1;
}
if(pos==-1)
cout<<" \t Element not found ";
else
cout<<" Element found at position "<<pos+1;
return 0;
}
Does anyone see my mistake? debugging is not my strong suit