Why can't the function handle the error in a given date input?
Show older comments
I'm studying MATLAB on coursera and stuck with a question:
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.
Multiple cases are tested using a pre-made evaluation code.
My code so far:
function answer = day_diff(month1, day1, month2,day2)
answer = -1;
days_in_months = [31,28,31,30,31,30,31,31,30,31,30,31]; %days in every month array
flag1 = days_in_months(month1); %to check if day1 is valid
flag2 = days_in_months(month2); %to check if day2 is valid
%Non valid values handing
if nargin < 4
error('Must have four arguments');
end
if ~isscalar(month1) || month1 < 1 || month1 ~= fix(month1) || month1 > 12
error('month1 needs to be a positive integer and not greater than 12.');
end
if ~isscalar(month2) || month2 < 1 || month2 ~= fix(month2) || month2 > 12
error('month2 needs to be a positive integer and not greater than 12.');
end
if ~isscalar(day1) || day1 < 1 || day1 ~= fix(day1)|| day1 > flag1
error('day1 needs to be a positive integer and a valid date.');
end
if ~isscalar(day2) || day2 < 1 || day2 ~= fix(day2)|| day2 > flag2
error('day1 needs to be a positive integer and a valid date.');
end
%end of error handler
%to find the age in days
if (month1 == month2)
inbetween_days = 0;
if day1 == day2
first_last_days = 0;
elseif day1 < day2
first_last_days = day2 - day1;
else
first_last_days = day1 - day2;
end
elseif month1 < month2
inbetween_days = sum(days_in_months(month1+1:month2-1));
first_last_days = (days_in_months(month1)-day1) + day2;
else
inbetween_days = sum(days_in_months(month2+1:month1-1));
first_last_days = day1 + (days_in_months(month2)-day2);
end
answer = first_last_days + inbetween_days;
end
so far so good but when it's day_diff(2, 29, 1, 22), the evaluation file gives an error instead of handling the wrong pre-defined input (day1 is 29 and maximum is 28), is the problem in how to return the -1 ?
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.
Accepted Answer
More Answers (0)
Categories
Find more on Birthdays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!