Write a function called day_diff 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 dif
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I can't deal it anymore. I have trouble to check an integer number or an array number in order to input argument in this problem .
function dd = day_diff(month1, day1, month2, day2)
if (month1 && day1 >0)&&(month1
if true
% code
end <= 12)&&(month2 && day2 >0) && month2 <= 12
if isinteger(month1 &&
if (month1 == 1 && month2 == 1)||(month1 == 3 && month2 == 3)||(month1 == 5 && month2 == 5)||(month1 == 7 && month2 == 7)||(month1 == 8 && month2 == 8)||(month1 == 10 && month2 == 10)||(month1 == 12 && month2 == 12) && day1<=31 && day2<=31
if day1 == day2
total1 = day1;
total2 = day2;
elseif day1 ~= day2
total1 = max(day1,day2);
total2 = min(day1,day2);
end
elseif (month1 == 4 && month2 == 4) ||(month1 == 6 && month2 == 6)||(month1 == 9 && month2 == 9)||(month1 == 11 && month2 == 11) % months have 30 days
if day1 == day2 && day1<=30 && day2<30
total1 = day1;
total2 = day2;
elseif day1 ~= day2 && day1<=30 && day2<30
total1 = max(day1,day2);
total2 = min(day1,day2);
else
dd=-1; return
end
elseif month1 == 1 && month2 ==2
total1 = day1;
total2 = day2+31;
elseif (month1 == 2 && day1<=28) && month2 == 1
total1 = day1 + 31;
total2 = day2;
elseif (month1 == 2 && day1>28) && month2 == 1
dd=-1;
return
elseif month1 == 1 && month2 == 12
total1 = day1;
total2 = day2 + 334;
elseif month1 == 2 && month2 == 3
total1 = day1 + 31;
total2 = day2 + 59;
elseif month1 == 1 && day1<=31&& month2 == 4
total1 = day1;
total2 = day2 + 90;
elseif month1 == 1 && day1>31 && month2 == 4
dd=-1; return
elseif month1 == 11 && day1<=30 && month2 == 12
total1 = day1 ;
total2 = day2 + 30;
elseif month1 == 11 && day1>30 && month2 == 12
dd=-1;
return
elseif month1 == 7 && month2 == 9
total1 = day1 + 181;
total2 = day2 + 243;
elseif month1 == 2 && day1<=28 && month2 == 6 && day2>30
dd=-1; return
elseif month1 == 2 && day1<=28 && month2 == 6 && day2<=30
total1 = day1;
total2 = day2 + 92;
end
dd = (max(total1,total2)) - (min(total1,total2));
else
dd = -1;
return
end
end
0 comentarios
Respuestas (4)
Image Analyst
el 26 de Nov. de 2016
Why not make your function just be a wrapper for etime()? Did they specifically prohibit using that function?
4 comentarios
Image Analyst
el 26 de Ag. de 2017
Floor rounds down to minus infinity. Fix rounds towards zero. They give different results when operating on negative numbers.
Ugur Ulas
el 29 de Dic. de 2017
Editada: Ugur Ulas
el 29 de Dic. de 2017
Here there is more basic solution:
function dd = day_diff(m1, d1, m2, d2)
A = [31 28 31 30 31 30 31 31 30 31 30 31]';
day1 = d1 + sum(A(1:(m1-1)));
day2 = d2 + sum(A(1:(m2-1)));
if prod(size(m1)) ~= 1 || prod(size(m2)) ~= 1 || prod(size(d1)) ~= 1 || prod(size(d2)) ~= 1
dd = -1;
elseif m1 < 1 || m2 < 1 || d1 < 1 || d2 < 1 || m1 ~= floor(m1) || m2 ~= floor(m2) || d1 ~= floor(d1) || d2 ~= floor(d2)
dd = -1;
elseif A(m1) < d1 || A(m2) < d2
dd = -1;
else
dd = abs(day2-day1);
end
end
4 comentarios
Raunil Raj
el 7 de Mzo. de 2018
Thank you for you response but I actually wanted to know the logic behind this:
elseif A(m1) < d1 || A(m2) < d2
dd = -1;
Image Analyst
el 8 de Mzo. de 2018
It says to set dd equal to minus one if A is less than d1 at the m1 index, or if A is less than d2 at the m2 index.
Vignesh M
el 4 de Mayo de 2018
if true
function [ dd ] = day_diff( m1,d1,m2,d2 )
% Short circuiting!
N = [31,28,31,30,31,30,31,31,30,31,30,31];
if isscalar(m1) && isscalar(d1) && isscalar(m2) && isscalar(d2) && ... m1 == fix(m1) && d1 == fix(d1) && m2 == fix(m2) && d2 == fix(d2) &&... m1 > 0 && m1 <= 12 && m2 > 0 && m2 <= 12 && ... d1 > 0 && d1 <= N(m1) && d2 > 0 && d2 <= N(m2)
day1 = d1 + sum(N(1:1:m1-1));
day2 = d2 + sum(N(1:1:m2-1));
dd = abs(day1-day2)
else
dd = -1
end
end
0 comentarios
RAMAKANT SHAKYA
el 8 de Feb. de 2019
function dd=day_diff(m1,d1,m2,d2)
num_of_days=[31,28,31,30,31,30,31,31,30,31,30,31];
dd=-1;
mmm1=round(m1);mmm2=round(m2);ddd1=round(d1);ddd2=round(d2);
if(isscalar(d1)&&isscalar(d2)&&isscalar(m1)&&isscalar(m2)) % checking for valid input
if(d1-ddd1 || d2-ddd2 || m1-mmm1 || m2-mmm2) % checking for valid input
dd=-1;
else
if (m1<1||m1>12||m2<1||m2>12) %checking months
dd=-1;
else
if(d1>num_of_days(m1) || d2>num_of_days(m2) || d1<1 || d2<1) %validating days of respective month
dd=-1;
else
dd1=sum(num_of_days(1:m1));
dd1=dd1+d1-num_of_days(m1);
dd2=sum(num_of_days(1:m2));
dd2=dd2+d2-num_of_days(m2);
if(dd1>=dd2)
dd=dd1-dd2;
else
dd=dd2-dd1;
end
end
end
end
end
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!