Guest User

Untitled

a guest
Jul 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. clc;
  2. input('think of a number from 0 to 100,000\n');
  3. clc;
  4. n=1;
  5. xmin=0;
  6. xmax=10;
  7. guess=(xmax-xmin)./2;
  8. final=-1;
  9.  
  10. while final==-1;
  11. guess=(xmax+xmin)./2;
  12. fix guess;
  13. if xmin==xmax
  14. final=guess;
  15. clc;
  16. fprintf('your number is %1.f \n',final);
  17. end
  18. fprintf('my guess is %1.f \n',guess)
  19. ask1=input('is my guess correct? 1 for yes, 0 for no\n');
  20. if ask1==1
  21. final=guess;
  22. clc;
  23. fprintf('your number is %1.f \n',final);
  24. break
  25. else
  26. clc;
  27. fprintf('my guess is %1.f \n',guess)
  28. end
  29. ask2=input('is your number greater than my guess? 1 for yes, 0 for no \n');
  30. clc;
  31. if ask2==0
  32. xmax=guess;
  33. else
  34. xmin=guess;
  35. end
  36. n=n+1;
  37. end
  38.  
  39. fprintf('iterations= %1.f \n',n);
Add Comment
Please, Sign In to add comment