Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int i,l,u,mid,a[10],val;
  6. l=0;
  7. u=9;
  8.  
  9.  
  10. cout<<"Enter elements in the array";
  11. for(i=0;i<10;i++){
  12. cin>>a[i];
  13. }
  14. for(i=0;i<10;i++){
  15. cout<<a[i]<<" ";
  16. }
  17. cout<<"ENter the element you want to search for";
  18. cin>>val;
  19. while(l<=u){
  20. mid=(l+u)/2;
  21.  
  22. if(a[mid]==val){
  23. cout<<"Element found at"<<i;
  24. } else if(a[mid]<val){
  25. u=mid-1;
  26. }
  27. else if(a[mid]>val){
  28. l=mid+1;
  29. } else {
  30. cout<<"element not in the array";
  31. }
  32. }
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement