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

unction valid=valid_date(year,month,date)
if nargin==3
valid1=true;
else valid=false;
return
end
v1=[year]; v2=[month]; v3=[date];
if isscalar(v1)==true && isscalar(v2)==true && isscalar(v3)==true
valid2=true;
else valid=false;
return
end
if year>0 && 0<month && month<=12 && 0<date && date<=31
valid3=true;
else valid=false;
return
end
a=year/4; b=year/100; c=year/400;
if rem(year,4)==0 && rem(year,100)~=0
valid4=true;
else valid4=false;
end
if rem(year,100)==0 && rem(year,400)~=0
valid5=true;
else valid5=false;
end
if rem(year,400)==0
valid4=true;
else valid4=false;
end
if (month==1||3||5||7||8||10||12 && date<=31) || (month==2 && date<=29) || (month==4||6||9||11 && date<=30) && valid4==true && valid5==false
valid6=true;
else valid6=false;
end
if (month==1||3||5||7||8||10||12 && date<=31) || (month==2 && date<=28) || (month==4||6||9||11 && date<=30) && valid5==true && valid4==false
valid7=true;
else
valid7=false;
end
if valid1==true && valid2==true && valid3==true && valid6==true
valid=true;
elseif valid1==true &&valid2==true && valid3==true && valid7==true
valid=true;
else
valid=false;
return
end
why this code is not working for 2018/4/31 and 2003/2/29? and other random dates. but is works for non scalar and random leap years

8 commentaires

