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.
Mostrar comentarios más antiguos
Hi
i am getting valid_date (2018,4,1) output=1
and valid_date (2018,4,31) output=0
but i am getting error in
Assessment result: incorrect The last day of every month. Variable valid has an incorrect value
valid_date(2017,9,31) failed.........
Kindly point out the errors I have made!
function valid = valid_date(year, month, date)
if(nargin ~= 3)
valid = false;
elseif ((~isscalar(year)||(mod(year,1)~=0) || year<0))
valid = false;
elseif ((~isscalar(month))||(mod(month,1)~=0) || (month<=0) || (month>12))
valid = false;
elseif ((~isscalar(date))||(mod(date,1)~=0) || (date<=0))
valid = false;
elseif(any(month==[1:2:7,8:2:12])&& date>31)
valid = false;
elseif (any(month==[4,6,9,11]) && date>30)
valid = false;
elseif month==2 && date>(28+(mod(year,400)==0 || (mod(year,100)~=0 && mod(year,4)==0)))
valid=false;
else
valid = true;
end
4 comentarios
These outputs look correct to me:
>> valid_date(2017,4,1)
ans =
1
>> valid_date(2017,4,31)
ans =
0
>> valid_date(2017,9,30)
ans =
1
>> valid_date(2017,9,31)
ans =
0
Your code looks okay, what specific dates are failing the test?
chaitanya lengure
el 20 de Feb. de 2020
Guillaume
el 20 de Feb. de 2020
This homework again!
You may want to check the previous questions on this exact same homework:
and many more...
Muzaffar Bashir Arain
el 26 de Abr. de 2020
this porgrame works fine i just made simple correction/ addition of end in the last that probabiliy missing... cheers
Respuestas (1)
I verified that your program correctly returns that valid_date(2017,9,31) is false.
It looks like you are using some kind of test script to see if your program is working properly. Maybe there is a problem with this test script, because your program seems to work OK.
1 comentario
chaitanya lengure
el 20 de Feb. de 2020
Categorías
Más información sobre Model Verification en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!