{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

HW4_Curtis_Ryan

# HW4_Curtis_Ryan - 10W-GV HW4 Ryan Curtis Problem ID Scholar...

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

10W-GV HW4 2/9/11 Ryan Curtis 1 Problem ID: Scholar: HW4 Problem 1 Problem Statement: To create a program that will have a user guess a random integer between 1 to 1000 Diagram : N/A Solution: Flowchart: Start N=randi(1000) Input Guess ‘G’ While G~=N If G>N If G<N Input Lower G Input Higher G Output Counter N Stop Count=Count+1

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

View Full Document
10W-GV HW4 2/9/11 Ryan Curtis 2 Listing: %This script file is made so that a middle-school child could play a %guessing game with his/her computer. This program picks a random integer 1 %to 1000 which the user will then have to keep guessing until the number is %chosen. Along the way the program will tell the user if the guess is too %high or too low. At the end once you have chosen the correct number the %program will tell you how many guesses it took to get to that number. N = randi(1000); G = input( 'Guess what number I have chosen 1-1000: \n' ); Count = 0; while G ~= N %This starts the loop while the user is wrong it keeps going through if G > N G = input( 'You are too high guess a lower number: \n' ); %If your too high it will tell you to guess a lower number elseif G < N G = input( 'You are too low guess a higher number: \n' ); %If your too low it will tell you to guess a higher number end Count = Count + 1; %Count will count up how many guesses it takes you end fprintf( 'Correct it only took you %0.0f tries to guess %0.0f \n' ,C ,N); %Output the counter and the number Sample Run: Guess what number I have chosen 1-1000: 200 You are too low guess a higher number: 800 You are too low guess a higher number: 915 You are too high guess a lower number: 845 You are too low guess a higher number: 888 You are too low guess a higher number: 899 You are too low guess a higher number: 913 You are too high guess a lower number: 903
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}