{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Lecture Uploads - sign = 1 end end Guess the Integer...

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

Sample 1 from Lecture material function changes = Sample1(in_vector) % Purpose: To compute the number of sign changes between successive % elements in the input vector. % Input: Vector of values in the interval [-0.5, 0.5] % Output: Number of sign changes (THIS IS OPTIONAL – can print to screen instead) changes = 0; % Number of sign changes sign = 0; % Possible values are -1, 0, 1 to represent the sign for num = in_vector if num < 0 & sign ~= -1 changes = changes + 1; sign = -1; elseif num == 0 & sign ~= 0 changes = changes + 1; sign = 0; elseif num > 0 & sign ~= 1 changes = changes + 1;
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: sign = 1; end end Guess the Integer function Sample2() % Purpose: To choose a random number between 0 and 100 and prompt the user % to guess the number. % Input: None % Output: None random_num = round(rand*100); % Cheaters can remove the semicolon % Run once to seed the loop user_num = input('Pick a number between 0 and 100: '); while (user_num ~= random_num & (user_num >= 0 & user_num <= 100)) user_num = input('Try again! Pick a number between 0 and 100: '); end if user_num == random_num disp('You guessed it!'); else disp('Thanks for playing!'); end...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online