If else problem for year
Mostrar comentarios más antiguos
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.
valid = valid_date(2018,4,1)
valid = valid_date(2018,4,31)
6 comentarios
RAHUL ANTIL
el 14 de Jun. de 2019
Rik
el 14 de Jun. de 2019
Have you seen my answer below? Also, what is your problem with determining if a year is a leap year? If you don't know about it yet: the mod function could be very helpful.
RAHUL ANTIL
el 14 de Jun. de 2019
Editada: RAHUL ANTIL
el 14 de Jun. de 2019
Matthew Myers
el 16 de En. de 2021
Editada: DGM
el 21 de Feb. de 2023
Hello everyone, I wanted to post my attempt at this and try to understand my mistakes in writing my code.
I notice that during the function, the month does not trigger the "limit" variable that I have set and I cannot understand why. I would appreciate any help with this.
I tried to take your advice Rik in takling each issue one at a time, but I didn't go about it in the right way
function[out] = valid_date(year,month,day)
out = false
if nargin<3
return
elseif ~isscalar(year) || year<1 || year ~= fix(year)
return
elseif ~isscalar(month) || month<1 || month>12 || month ~= fix(month)
return
end
persistent limit;
if isempty(limit), limit = 0;end
if month == (1:2:11)
limit = 31;
elseif month == (4:2:12)
limit = 30;
elseif month == 2
limit = 28;
end
%leap year part. Month must be february
if year == (0:4:3000) && month == 2
limit = 29;
elseif year == (0:100:3000) && month == 2
limit = 28;
else year == (0:400:3000) && month == 2
limit = 29;
end
if ~isscalar(day) || day<1 || day>limit|| day ~= fix(day)
return;
end
if isscalar(day) && day>=1 && day<=limit
out = true;
end
end
Rik
el 16 de En. de 2021
I don't understand your indentation and why you are using a persistent variable. I would also suggest to always make sure that conditionals are scalar. I would also suggest making sure your code can handle dates beyond the year 3000.
Did you use the debugger to step through your code line by line?
Respuesta aceptada
Más respuestas (3)
Muthu Dhanush Santh Nagarajan
el 9 de Abr. de 2020
Editada: DGM
el 21 de Feb. de 2023
I have tried this program and i got all the answers correct. Please check
function valid=valid_date(y,m,d)
valid = false;
if(((isscalar(y) && y>=1 && y==fix(y))&& (isscalar(m) && m>=1 && m==fix(m) && m<=12)...
&& (isscalar(d) && d>=1 && d==fix(d) && d<=31))==1)
c1= (ismember(m,[4,6,9,11]) && ismember(d,[1:30]));
c2=(ismember(m,[1,3,5,7,8,10,12]) && ismember(d,[1:31]));
if ((c1==1 || c2==1)==1)
valid = true;
else
if ((mod(y,4)==0&&mod(y,100)~=0 || mod(y,400)==0&&mod(y,100)==0)==1)
if (ismember(d,[1:29])==1)
valid = true;
end
return;
else
if (ismember(d,[1:28])==1)
valid = true;
end
return;
end
end
end
end
3 comentarios
Rik
el 9 de Abr. de 2020
This function is missing documentation.
And what are the exact requirements of your assignment? You should try to create a test-suite that contains syntaxes that should work and return truie or false, and sytaxes that should return an error.
Also, do you have a question? Because you posted this in the answer section.
Mohammad Aiyoob Rahmani
el 18 de Jun. de 2020
this code is right
it is working properly
abdul kabeer
el 29 de Mzo. de 2023
This is good
Salman P H
el 28 de Abr. de 2020
function valid = valid_date(x,y,z)
t = (isscalar(x) && isscalar(y) && isscalar(z));
if t==false
valid = false;
return;
end
if (x<=0 || y<=0 || z<=0)
valid = false;
return;
end
if any(rem([x, y, z], 1))
valid = false;
return;
end
if (((rem(x,4)==0) && (rem(x,100)~=0)) || (rem(x,400)==0))
a=1;
else
a=0;
end
if (x>0) && a==1
if (y==1 || y==3 || y==5 || y==7 || y==8 || y==10 || y==12) && (z>0 && z<=31)
valid = true;
elseif (y==4 || y==6 || y==9 || y==11) && (z>0 && z<=30)
valid = true;
elseif (y==2 && (z>0 && z<=29))
valid = true;
else
valid = false;
end
elseif x>0 && a==0
if (y==1 || y==3 || y==5 || y==7 || y==8 || y==10 || y==12) && (z>0 && z<=31)
valid = true;
elseif (y==4 || y==6 || y==9 || y==11) && (z>0 && z<=30)
valid = true;
elseif (y==2 && (z>0 && z<=28))
valid = true;
else
valid = false;
end
end
2 comentarios
arjun gupta
el 11 de Oct. de 2022
Perfect :))
khaula
el 1 de En. de 2023
after trying all other codes, i found this one correct. thanks
Rik
el 13 de Jun. de 2019
1 voto
You can do this two ways:
Option 1 is to do the actual work. Is the input correct? Does the month entered actually have at least as many days as the day input (taking leap years into account)?
Or option 2: cheat by using the builtin functions to convert your input to a datetime scalar, and extract the year,month,day numbers from that. If those match the input, your date is valid. You should still put in a check if the inputs are positive scalars.
4 comentarios
Divya Nangaru Sudhakar
el 23 de Jun. de 2019
Editada: Rik
el 24 de Jun. de 2019
function valid = valid_date(year,month,day)
if (isinteger(year/4) || ~isinteger(year/4)) && ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && (day>0 && day<=31)
valid = true;
elseif (isinteger(year/4) || ~isinteger(year/4)) && ((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day>0 && day<=30)
valid = true;
elseif mod(year, 400) == 0 && month == 2 && (day>0 && day<=29)
valid = true;
elseif mod(year, 4) == 0 && mod(year, 100) ~= 0 && month == 2 && (day>0 && day<=29)
valid = true;
elseif ~(mod(year, 400) == 0) && month == 2 && (day>0 && day<=28)
valid = true;
elseif ~(mod(year, 4) == 0 && mod(year, 100) ~= 0) && month == 2 && (day>0 && day<=28)
valid = true;
else
valid = false;
end
i used the same code but its showing that
"Non-scalar
Variable valid has an incorrect value.
Return false if an input is not scalar..."
Can anyone please help me with the modification of the code.
Thank you
Rik
el 24 de Jun. de 2019
Why are you using neither my suggestion, nor James's?
Also, this is input checking, which you should do before any other processing. Read about the numel function
Divya Nangaru Sudhakar
el 24 de Jun. de 2019
I tried executing that function explained by James's, I dint get the output. Can you please help me with this function.
Thank you
Rik
el 24 de Jun. de 2019
James didn't provide a full function, because it is a homework exercise. If you want me to make your homework, first make sure I'll get the points from your teacher. (You can find guidelines for posting homework on this forum here.)
Did you read the documentation for numel? And how did you try to implement either my solution or the solution by James?
Categorías
Más información sobre Logical 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!