Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

tasreeb

  • 2 years ago

binary search program in c++??

  • This Question is Closed
  1. Isaacisbeast123
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    do you need the whole process??

  2. Isaacisbeast123
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks

  6. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find 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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.