276 views (last 30 days)

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)

James Tursa
on 14 Jun 2019

Edited: James Tursa
on 14 Jun 2019

All of those if-elseif blocks make the code difficult to read, and difficult to debug as well. I would advise against that approach, and instead tackle each issue one at a time and code only for that issue. That makes the logic of each test much easier to read and to debug. For example, here is an outline of what the code could do using words:

function valid = valid_date(year,month,day)

valid = false; % Set a default return value

% Check for positive integer scalar inputs

if( the year is not a positive integer scalar )

return;

end

if( the month is not a positive integer scalar )

return;

end

if( the day is not a positive integer scalar )

return;

end

% Check for proper month

if( the month is not between 1 and 12 inclusive )

return;

end

% Construct an array of the number of days in each month

days_in_month = a 12-element array of the number of days in each month;

% Check to see if this is a leap year

is_leap_year = (you put some code here to determine if the year is a leap year)

% If it is a leap year, change February number of days to 29

if( is_leap_year )

Change the days_in_month value for February to 29

end

% Check to see if the day number is valid

if( the day is greater than the number of days in the month )

return;

end

% Passed all of our checks, so the date must be valid

valid = true;

return;

end

Once you are satisfied that the words do what you want (you should check this yourself ... maybe I missed something that needs to be checked), then you need to write actual code for all of the worded places. The code you write for this outline will in many places be pieces of the code you have already written in your if-elseif blocks above.

Ashwin Raju
on 27 Feb 2020

Rik
on 7 Jun 2020

Salman P H
on 28 Apr 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

Muthu Dhanush Santh Nagarajan
on 9 Apr 2020

Edited: Rik
on 9 Apr 2020

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

Rik
on 9 Apr 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.

Rik
on 13 Jun 2019

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.

Rik
on 24 Jun 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
on 24 Jun 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
on 24 Jun 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?

MOHD FARHAN
on 7 Jun 2020

Can anyone please give me the correct and accepted ans of this question please

Rik
on 7 Jun 2020

Why should other people do your homework?

Also, why did you post this in the answer section?

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466971-if-else-problem-for-year#comment_714499

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466971-if-else-problem-for-year#comment_714499

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466971-if-else-problem-for-year#comment_714643

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466971-if-else-problem-for-year#comment_714643

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466971-if-else-problem-for-year#comment_714678

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466971-if-else-problem-for-year#comment_714678

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466971-if-else-problem-for-year#comment_714825

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466971-if-else-problem-for-year#comment_714825

Sign in to comment.