Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc;
- input('think of a number from 0 to 100,000\n');
- clc;
- n=1;
- xmin=0;
- xmax=10;
- guess=(xmax-xmin)./2;
- final=-1;
- while final==-1;
- guess=(xmax+xmin)./2;
- fix guess;
- if xmin==xmax
- final=guess;
- clc;
- fprintf('your number is %1.f \n',final);
- end
- fprintf('my guess is %1.f \n',guess)
- ask1=input('is my guess correct? 1 for yes, 0 for no\n');
- if ask1==1
- final=guess;
- clc;
- fprintf('your number is %1.f \n',final);
- break
- else
- clc;
- fprintf('my guess is %1.f \n',guess)
- end
- ask2=input('is your number greater than my guess? 1 for yes, 0 for no \n');
- clc;
- if ask2==0
- xmax=guess;
- else
- xmin=guess;
- end
- n=n+1;
- end
- fprintf('iterations= %1.f \n',n);
Add Comment
Please, Sign In to add comment