Hey guys, i am trying to solve this assignemnt. :
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.
I have figured the Hard part but i have problems how to make it accpent only whole possitve numbers and no matrixes as an input. Here is my code :
function [d] = day_diff( month1,day1,month2,day2 ) M=[31,28,31,30,31,30,31,31,30,31,30,31]; if month1==fix(month1)&&month1==fix(month1) && day1==fix(day1) && day2==fix(day2) && isscalar(month1)&&isscalar(day1)&&isscalar(month2)&&isscalar(day2) && day1>0 && day2 > 0 && month1 <=12 && month1 >= 1 && month2 <=12 && month2 >= 1 && M(month1) >= day1 && M(month2) >= day2 d1 = day1 + sum(M(1:1:month1-1)); d2 = day2 + sum(M(1:1:month2-1)); dd = d2-d1; if dd <0 d = dd*(-1); else d = dd; end else d = -1; end end
Best Answer