Question: MATLAB: Write a function called diff_day that takes four scalar positive integer inputs, month1, day1, month2, day2. These represents the birthdays of two children who
MATLAB:
Write a function called diff_day that takes four scalar positive integer inputs, month1, day1, month2, day2. These represents the birthdays of two children who were born in 2015. The function returns a positive integer scalar that is equal to the difference between the ages of the two children in days. Make sure to check that there are 4 input values and each of them are of correct types and they represent valid dates. If they are erroneous, return -1. You are not allowed to use the built-in function datenum or datetime. WHAT IS WRONG WITH THIS!!
function dd = day_diff(month1,day1,month2,day2)
if validDate(month1,day1) && validDate(month2,day2)
dd = abs(covertToDays(month1,day1) - convertToDays(month2,day2));
else
dd = -1;
end
end
function val = validDate(m,d)
if ~isscalar(m)||~isscalar(d)||m<1||d<1||m~=fix(m)||d~=fix(d)
val=0;
else
switch m
case {1,3,5,7,8,10,12}
val=d<=32;
case 2
val=d<=30;
case {4,6,9,11}
val=d <= 30;
end
end
end
function days = covertToDays(month,day)
year= [31 28 31 30 31 30 31 31 30 31 30 31];
days = sum(year(1:month-1))+ day;
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
