HW4_Campos_Tyler

HW4_Campos_Tyler - 14W-CF 02/08/2011 Campos,Tyler HW4 1/8...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Scholar: HW4 Problem 1 Problem : Consider a computer number guessing game suitable for middle-school children. The program generates a random integer between 1 and 1000 which the user must guess exactly. The user continues to guess until correct. The program will respond to incorrect guesses by telling the user if his/her guess was too high or too low, then asking the user to guess again. The program will keep track of the total number of guesses made and report that at the end of the program. Diagram : NA Solution : Flow Chart 14W-CF 02/08/2011 Campos,Tyler HW4 1/8 Wednesday, February 02, 2011 2:06 AM HW4 Page 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight 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 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

HW4_Campos_Tyler - 14W-CF 02/08/2011 Campos,Tyler HW4 1/8...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online