필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 30일)
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
@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