HW8 #2

# HW8#2 - if lottonums(k == luckynumbers(j%if a match is found matches = matches 1 end end end%display number of matches fprintf'The winning lotto

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

Sheet1 Page 1 %Engineering 6, Spring 2004, Problem 8.2 %Ryan Uy, 2509 %Section B05, Tues. 3-4 disp('Play the Lottery Game: Enter Your 6 Lucky Numbers') luckynumbers = zeros(6,1) for k=1:6 %loop 6 times and prompt user for luckynumbers luckynumbers(k) = input('Enter number: ') end numbersvalid = 1 for k=1:6 %loop six times to check if each number is valid if luckynumbers(k)<1 | luckynumbers(k)>49 numbersvalid = 0 break end for j=1:6 numbersvalid = 0 break end end end if numbersvalid == 0 %if numbers were ever invalid, numbersvalid flag would have been set to 0 disp('Invalid numbers. Program halted.') end if numbersvalid == 1 %only executes the code below if numbersvalid = 1, i.e. numbers are all valid rand('state',97) lottonums = zeros(6,1) matches = 0 lottonums = rcu_lottery(lottonums) for k=1:6 %use nested loop for j=1:6 %loop twice to compare each lottonum with each luckynum

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

View Full Document

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

View Full Document

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: if lottonums(k) == luckynumbers(j) %if a match is found matches = matches + 1 end end end %display number of matches fprintf('The winning lotto numbers were %.0f, %.0f, %.0f, %.0f, %.0f, and %.0f. You matched %.0f of the six numbers.', lotto n Sheet1 Page 2 end Sheet1 Page 3 %zero out a 6x1 matrix and allocate space for luckyumbers %set numbersvalid flag to 1 %if number is not between 1 and 49, set numbersvalid to 0 %then break from the loop (not necessary but makes code more efficient) %if two luckynumbers are ever equal, set numbersvalid to 0 %then break from the loop %if the case was such, display error message %set state to 97 %allocate space for lottonums %set number of matches flag to 0 %function call %increment matches flag by one Sheet1 Page 4 Sheet1 Page 5 returns array of 6 non-identical numbers from 1 to 49 Sheet1 Page 6...
View Full Document

## This note was uploaded on 01/22/2011 for the course ENG 006 taught by Professor Lagerstrom during the Spring '06 term at UC Davis.

### Page1 / 6

HW8#2 - if lottonums(k == luckynumbers(j%if a match is found matches = matches 1 end end end%display number of matches fprintf'The winning lotto

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

View Full Document
Ask a homework question - tutors are online