MATLAB Answers

Write a function three positive integer scalar inputs year, month, day.

124 views (last 30 days)
Taekyoung Lee
Taekyoung Lee on 1 Feb 2020
Commented: Taekyoung Lee on 2 Feb 2020
Write a function called valid_date that takes three positive integer scalar inputs year, month, day. If these three represent a valid date, return a logical true, otherwise false. The name of the output argument is valid. If any of the inputs is not a positive integer scalar, return false as well. Note that every year that is exactly divisible by 4 is a leap year, except for years that are exactly divisible by 100. However, years that are exactly divisible by 400 are also leap years. For example, the year 1900 was not leap year, but the year 2000 was.
%Code to call your function
valid = valid_date(2018,4,1)
valid = valid_ date(2018,4,31)
I only got 1 of 6 which is random leap years.
Here is my code.
function valid = valid_date(year,month,day)
if ~isscalar(year) || year <1 || year ~= fix(year);
valid = false;
end
if ~isscalar(month) || month <1 || month ~= fix(month) || month > 12;
valid = false;
end
if ~isscalar(day) || day <1 || day ~= fix(day) || day > 31;
valid = false;
end
if year/4 == 0 && year/100 == 0 && year/400 == 0;
if month == [2,4,6,9,11];
if month == 2 && day > 29;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month ~= [2,4,6,9,11];
if day > 31;
valid = false;
else
valid = true;
end
end
elseif year/4 == 0 && year/100 ~= 0 && year/400 ~= 0;
if month == [2,4,6,9,11];
if month == 2 && day > 29;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month ~= [2,4,6,9,11];
if day > 31;
valid = false;
else
valid = true;
end
end
elseif year/4 == 0 && year/100 == 0 && year/400 ~= 0;
if month == [2,4,6,9,11];
if month == 2 && day > 28;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month ~= [2,4,6,9,11];
if day > 31;
valid = false;
else
valid = true;
end
end
else year/4 ~= 0 && year/100 ~= 0 && year/400 ~= 0;
if month == [2,4,6,9,11];
if month == 2 && day > 28;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month ~= [2,4,6,9,11];
if day > 31;
valid = false;
else
valid = true;
end
end
end

  3 Comments

Mohammad Sami
Mohammad Sami on 1 Feb 2020
There are a few issues with your code.
  1. For the first three if statements checking the validity of the input, add a return statement after valid = false. This will prevent the rest of your code from executing.
  2. when you are equating month with an array month == [2,4,6,9,11], this returns a logical array. You need a logical scalar. use function any or all to convert logical array to a scalar. e.g. any(month == [2,4,6,9,11])
  3. your condition year/4 and others are not giving you the remainder, you need to use mod or rem function to get the remainder
Taekyoung Lee
Taekyoung Lee on 2 Feb 2020
@Mohammad Sami
Thank you for your support! I got 6 of 6!! Thank you so much ones agian.
function valid = valid_date(year,month,day)
if ~isscalar(year) || year <1 || year ~= fix(year);
valid = false;
return
end
if ~isscalar(month) || month <1 || month ~= fix(month) || month > 12;
valid = false;
return
end
if ~isscalar(day) || day <1 || day ~= fix(day) || day > 31;
valid = false;
return
end
if rem(year,4) == 0 && rem(year,100) == 0 && rem(year,400) == 0;
if month == 2 || month == 4 || month == 6 || month == 9 || month == 11;
if month == 2 && day > 29;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;
if day > 31;
valid = false;
else
valid = true;
end
end
elseif rem(year,4) == 0 && rem(year,100) ~= 0 && rem(year,400) ~= 0;
if month == 2 || month == 4 || month == 6 || month == 9 || month == 11;
if month == 2 && day > 29;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;
if day > 31;
valid = false;
else
valid = true;
end
end
elseif rem(year,4) == 0 && rem(year,100) == 0 && rem(year,400) ~= 0;
if month == 2 || month == 4 || month == 6 || month == 9 || month == 11;
if month == 2 && day > 28;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;
if day > 31;
valid = false;
else
valid = true;
end
end
else rem(year,4) ~= 0 && rem(year,100) ~= 0 && rem(year,400) ~= 0;
if month == 2 || month == 4 || month == 6 || month == 9 || month == 11;
if month == 2 && day > 28;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;
if day > 31;
valid = false;
else
valid = true;
end
end
end
Capture.PNG

Sign in to comment.

Answers (1)

Subhadeep Koley
Subhadeep Koley on 1 Feb 2020
Refer the code below. Hope this helps!
function valid = validateDate(year, month, day)
year1 = year/4;
leap1 = year1 == round(year1); % Condition 1
year2 = year/100;
leap2 = year2 == round(year2); % Condition 2
year3 = year/400;
leap3 = year3 == round(year3); % Condition 3
if leap1
leapYear = 1;
elseif leap2
leapYear = 0;
elseif leap3
leapYear = 1;
else
leapYear = 0;
end
if isscalar(year) && year >= 1 && isscalar(month) && month >= 1 && month <= 12 && isscalar(day) && day >= 1 && day <= 31
if leapYear
if month == 2
valid = day <= 29;
elseif month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12
valid = day <= 31;
else
valid = day <= 30;
end
else
if month == 2
valid = day <= 28;
elseif month == 1 || month ==3 || month ==5 || month ==7 || month == 8 || month == 10 || month == 12
valid = day <= 31;
else
valid = day <= 30;
end
end
else
valid = false;
end
end

  0 Comments

Sign in to comment.

Sign in to answer this question.