Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int main()
- {
- int i,l,u,mid,a[10],val;
- l=0;
- u=9;
- cout<<"Enter elements in the array";
- for(i=0;i<10;i++){
- cin>>a[i];
- }
- for(i=0;i<10;i++){
- cout<<a[i]<<" ";
- }
- cout<<"ENter the element you want to search for";
- cin>>val;
- while(l<=u){
- mid=(l+u)/2;
- if(a[mid]==val){
- cout<<"Element found at"<<i;
- } else if(a[mid]<val){
- u=mid-1;
- }
- else if(a[mid]>val){
- l=mid+1;
- } else {
- cout<<"element not in the array";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement