{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

HW8 #2

# HW8 #2 - if lottonums(k == luckynumbers(j%if a match is...

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 if luckynumbers(k) == luckynumbers(j) & k~=j 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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern