Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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

1 Ansicht (letzte 30 Tage)
QUAZI MOHD ARMAN UZ-ZAMAN
QUAZI MOHD ARMAN UZ-ZAMAN am 8 Aug. 2020
Geschlossen: Stephen23 am 13 Jun. 2021
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

Antworten (2)

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

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by