HW8 #1

# HW8 #1 - %loop twice to compare each lottonum with each...

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

Sheet1 Page 1 %Engineering 6, Spring 2004, Problem 8.1 %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 for k=1:6 %loop 6 times to set values for lottonums lottonums(k) = floor(rand*49+1) end for k=1:6 %use nested loop for j=1:6

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: %loop twice to compare each lottonum with each luckynum if lottonums(k) == luckynumbers(j) %if a match is found matches = matches + 1 end end end Sheet1 Page 2 %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 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 %lottonums are random numbers between 1 and 49 %increment matches flag by one Sheet1 Page 4...
View Full Document

## HW8 #1 - %loop twice to compare each lottonum with each...

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

View Full Document
Ask a homework question - tutors are online