Lottery
The lottery game matches three different integer numbers between 1 and 10. Winning depends on how many matching numbers are provided by a player. The player provides three different integers between 1 and 10.
If there is a match of all 3 numbers, the winning $ 1000.
If there is a match with 2 numbers, the winning $ 10.
If there is a match with 1 number, the winning $ 1.
With no match, the winning is $0.
Write a function Lottery3 that checks three numbers provided by a player and determine the winning amount. If the user mistakenly enters same number twice/thrice and if that number matches one of the winning numbers, the code should count that number only once and display correct result. The player doesn’t have to guess the order of numbers.
The input to the function Lottery3 can have up to two input arguments. The first input argument is a row array numbers with 3 numbers. If the second argument input testCode is present, and is a row vector of 3 values, the function Lottery3 uses the code in testCode as the three winning numbers (the test must be three different integer numbers between 1 and 10), else three different numbers will be automatically generated by testCode.
The ouput should return the variable winnings and the three winning numbers in the row array winNumbers.
Hint: Make use of the internal functions any and nargin.
Restriction: The function must use switch-case statements to determine the winning.
Example #1:
winning = Lottery3( [1,2,1],[1,2,3])
produces
winning =
10
Example #2:
[winning,winNumbers] = Lottery3( [1,2,3])
produces
winning =
3
winNumbers =
8 5 3
function [winning,winNumbers] = Lottery3(numbers,testCode)
winNumbers=randperm(10,3); % generates three different integers between 1 and 10
% Insert code here to see is winNumbers should be overwritten with testCode %
% Insert code here to determine how many winning numbers are present %
% Insert code to calculate the winnings using switch %
end
function [winning, winNumbers] = Lottery3(numbers,
testCode)
winNumbers = randperm(10, 3);
if nargin == 2
winNumbers = testCode;
end
count = 0;
if any(winNumbers(:) == numbers(1))
count = count +1;
end
if numbers(1) ~= numbers(2) && any(winNumbers(:) ==
numbers(2))
count = count +1;
end
if (numbers(1) ~= numbers(3) && numbers(2) ~= numbers(3))
&& any(winNumbers(:) == numbers(3))
count = count +1;
end
switch count
case 0
winning = 0;
case 1
winning = 1;
case 2
winning = 10;
case 3
winning = 1000;
end
end
code to call your function
testCode=[1,2,3]; numbers=[1,2,5];
[winning, winNumbers] = Lottery3(numbers, testCode)
sample run
winning = 10 winNumbers = 1 2 3
Get Answers For Free
Most questions answered within 1 hours.