14W-CF 02082011 Campos,Tyler HW4 28

14W-CF 02082011 Campos,Tyler HW4 28

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Matlab Code clear;clc n = 0; r = randi(1000); c = 'y'; while c == 'y'; Guess = input('Guess a number between 1 and 1000: '); if Guess == r n = n+1; c = 'n'; fprintf('You have guessed %.0f correctly in %0.1f guesses.', r,n); elseif Guess > r n = n+1; fprintf('Your guess was too high. Please guess again.\n'); elseif Guess < r n = n+1; fprintf('Your guess was too low. Please guess again.\n'); end end Sample Run: Guess a number between 1 and 1000: 500 Your guess was too high. Please guess again. Guess a number between 1 and 1000: 250 Your guess was too high. Please guess again. Guess a number between 1 and 1000: 250 Your guess was too high. Please guess again. Guess a number between 1 and 1000: 100 Your guess was too high. Please guess again. Guess a number between 1 and 1000: 50
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Your guess was too low. Please guess again. Guess a number between 1 and 1000: 75 Your guess was too low. Please guess again. Guess a number between 1 and 1000: 85 Your guess was too low. Please guess again. Guess a number between 1 and 1000: 90 Your guess was too low. Please guess again. Guess a number between 1 and 1000: 95 Your guess was too low. Please guess again. Guess a number between 1 and 1000: 97 Your guess was too low. Please guess again. Guess a number between 1 and 1000: 99 Your guess was too high. Please guess again. Guess a number between 1 and 1000: 98 You have guessed 98 correctly in 12.0 guesses. 14W-CF 02/08/2011 Campos,Tyler HW4 2/8 Tuesday, March 08, 2011 2:10 AM HW4 Page 1...
View Full Document

Ask a homework question - tutors are online