Question

In: Computer Science

You have 21 cards written from 1 to 21. The number K is inputted from a...

You have 21 cards written from 1 to 21. The number K is inputted from a command window (using input command). First, please shuffle these 21 cards such that the sequence becomes random (using rand command). You need to make a function of New_Cards=shuffle(Orig_Card), where New_Cards is a 1X21 vector that has random sequence of the cards (Ex. New_Cards=[ 14 7 21 6….. 9 1 3 5 8]). When the first even number is greater than K, you should print in the command window saying that ‘You win!’, otherwise ‘You lost!’. K should be set as a global variable in the main cell. Please create a function of Output = game(New_Cards). Output is 1 when the first even number is greater than K, and it is 0 when the first even number is less than K.(Using Matlab)

Solutions

Expert Solution

Screenshot

------------------------------------------------------------------------------------

Program

Functions

%function to set a global variable
function setGlobalK(value)
    global k
    k = value;
end
%Function to get global value
function k = getGlobalK
    global k
    k = k;
end
%Function to shuffle the cards
function New_Cards=shuffle(Orig_Card)
    New_Cards=Orig_Card;
    New_Cards=New_Cards(randperm(length(New_Cards)));
end
%Function to check condition
function Output = game(New_Cards)
    evens=New_Cards(mod(New_Cards,2)==0);
    if evens(1)>getGlobalK
      Output=1;
    else
      Output=0;
    end
end

Test

Orig_Card=1:21;
New_Cards=shuffle(Orig_Card);
setGlobalK(input('Enter value of k: '))
Output = game(New_Cards);
if(Output)
   disp('You win!');
else
   disp('You lost!');
end

-------------------------------------------------------

Output

Enter value of k: 10
You win!


Related Solutions

Two cards are drawn at random from a standard deck of 52 cards. The number of...
Two cards are drawn at random from a standard deck of 52 cards. The number of aces drawn is counted. Prepare a probability distribution for this random experiment. Hint: Find the probability that no aces are drawn, exactly one ace is drawn, etc.
In a game of cards you win $1 if you draw an even number, $2 if...
In a game of cards you win $1 if you draw an even number, $2 if you draw diamonds, $5 if you draw a king and $10 if you draw the queen of diamonds. You get nothing for any other card that you draw. In any random draw, how much money should you expect to win? What is the standard deviation of expected winnings?
1) a) What is the minimum number of cards that you must pick out of a...
1) a) What is the minimum number of cards that you must pick out of a shuffled standard 52-card deck to guarantee that you will get at least one face card (King, Queen, or Jack)? Explain your answer. A standard 52 card deck contains 4 suits of 13 cards. b) You are buying a box of 16 truffles as a birthday gift for a friend of yours. You will select which truffles to put in the box. The chocolate store...
1. What are the k-mers of length k = 21 for this sequence read in FASTQ...
1. What are the k-mers of length k = 21 for this sequence read in FASTQ format? @K000384:75:HM57CBBXX:1:1101:25530:1384 1:N:0:GTGGCC CTGGCACTGGGCTTCAAGCTGGGCTACCTTCTGTTT + AAFFFJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ [please be detailed, thanks]
1. Two cards are drawn from a deck of cards (consisting of 52 cards: 13 cards...
1. Two cards are drawn from a deck of cards (consisting of 52 cards: 13 cards for each of the four suits, Spades (S), Hearts (H), Diamonds (D) and Clubs (C)). Drawing is done with replacement, that is, the first card is drawn, recorded and put back in the deck, then the second card is drawn and recorded. Assuming an outcome is recorded in the order which two cards are drawn, what is the sample space for this (random) phenomenon?...
Draw cards from a standard deck until three Aces have appeared. Let X = number of...
Draw cards from a standard deck until three Aces have appeared. Let X = number of cards drawn. Find: P(X > x) P(X = x) E(X) as a simple fraction Var(X) using the method of indicators
You draw cards from a standard deck of 52 playing cards. There are 12 “face cards”...
You draw cards from a standard deck of 52 playing cards. There are 12 “face cards” in the deck (J, Q, or K). Let X be the number of drawings (with replacement) it takes until you get your first face card. Let Y be the number of drawings (with replacement) it takes until you get your fifth face card. Let Z be the number of face cards removed if you draw 10 cards without replacement. (a) Calculate P(X = 5)....
Suppose you have a deck of 40 cards, containing 10 red cards and 30 black cards....
Suppose you have a deck of 40 cards, containing 10 red cards and 30 black cards. A player draws 7 cards from the deck and places them into their hand. The remaining deck of 33 cards has 5 red cards and 5 black cards in the top 10 cards. The other 23 cards in the remaining deck are unknown color. What is the probability of the player's hand having 0,1,2,3,4,5 red cards?
A card is selected from a regular deck of cards.  Count the number of outcomes with:  a) a...
A card is selected from a regular deck of cards.  Count the number of outcomes with:  a) a king or a queen, b) red and ace, c) spade and heart, and d) odd or less than five.
All Code should be written in C: 1. A perfect number is defined as a number...
All Code should be written in C: 1. A perfect number is defined as a number whose proper divisors (factors not including the number itself) add up to the same number. For example, 28 is a perfect number because its perfect divisors are 1, 2, 4, 7, 14, which add up to 28. Write a C function called is_perfect that takes a since integer as input, and returns 1 if the number is perfect, or 0 otherwise. 2. Using the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT