Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Whats wrong with my code,I'm not able to get though my test if the month is 12?

1 回表示 (過去 30 日間)
Vijayramanathan B.tech-EIE-118006077
Vijayramanathan B.tech-EIE-118006077 2018 年 2 月 11 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
4. 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 difference between the ages of the two children in days. Make sure to check that the input values are of the correct types and they represent valid dates. If they are erroneous, return -1. An example call to the function would be >> dd = day_diff(1,30,2,1); 2 which would make dd equal 2. You are not allowed to use the built-in function datenum or datetime.
function a = day_diff(month1,day1,month2,day2)
if ((month1 && month2)==(1 || 3 || 5 || 7 || 8 || 10 || 12))
month1 = month1*31;
month2=month2*31;
else if ((month1 && month2)==(4 || 6 || 9 || 11))
month1 = month1*30;
month2=month2*30;
else
month1=28;
month2=28;
end
end
finday1=month1+day1;
finday2=month2+day2;
a=abs(finday1-finday2);
end
  5 件のコメント
Vijayramanathan B.tech-EIE-118006077
Vijayramanathan B.tech-EIE-118006077 2018 年 2 月 11 日
@Geoff Hayes This was my call statement day_diff(1, 1, 12, 31)
Geoff Hayes
Geoff Hayes 2018 年 2 月 11 日
also look at
month1 = month1*31;
month2 = month2*31;
If month1 is 1 (January) and month2 is 12 (December), what is the intent of the above code? It is almost as if you are assuming that every month has 31 days (12*31) and then the
a=abs(finday1-finday2);
would make sense. But that assumption is invalid...

回答 (0 件)

この質問は閉じられています。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by