Unit12LectureProblems_Template

# Unit12LectureProblems_Template - Sample 2(Guessing Game Pts...

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

Sample 1 (Random numbers) Pts Earned Pts Possible Function line correct (name/inputs) 1 1 Comments for purpose, inputs, outputs (if any) 2 2 Loop used properly to iterate over vector 2 2 Sign changes are counted (1 pt per “if” case) 1 3 Correct number of sign changes returned* 2 Total 6 10 * Recommendation: use the same input vector each time. Do not generate a new one. 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; sign = 1; end end

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

View Full Document
Sample 2 Page 3

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sample 2 (Guessing Game) Pts Earned Pts Possible Function line correct (name, no inputs or outputs) 1 1 Comments for purpose, inputs, and outputs 1 1 Random number generated correctly in [0, 100] 2 2 Loop used to repeatedly prompt user 2 2 Number outside of [0, 100] exits the program 2 2 Success message shown if user guesses number 2 2 Total 10 10 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 Sample 2 Page 4...
View Full Document

## This homework help was uploaded on 03/30/2008 for the course CSE 131 taught by Professor Sticklen during the Spring '08 term at Michigan State University.

### Page1 / 4

Unit12LectureProblems_Template - Sample 2(Guessing Game Pts...

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

View Full Document
Ask a homework question - tutors are online