A community for students.
Here's the question you clicked on:
 0 viewing
tasreeb
 3 years ago
binary search program in c++??
tasreeb
 3 years ago
binary search program in c++??

This Question is Closed

Isaacisbeast123
 3 years ago
Best ResponseYou've already chosen the best response.1do you need the whole process??

Isaacisbeast123
 3 years ago
Best ResponseYou've already chosen the best response.1Either 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(); }

Isaacisbeast123
 3 years ago
Best ResponseYou've already chosen the best response.1Or 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=mid1; 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(); }

krithika
 3 years ago
Best ResponseYou've already chosen the best response.0Try 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=n1; f=0; while(top<=bottom && f==0) { mid=(top+bottom)/2; if(s>a[mid]) { top=mid+1; } else if(s<a[mid]) { bottom=mid1; } else f=1; } if(f==1) cout<<"Element found"; else cout<<"Element not found."; getch(); }
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.