Need some help with the codes

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);
which would make dd equal 2. You are not allowed to use the built-in function datenum or datetime. Hint: store the number of days in the months of 2015 in a 12-element vector (e.g., 31, 28, 31, 30 …) and use it in a simple formula.
Can anyone help take a look at my code? i can run anything correctly, however the grader is unable to recognize my code,any suggestions?
function dd=day_diff (month1, day1,month2, day2)
if (month1 ==1||month1== 3 ||month1==5 ||month1==7 ||month1==8 ||month1==10||month1== 12)...
&&day1>=1&&day1<=31
a=1;
elseif (month1==4||month1==6||month1==9||month1==11)&&day1>=1&&day1<=30
a=1;
elseif month1==2&&day1>=1&&day1<=28
a=1;
else a=-1; fprintf 'date error\n'
return
end
if (month2 ==1||month2== 3 ||month2==5 ||month2==7 ||month2==8 ||month2==10||month2== 12)...
&&day2>=1&&day2<=31
a=1;
elseif (month2==4||month2==6||month2==9||month2==11)&&day2>=1&&day2<=30
a=1;
elseif month2==2&&day2>=1&&day2<=28
a=1;
else a=-1; fprintf 'date error\n'
return
end
x=[31,28,31,30,31,30,31,31,30,31,30,31];
d1=day1+sum(x(1:month1-1));
d2=day2+sum(x(1:month2-1));
dd=d2-d1;
if dd<0
dd=dd*-1;
else dd=dd;
end
end
below is the grader result
Problem 4 (day_diff):
Feedback: Your function performed correctly for argument(s) 1, 30, 2, 1
Feedback: Your function performed correctly for argument(s) 1, 1, 1, 1
Feedback: Your function performed correctly for argument(s) 1, 1, 1, 2
Feedback: Your function performed correctly for argument(s) 1, 2, 1, 1
Feedback: Your function performed correctly for argument(s) 1, 1, 2, 1
Feedback: Your function performed correctly for argument(s) 2, 1, 1, 1
Feedback: Your function performed correctly for argument(s) 1, 31, 2, 1
Feedback: Your function performed correctly for argument(s) 2, 1, 1, 31
Feedback: Your function performed correctly for argument(s) 1, 1, 12, 31
Feedback: Your function performed correctly for argument(s) 2, 1, 3, 1
Feedback: Your function performed correctly for argument(s) 7, 1, 9, 30
Feedback: Your program made an error for argument(s) 2, 29, 1, 22
Your solution is _not_ correct.

2 件のコメント

Walter Roberson
Walter Roberson 2019 年 1 月 5 日
You are not asked to print "date error".
Walter Roberson
Walter Roberson 2019 年 1 月 5 日
Hint:
days_in_month = [31 28 31 30 31 30 31 31 30 31 30 31];
if some_code_here && day1 <= days_in_month(month1)

回答 (1 件)

Rik
Rik 2019 年 1 月 5 日
編集済み: Rik 2019 年 1 月 5 日

0 投票

You are almost correct. If you look at the sidebar you will see several m-lint warnings. The solution is to actually return a -1, not set a to -1 and then return. Currently your output is not set when you return an error-code. (so do dd=-1;return instead of a=-1;return)
Also, you should probably have a look at the ismember function, it will make your code look much cleaner.

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

タグ

タグが未入力です。

質問済み:

2019 年 1 月 5 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by