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

Suppose that you wish to write a MATLAB script that simulates the dates of birth

ID: 3589622 • Letter: S

Question

Suppose that you wish to write a MATLAB script that simulates the dates of birth (day and month only) for n people and determines if at least two of the n people have the same birth date. Each person has a 1 in 365 chance of having a birthday on a specific day and month (excluding leap years). For the birth dates, for example, January 1 is represented as birth date 1, and September 17 is represented as birth date 260. Write a MATLAB program that prompts the user for the number of people n to compare and randomly generates the birth dates for the n people. It should determine if at least two people of the n people have the same birthday. If the program determines that two people are found to have the same birthday during the analysis of all of the birthdays, no additional comparisons are required, and the program should output to the command window that two people share the same birthday. If none of the n people are found to have the same birthday, the program should output to the command window that no one was found to have the same birthday. Turn in a hardcopy of your code as well as results for n = 2, n = 30, n = 75, n = 100, and n = 250.

Explanation / Answer

n = 250
% generate random vector of n numbers in range of 1,365
r = randi([1 365],1,n);
% get the unique values in vector r
c = unique(r);
% get duplicate elements in vector
same = length(r)-length(c);
if(same==0)
disp("No one was found to have the same birthday");
else
disp("Two people share the same birthday");
end

%%%% sample output %%%%%%
%
%n = 2
%No one was found to have the same birthday
%n = 30
%Two people share the same birthday
%n = 75
%Two people share the same birthday
%n = 100
%Two people share the same birthday
%n = 250
%Two people share the same birthday

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote