Please what is wrong with this code?

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. valid = valid_date(2018,4,1) valid = valid_date(2018,4,31)

6 Comments

Ajay - rather than attaching a screen shot of your code, please attach the code itself. Also, please discuss what you thin is wrong with the code. Is there an error? If so, please copy and paste the full error message to this question. Do you get an incorrect answer for some inputs? If so, please copy and paste the inputs that you are using with the observed answer and the expected answer.
Thank you for your reply
it is showing error for
Various inputs
Non-scalar
The last day of every month
Random non-leap years
Random dates
And can you attach the code instead of a screen shot? Also, please include the inputs that you are using (i.e. the test cases).
function valid = valid_date (year, month, day);
%Check if the input has 3 elements:
if nargin < 3;
error('The date must have 3 elements')
else
nargin==3;
validN=true;
end
%Check if the 3 elements are integer, scalars and positives:
%Also for month between 0 and 12.
%also fot days between 0 and 31.
if isscalar(year) && (not(mod(year,1))) == true && year>0
validY = true;
else
error('Year has to be integer, scalar and positive')
end
if isscalar(month) && (not(mod(month,1))) == true && month > 0 && month <= 12
validM = true;
else
error('Month has to be integer, scalar, positive and 0<m<=12')
end
if isscalar(day) && (not(mod(day,1))) == true && day > 0 && day <= 31
validD = true;
else
error('Day has to be integer, scalar, positive and 0<d<=31')
end
if (not(mod(year,4)) == true) && (not(mod(year,400)) == true)
if month == 1 3 5 7 8 10 12;
day <= 31;
elseif month == 2;
day <= 29;
elseif month == 3 4 6 9 11;
day <= 30;
end
else
if month == 1 3 5 7 8 10 12;
day <= 31;
elseif month == 2;
day <= 28;
elseif month == 3 4 6 9 11;
day <= 30;
end
end
if validN == true && validY == true && validM == true && validD == true
valid = true;
else
valid = false;
end
inputs valid =valid_date(2018,4,1) valid = valid_date(2018,4,31) valid= valid_date(2025,6,-8)
Ajay - don't lines like
if month == 1 3 5 7 8 10 12;
cause an error for you? You probably want to replace with something similar to
if ismember(month, [1 3 5 7 8 10 12])
I strongly suggest that you use the MATLAB debugger to step through the code while the function is running. As you step through each line, ask yourself if what you are trying to do makes sense and that the answer you get is expected.

Answers (0)

This question is closed.

Asked:

on 7 Apr 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!