Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Flipping a coin is ultimately a binary outcome (heads or tails). The code below

ID: 3789199 • Letter: F

Question

Flipping a coin is ultimately a binary outcome (heads or tails). The code below simulates flipping a coin by using a random number generator with 0 and 1 as the possible outcomes and identifies Flip = 1 as heads and Flip = 0 as tails. clear all; Flip = Randi ([0 1], 1, 1); %Generating one random integer of 1 or 0 fprintf ('%d ', Flip); %Let the user know the outcome. if Flip(1) == 1 fprintf ('The coin flip was Heads '); elseif Flip(1) == 0 fprintf('The coin flip was Tails '); end Write a MATLAB Script that can handle two coin flips and the four possible outcomes. Use the above script as a starting point. Make sure your fprintf acknowledges both flip results.

Explanation / Answer

% matalb code

% generating two random integers of 1 and 0
Flip = randi([0 1],1,2);
fprintf("coin1: %d ",Flip(1));
fprintf("coin2: %d ",Flip(2));

if Flip(1) == 1 && Flip(2) == 1
fprintf("First coin flip was Head and second coin flip was also Head ");
elseif Flip(1) == 1 && Flip(2) == 0
fprintf("First coin flip was Head and second coin flip was Tail ");
elseif Flip(1) == 0 && Flip(2) == 1
fprintf("First coin flip was Head and second coin flip was Head ");
elseif Flip(1) == 0 && Flip(2) == 0
fprintf("First coin flip was Tail and second coin flip was also Tail ");
end

%{
sample output:

coin1: 1
coin2: 0
First coin flip was Head and second coin flip was Tail

%}