If Else problem Valid_date code.
Afficher commentaires plus anciens
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
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.
Jeremy
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.
Jeremy
le 4 Mai 2024
Jeremy
le 4 Mai 2024
Jeremy
le 4 Mai 2024
Réponses (1)
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
En savoir plus sur Time Series Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!