Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (tasreeb):

binary search program in c++??

OpenStudy (anonymous):

do you need the whole process??

OpenStudy (anonymous):

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(); }

OpenStudy (anonymous):

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(); }

OpenStudy (anonymous):

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(); }

OpenStudy (tasreeb):

thanks

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!