## tasreeb 3 years ago binary search program in c++??

1. Isaacisbeast123

do you need the whole process??

2. Isaacisbeast123

Either use this short equation (arra[mid]!=num) //NOTE THE EXTRA flush AT END. //FYI endl = flush + "\n" (so that can also be used) cout<<"\nnot found" << flush ; else if (arra[mid]==num) cout<<"location is\t"<<mid; getch(); }

3. Isaacisbeast123

Or if your looking for advanced use/// #include<iostream.h> #include<conio.h> #define siz 10 void main() { int arra[siz]={0}; int mid,beg=1,end=siz,loca=0,num; clrscr(); cout<<"Please Enter The Elements Of Array\n"; for(loca=0;loca<siz;loca++) { cin>>arra[loca]; } cout<<"Please enter the required number"; cin>>num; mid=(loca)/2; while(beg<end&&arra[mid]!=num) { if(num<arra[mid]) end=mid-1; else beg=mid+1; mid=(beg+end)/2; } if (arra[mid]!=num) cout<<"\nnot found"; else if (arra[mid]==num) cout<<"location is\t"<<mid; getch(); }

4. krithika

Try this too if you want #include<iostream.h> #include<conio.h> void main() { int a[20],n,i,top,bottom,mid,f,s; clrscr(); cout<<"Enter the value of n:"; cin>>n; cout<<"\nEnter the values:\n"; for(i=0;i<n;i++) { cin>>a[i]; } cout<<"Enter the element to be searched:"; cin>>s; top=0; bottom=n-1; f=0; while(top<=bottom && f==0) { mid=(top+bottom)/2; if(s>a[mid]) { top=mid+1; } else if(s<a[mid]) { bottom=mid-1; } else f=1; } if(f==1) cout<<"Element found"; else cout<<"Element not found."; getch(); }

5. tasreeb

thanks