Info
This question is closed. Reopen it to edit or answer.
What is wrong with my function? (Lesson 5 wrap up assignment)
1 view (last 30 days)
Show older comments
function valid = valid_date (year,month,day)
if nargin <3
valid = false;
return
end
if (isscalar(year)) && (year >= 1) && (year == fix(year))
valid = true;
elseif (isscalar(month)) && (month >= 1) && (month==fix(month))
valid = true;
elseif (isscalar(day)) && (day >= 1) && (day == fix(day))
valid = true;
else
valid = false;
end
if ((rem(year,4) == 0) || (rem(year,400)==0)) && (rem(year,100)~=0)
if (month == 2) && (day<30)
valid = true;
elseif (month == 1||3||5||7||9||11) && (day<32)
valid = true;
elseif (month == 4||6||8||10||12) && (day<31)
valid = true;
else
valid = false;
end
else
valid = false;
return
end
if (month == 1||3||5||7||9||11) && (day<32)
valid = true;
elseif (month ==2) && (day<29)
valid = true;
elseif (month == 4||6||8||10||12) && (day<31)
valid = true;
else
valid = false;
end
end
0 Comments
Answers (2)
IMAD ACHAHBOUN
on 20 Apr 2021
function [valid] = valid_date(year,month,day)
if nargin==3
if month==fix(month) && isscalar(month) && day==fix(day) && isscalar(day) && year==fix(year) && isscalar(year) && isscalar(valid_date) && year>0 && month>0 && day>0
if mod(year,4)==0&&mod(year,100)~=0 || mod(year,400)==0&&mod(year,100)==0
if ismember(month,[1,3,5,7,8,10,12]) && ismember(day,[1:31])
valid=true;
elseif month==2 && ismember(day,[1:29])
valid=true;
elseif ismember(month,[4,6,9,11]) && ismember(day,[1:30])
valid=true;
else
valid=false;
end
else
if ismember(month,[1,3,5,7,8,10,12]) && ismember(day,[1:31])
valid=true;
elseif ismember(month,[4,6,9,11]) && ismember(day,[1:30])
valid=true;
elseif month==2 && ismember(day,[1:28])
valid=true;
else
valid=false;
end
end
else
valid=false;
end
else
valid=false;
end
0 Comments
Abhishek Inamdar
on 13 Jun 2021
function valid = valid_date (year, month, day)
if nargin == 3
if isscalar(month) && isscalar(day) && isscalar(year) && month==fix(month) && day==fix(day) && year==fix(year) && year>0 && month>0 && day>0
if mod(year,4)==0 && mod(year,100)~=0 || mod(year,400)==0 && mod(year,100)==0
if ismember(month,[1,3,5,7,8,10,12]) && ismember(day,[1:31])
valid=true;
elseif month==2 && ismember(day,[1:29])
valid=true;
elseif ismember(month,[4,6,9,11]) && ismember(day,[1:30])
valid=true;
else
valid=false;
end
else
if ismember(month,[1,3,5,7,8,10,12]) && ismember(day,[1:31])
valid=true;
elseif ismember(month,[4,6,9,11]) && ismember(day,[1:30])
valid=true;
elseif month==2 && ismember(day,[1:28])
valid=true;
else
valid=false;
end
end
else
valid=false;
end
else
valid=false;
end
Keep isscalar first in the line 3 is importatnt, first we need to check scalar
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!