Info

This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
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 Comments
Geoff Hayes
Geoff Hayes on 11 Feb 2018
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...

Answers (0)

Community Treasure Hunt

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

Start Hunting!