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

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

View Full Document Right Arrow Icon
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 changes = changes + 1; sign = -1; changes = changes + 1; sign = 0; changes = changes + 1;
Background image of page 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

Ask a homework question - tutors are online