MATLAB Answers

Info

This question is closed. Reopen it to edit or answer.

What is wrong with my function? (Lesson 5 wrap up assignment)

32 views (last 30 days)
QUAZI MOHD ARMAN UZ-ZAMAN
Closed: Stephen Cobeldick on 13 Jun 2021 at 4:40
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

Answers (2)

IMAD ACHAHBOUN
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

Abhishek Inamdar
Abhishek Inamdar on 13 Jun 2021 at 3:40
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

Community Treasure Hunt

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

Start Hunting!