function isvalid = valid_date(y, m, d)
% Check if the inputs are valid
% Check that they are scalars
if ~(isscalar(y) && isscalar(m) && isscalar(d)) isvalid = false;
% Check that inputs are positive elseif ~all([y, m, d] > 0) isvalid = false;
% Check that inputs are integers (not the data type) elseif any(rem([y, m, d], 1)) isvalid = false;
% Check that m and d are below the max possible elseif (m > 12) || (d > 31) isvalid = false;
% The inputs could be a valid date, let's see if they actually are else
% Vector of the number of days for each month daysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
% If leap year, change days in Feb if isequal(rem(y, 4), 0) && (~isequal(rem(y, 100), 0) || isequal(rem(y, 400), 0)) daysInMonth(2) = 29;
end
maxDay = daysInMonth(m);
if d > maxDay isvalid = false;
else isvalid = true;
end
end
end
Julian,
before posting, please use the MATLAB editor to "smart indent". Then when you post, first click on the '>' button in the CODE toolbar section of the Answers editor, and then paste the code into the highlight box that appears: that will cause the code to be posted in formatted form.
function valid = valid_date(year, month, day)
if ~isscalar(year) || ~isscalar(month) || ~isscalar(day) || year ~=fix(year) || month ~=fix(month) || day ~=fix(day)
valid = false;
return;
end
if month <= 0 || month >= 13 || day <= 0 || year <=0
valid = false;
return;
end
ma =0;
if month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12
ma = 1;
elseif month == 2
ma = 2;
elseif month == 4 || month == 6 || month == 9 || month == 11
ma= 3;
end
if ma == 1
if day >= 32
valid = false;
else
valid = true;
end
end
if ma == 2
if mod( year , 4 )
if day >= 29
valid = false;
else
valid = true;
end
end
if ~mod( year , 4 )
if ~mod(year , 100)
if ~mod(year, 400)
if day >= 30
valid = false;
else
valid = true;
end
else
if day >= 29
valid = false;
else
valid = true;
end
end
else
if day >= 30
valid = false;
else
valid = true;
end
end
end
end
if ma == 3
if day >= 31
valid = false;
else
valid = true;
end
end
function [valid]=valid_date(year,month,day)
leap_year = false; % We set the leap year as false
if nargin == 3
% Year prerequisites:
if ~isscalar(year) || year ~= fix(year) || year < 0, valid=false; %error('Invalid input as a year');
else
% leap year prerequisites:
if rem(year,4) == 0
if rem(year,400) == 0, leap_year=true;
else
if rem(year,100) == 0, leap_year=false;
else leap_year= true;
end
end
end
% Month prerequisites:
if ~isscalar(month) || month ~= fix(month) || month < 0 || month > 12, valid = false; %error('Invalid input as a month');
else
% Day prerequisites:
if ~isscalar(day) || day ~= fix(day) || day < 0 || day > 31, valid = false; %error('Invalid input as a day');
else
% months with odd (i.e.31) number of days:
if month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12 && day <= 31, valid = true;
else,valid = false; end
% months with even (i.e.30)number of days:
if month == 4 || month == 6 || month == 9 || month == 11 && day <= 30; valid = true;
else, valid = false; end
% February -special- Prerequisites:
if month == 2 && day > 28 && leap_year == false, valid = false; %error('February has maximum 28 days');
elseif month == 2 && day > 29 && leap_year == true, valid = false; %error('February, for a leap year has at maximum 29 days');
else,valid = true; end
end
end
end
else
valid = false; %error('The number of input arguement has to be 3 and be with this order: "year","month","day"');
end
end
The code is running ok, but when it comes to test:
valid_date(2007, 4, 31) fails to comply :/ i am stuck for hours :(((
Did you use the debugger to run your code line by line for this input?
Tbh, no you are right i have to get more familiar with the debugging. However, i followed sb's else instructions and i managed to solve the assignment 1 hr later. I think i got it what i did wrong

Réponses (11)

function [valid]=valid_date(year,month,date)
y=year;m=month;d=date;
if ~isscalar(y)|| ~isscalar(m) || ~isscalar(d)
valid=false;
return
end
if y>=1 && m==1 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
elseif y>=1 && m==3 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==4 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==5 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==6 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==7 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==8 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==9 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==10 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==11 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==12 && d>=1 && d<=31
valid=true;
else
valid=false;
end
end

15 commentaires

when y mod 4 is nonzero then y mod 100 cannot be 0, since 100 is 4*25
Thanks for bringing it to my notice,now it's been resolved.
I would point out to you that if you were to use
if y < 1 | m < 1 | d < 1 | m > 12 | d > 31
valid = false;
elseif .....
end
then you would not have to keep rechecking y>=1 & d>=1 and that you would be able to skip the d<=31 on months that have 31 days
I would also point out that
if m == 1 || m == 3 || m == 5 (and so on)
handles a lot of cases all in one place without needing to test month by month.
That's a very useful suggestion. Thanks a lot.
You didn't write code for "integer" input. It will show any fraction date as valid.
I am getting incorrect answer only for the last day of every month. What changes needs to be done in the above syntax? Anyone?
JIGNESH PATIL: what code are you using?
nice ....this was simple of all... but lengthy
function [valid] = valid_date(y, m, d)
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d)
valid = false
return
end
if y>=1 && ismember(m,[1 3 5 7 8 10 12]) && d>=1 && d<=31
valid=true;
elseif y>=1 && ismember(m,[4 6 9 11]) && d>=1 && d<=30
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
else valid = false
return
end
end
Notice that if you were to use
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d) || y < 1
then you would not have to keep checking y>=1
function valid = valid_date(year,month,day)
if isscalar(year) && isscalar(month) && isscalar(day)
valid = year>0 && ((12>=month) && (month>0)) && ((31>=day) && (day>0)) && year==fix(year) && month==fix(month) && day==fix(day) && (((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && day<=31) || ((month==4 || month==6 || month==9 || month==11) && day<=30) || (((((rem(year,4)==0 && rem(year,100)~=0) || (rem(year,400)==0)) && day<=29) || (day<=28)) && month==2));
else
valid=false;
end
What about this code!!
Hello, Can you explain to me what you did in the code?
if i take (0<month<=12) in place of ((12>=month) && (month>0)) why am i getting mistake
Because that is not how Matlab operations work. The explanation provided by mlint explains it:
Would this function pass?
function [ valid, dt ] = valid_date( y, m, d )
% The names of the input arguments, (year,month,day), are they
% mandatory? Are these names chosen to shadow functions in the
% finance toolbox?
try
% datetime is "smart". Doc says: "Each element of DateVector
% should be a positive or negative integer value [...]. If an
% element falls outside the conventional range, datetime adjusts
% both that date vector element and the previous element."
dt = datetime( y, m, d );
% If datetime didn't adjust any element the input is a
% valid date.
% valid = all( [ year(dt)==y, month(dt)==m, day(dt)==d ] );
vec = datevec( dt );
valid = all( [ vec(1)==y, vec(2)==m, vec(3)==d ] );
catch
% datetime throws an exception when not all input values are
% integers
dt = datetime.empty;
valid = false;
end
end

1 commentaire

I would hope so. As an instructor I would applaud lateral thinking like this. Just as I would accept code where someone did this:
for n=1%use a loop because it is a requirement
output=sum(data,2);
end
function valid=valid_date(year, month, day)
if year<=0 || month<=0 || day<=0 || mod( year , 1 )~=0 || mod( month , 1 )~=0 || mod( day , 1 )~=0
valid=false;
return
end
if month<=12 && (ismember(month, [4 6 9 11]) && ismember(day, [1:30]))
valid=true;
elseif month<=12 && (ismember(month, [1 3 5 7 8 10 12]) && ismember(day, 1:31))
valid=true;
elseif month==2 && (mod(year,4)==0 && mod(year,100)~=0 || mod(year,400)==0 && mod(year,100)==0) && ismember(day, 1:29)
valid=true;
elseif month==2 && ismember(day, 1:28)
valid=true;
else
valid=false;
return
end
end
it works in Matlab, when I test a non-scalar inputs, it return false. But here it doesn't work!!!!! ((Assessment 2))
What is the problem?

4 commentaires

It doesn't return false, it returns an error:
valid_date([2020 2020], 11, 5)
Then you should change your code.
If you think this comment is unhelpful: show the code you used to ensure the output is false for non-scalar inputs. That is the only way someone will be able to help you.
looks like you didnt verifyng the input is scalar or not
function x = valid_date(year,month,day)
if nargin == 3
if isscalar(year) && isscalar(month) && isscalar(day)
if month > 0 && month <= 12 && day >= 0 && year > 0
if (month == 1 || 3 || 5 || 7 || 8 || 10 || 12 && day <= 31)...
|| (month == 2 && day <= 29) && (month == 4 || 6 || 9 || 11 && day <= 30)
c0 = mod(year,4)
c1 = mod(year,100)
c2 = mod(year,400)
if (c0 == 0 && c1 ~=0) || (c1 == 0 && c2 ~= 0) || c2 == 0
x = 'true'
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
end
why this code not working? :(

1 commentaire

Let's try some cases that will likely point us to an issue:
valid_date(2020,2,29) %returns true
valid_date(2021,2,29) %returns false
valid_date(2021,2,28) %returns false
That last one is a problem. Can you follow the flow of your code where it should mark Feb 29 as valid only in leap years, and the rest of Feb as valid every year?
Also, your code returns the value as a char array, not a logical.
Why is my code not working?

1 commentaire

Because you didn't use the debugging tools to run your code line by line.
I did spend whole noon to make it work. Even though it is quite long, hope it is useful for someone.
function valid = valid_date(year, month, day)
if ~isscalar(year) || year ~= fix(year)||year<=0
valid = false;
elseif ~isscalar(month) || month >12 || month<=0 || month ~= fix(month)
valid = false;
elseif ~isscalar(day) || day >31 || day ~= fix(day)|| day<=0
valid = false;
elseif day >29 && month == 2 && rem(year,4) == 0 && rem(year,100)~=0 %leap year
valid = false;
elseif day>29 && month == 2 && rem(year,400) == 0 %leap year
valid =false;
elseif day>30 && (month == 4 ||month == 6||month == 9 ||month == 11)
valid =false;
elseif day>28 && month==2 && rem(year,100) == 0 && rem(year,4)==0 && rem(year,400)~=0 %non leap year
valid =false;
elseif day>28 && month==2 && rem(year,4)~=0 && rem(year,400)~=0 %non leap year
valid =false;
else
valid = true;
end
end

2 commentaires

It is a good exercise to write a function like this, but why did you post this? What does it add to the previous answers?
I simply think that there are a lot of ways to solve this code. Just want to share my view. Is there any problem? btw
function valid = valid_date(year, month, day)
if ~isscalar(year) || ~isscalar(month) || ~isscalar(day) || year ~=fix(year) || month ~=fix(month) || day ~=fix(day)
valid = false;
return;
end
if month <= 0 || month >= 13 || day <= 0 || year <=0
valid = false;
return;
end
ma =0;
if month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12
ma = 1;
elseif month == 2
ma = 2;
elseif month == 4 || month == 6 || month == 9 || month == 11
ma= 3;
end
if ma == 1
if day >= 32
valid = false;
else
valid = true;
end
end
if ma == 2
if mod( year , 4 )
if day >= 29
valid = false;
else
valid = true;
end
end
if ~mod( year , 4 )
if ~mod(year , 100)
if ~mod(year, 400)
if day >= 30
valid = false;
else
valid = true;
end
else
if day >= 29
valid = false;
else
valid = true;
end
end
else
if day >= 30
valid = false;
else
valid = true;
end
end
end
end
if ma == 3
if day >= 31
valid = false;
else
valid = true;
end
end

3 commentaires

Why did you post this? What does it teach? Why should it not be deleted?
It is just Another way to solve same problem, As I have used pretty basic coding that's why it is big. It can be optimized.
Big code is not a problem. I think undocumented code is a problem if you want to teach people something. You don't explain what your code is doing. Your choice of variable names also doesn't help: why use ma if you can use something more descriptive like MonthType? I know you are determining if a year is a leap year, but you don't explain that. If someone is inexperienced enough to scroll down all the way to your answer, you can't assume that would be clear to them.
If you are posting a complete solution to a homework question, at least try to teach something, instead of only providing the opportunity for cheating.
function isvalid = valid_date(y, m, d)
% Check if the inputs are valid
% Check that they are scalars
if ~(isscalar(y) && isscalar(m) && isscalar(d))
isvalid = false;
% Check that inputs are positive
elseif ~all([y, m, d] > 0)
isvalid = false;
% Check that inputs are integers (not the data type)
elseif any(rem([y, m, d], 1))
isvalid = false;
% Check that m and d are below the max possible
elseif (m > 12) || (d > 31)
isvalid = false;
% The inputs could be a valid date, let's see if they actually are
else
% Vector of the number of days for each month
daysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
% If leap year, change days in Feb
if isequal(rem(y, 4), 0) && (~isequal(rem(y, 100), 0) || isequal(rem(y, 400), 0))
daysInMonth(2) = 29;
end
maxDay = daysInMonth(m);
if d > maxDay
isvalid = false;
else
isvalid = true;
end
end
end
function valid = valid_date(year,month,date)
y=year;m=month;d=date;
if ~isscalar(y)|| ~isscalar(m) || ~isscalar(d)
valid=false;
return
end
if y>=1 && m==1 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
elseif m<=12 && (ismember(m, [4 6 9 11]) && ismember(d, [1:30]))
valid=true;
elseif m<=12 && (ismember(m, [1 3 5 7 8 10 12]) && ismember(d, 1:31))
valid=true;
else
valid = false
end
% This is the shortest and oring code for all the valid date i could write
% date formaat should be in lie vald_date(year,month,date) to mae the function work

1 commentaire

Shorter code is possible with a different strategy, see e.g. this answer.
My opinion would be that the shortest code is not always the best code. The best code in my opinion would be maintainable (so well-commented), as well as fast.
Why would this code not pass the scalar test? %Return false if the input is not scalar

3 commentaires

if d==fix(d)&&e~=fix(e)
Suppose d is a vector. Then d==fix(d) would result in a vector the same size. Then you would have the && operation. But the && operation can only be used when both sides evaluate to scalars (exception: if the left side evalutes to scalar false, then with && the right side is ignored.)
oh so tthe isscalar can't be used in tthat whole bunch of &&? So can i just write a seperate if statements with all tthe ~isscalars att the front, I think that'll work right?
You need to have the isscalar() check before the other checks.
if ~all(iscalar(a) && isscalar(b) && isscalar(c))
do whatever appropriate for error
end
function valid=valid_date(y,m,d)
k=y/4;
j=y/400;
i=y/100;
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d) || y~=fix(y) || m~=fix(m) || d~=fix(d)
valid=false;
elseif (k==fix(k) || j==fix(j)) && m==2 && d<=29 && i~=fix(i) && d>0
valid=true;
elseif j==fix(j) && m==2 && d<=29 && d>0
valid=true;
elseif (ismember(m, [1 3 5 7 8 10 12])) && d<=31 && d>0
valid=true;
elseif (ismember(m, [4 6 9 11])) && d<=30 && d>0
valid=true;
elseif m==2 && d<=28 && i==fix(i) && d>0
valid=true;
elseif m==2 && d<=28 && i~=fix(i) && d>0
valid=true;
else
valid=false;
end

Cette question est clôturée.

Clôturé :

le 12 Avr 2021

Community Treasure Hunt

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

Start Hunting!

Translated by