If Else problem Valid_date 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. Note that your solution must not contain any of the date related built-in MATLAB functions.
So ive seen other peoples code and ive been trying to get it on my own but to no luck fails all test but non-scalar and random non-leap years
function [valid] = valid_date(year, month, day);
leap_year = false;
valid = true;
if nargin ~= 3
valid = false;
else
end
if isscalar(year) && isscalar(month) && isscalar(day)
%%if isinteger(year) && isinteger(month) && isinteger(day)
if (year>=0) && (13>month>0) && (32>day>0)
%%fix(year), fix(month), fix(day)
if month == 2 && day == 29
leap_year = true;
elseif rem(year,400) == 0
leap_year = true;
elseif rem(year,4) == 0 && rem(year,100) ~= 0
leap_year = true;
else
leap_year = false;
end
if month ~= 1 || month ~= 3 || month ~= 5 || month ~= 7 || month ~= 9 month ~= 12 && day == 31
valid = false;
elseif month == 2 && day > 29
valid = false;
end
else
valid = false;
end
else
valid = false;
end

6 commentaires

Stephen23
Stephen23 le 2 Mai 2024
Modifié(e) : Stephen23 le 2 Mai 2024
A few improvements and bugs, in no particular order:
1- it is simpler and more robust to set the default to FALSE (and only return TRUE when all tests pass):
valid = true;
2- MATLAB does not have any ternary logical operators (i.e. accept three arguments), all logical operators are binary (i.e. accept two arguments), so following the documented rules of operator precedence your code e.g.
13>month>0
is equivalent to:
(13>month)>0
which (depending on the month) will be equivalent to either of these:
true>0 -> true
false>0 -> false
but note: these do NOT test if MONTH>0 !
3- You need to think about your logic here (I fixed the missing || operator):
month ~= 1 || month ~= 3 || month ~= 5 || month ~= 7 || month ~= 9 || month ~= 12 ..
Lets consider month=3, then month~=1 will be TRUE and so the whole list will be TRUE. Can you tell me one month value for which your logic will ever return FALSE ? (Hint: there is no such number). Rather than writing out lots of logical operations, use ISMEMBER.
4- && has higher precedence than ||, so your code (I fixed the missing || operator):
month ~= 1 || month ~= 3 || month ~= 5 || month ~= 7 || month ~= 9 || month ~= 12 && day == 31
is equivalent to this:
month ~= 1 || month ~= 3 || month ~= 5 || month ~= 7 || month ~= 9 || (month ~= 12 && day == 31)
Note how the && only applies to the term immediately preceding it.
5- Skip this test:
if month == 2 && day == 29
because it is already covered by the standard tests of year divisibility.
There might be more bugs. Tip: test everything and read the documentation for every operator that you use.
thanks i dont exactly understand is member
i have remade my code but still fails all the same stuff
function [valid] = valid_date(year, month, day);
%initilize
valid = true;
A = [1 3 5 7 9 12];
B = [4 6 8 10 11];
%checks for exactly 3 inputs
if nargin ~= 3
valid = false;
else
end
%if the inputs are scalar, continue
if isscalar(year) && isscalar(month) && isscalar(day)
%if inputs are a proper y/m/d, continue
if (year>=0) && (month<13 && month>0) && (day<32 && day>0)
if (month == ismember(A,1)) & (day < 32)
valid = true;
elseif (month == ismember(B,1)) & (day < 31)
valid = true;
elseif (month == 2) & (day <= 29)
valid = true;
else
valid = false;
end
else
valid = false;
end
else
valid = false;
end
Stephen23
Stephen23 le 3 Mai 2024
Modifié(e) : Stephen23 le 3 Mai 2024
"i dont exactly understand is member"
Replace this
month == ismember(A,1)
with
ismember(month,A)
Note: it is simpler and more robust to set valid=FALSE as the default.
hello again, ive got it to a point where only last day of month and random dates dont work
function [valid] = valid_date(year, month, day);
valid = false;
A = [1 3 5 7 9 12]; %31 day months
B = [4 6 8 10 11]; %30 day months
if nargin ~= 3
valid = false;
else
end
if isscalar(year) && isscalar(month) && isscalar(day)
if (year>=0) && (month<13 && month>0) && (day<32 && day>0)
if (ismember(month,A)) & (day < 32)
valid = true;
elseif (ismember(month,B)) & (day < 31)
valid = true;
elseif (month == 2) & (day <= 29)
if rem(year,400) == 0 || rem(year,4) == 0 && rem(year,100) ~= 0
valid = true;
elseif rem(year,400) ~= 0 || rem(year,4) ~= 0 && rem(year,100) == 0
valid = false;
end
else
valid = false;
end
else
valid = false;
end
else
valid = false;
end
Jeremy
Jeremy le 4 Mai 2024
im aware that "elseif rem(year,400) ~= 0 || rem(year,4) ~= 0 && rem(year,100) == 0" could just be else valid = false; just now.
Jeremy
Jeremy le 4 Mai 2024
i just figured out my issue thank you for the help

Connectez-vous pour commenter.

Réponses (1)

Les Beckham
Les Beckham le 2 Mai 2024

0 votes

There are several errors. For one thing, you can't combine multiple comparisons into one such as 13>month>0, this must be two conditions: 13>month && month>0. Also, you are setting the leap_year flag but never using it. You should also restore the isinteger tests. Fix up these things and pay attention to the Code Analyzer warnings and you should be able to make it work.

Catégories

Question posée :

le 2 Mai 2024

Commenté :

le 4 Mai 2024

Community Treasure Hunt

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

Start Hunting!

Translated by