MATLAB Answers

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.

307 views (last 30 days)
Rakeshwar Elango
Rakeshwar Elango on 27 Jun 2019
Answered: AKSHAY K J ungefär 7 timmar ago
Hi Folks,
I have tried and ended up in errors. For example(valid_date(2018, 4, 1) failed,The last day of every month
Variable valid has an incorrect value. valid_date(2000, 1, 31) failed..., Random leap years
Variable valid has an incorrect value. valid_date(1624, 2, 29) failed..., Random dates
Variable valid has an incorrect value. valid_date(1433, 6, 28) failed...)
Kindly point out the errors I have made!
function valid = valid_date(year,month,day)
if (nargin==3)
if (fix(valid_date)&&isscalar(valid_date))
if((rem(year,4)==0||rem(year,400)==0) && rem(year,100)~=0)
if((month==1||3||5||7||9||11) && 0<day<32)
valid=true;
elseif(month==2 && 0<day<30)
valid=true;
elseif((month==4||6||8||10||12) && 0<day<31)
valid=true;
else
valid=false;
end
else
if((month==1||3||5||7||9||11) && 0<day<32)
valid=true;
elseif((month==4||6||8||10||12) && 0<day<31)
valid=true;
elseif(month==2 && 0<day<29)
valid=true;
else
valid=false;
end
end
else
valid=false;
end
else
valid=false;
end

  7 Comments

Show 4 older comments
Amina Sadia
Amina Sadia on 14 Jun 2020
function valid = valid_date(year,month,date)
a = rem(year,400);
b = rem(year,4);
c = rem(year,100);
%check if either year, month, day is not a scalar
if ~isscalar(year) || ~isscalar(month) || ~isscalar(date)
valid =false
return
end
%checking integer
e = rem(year,1)+rem(month,1)+rem(date,1)
if year > 0 && month > 0 && month <=12 && date > 0 && date <= 31 && e==0
if (month == 4 || month == 6 || month == 9 || month == 11) && date <=30
valid = true;
elseif (month == 1 || month == 3 ||month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && date <=31
valid = true;
elseif month == 2 && a == 0 && date <=29
valid = true;
elseif month == 2 && b == 0 && c ~= 0 && date <=29
valid = true;
elseif month == 2 && date <=28
valid = true;
else
valid = false;
end
else
valid = false;
end

Sign in to comment.

More Answers (4)

Thyagharajan K K
Thyagharajan K K on 2 Jun 2020
valid = valid_date(2019, 12, 1)
function valid = valid_date(year,month,day)
%check if either year, month, day is not a scalar
if ~isscalar(year) || ~isscalar(month) || ~isscalar(day)
valid =false
return
end
%check if either year, month, day is 0 or negative. check if month is >12
if year<=0 || month>12 || month <=0 ||day<=0
valid =false
return
end
A = [1 3 5 7 8 10 12]; %months with 31 days
B = [4 6 9 11]; %months with 30days
month_31d = sum(ismember(A,month));
%ismember() returns a logical array where element found will be marked as 1 & other positions will be 0
%so only one element in this logical array will be 1 and other elements will be 0 if month falls in this
%month_31d category. Hence sum will be 1 i.e month_31d will be true.
month_30d = sum(ismember(B,month)); %same as above to check whether month falls in the category month_30d
%check for leap year
if rem(year,400)==0
leap_year = true;
elseif rem(year,4)==0 && rem(year,100) ~=0
leap_year =true;
else
leap_year = false;
end
%Check the days for the conditions 28,29,30,31
if month_31d && day>31
valid = false;
elseif month_30d && day>30
valid = false;
elseif (leap_year && month==2 && day>29)||(~leap_year&&month==2&&day>28)
valid = false;
else
valid =true;
end
end

  1 Comment

ManiSandeep Reddy
ManiSandeep Reddy on 20 Jun 2020
function valid=valid_date(year,month,day)
if ~isscalar(year)||~isscalar(month)||~isscalar(day)
valid=false;
elseif year<0 || month<0 || day<0 || month>12 || day>31|| month==0 || day==0 || year ~= fix(year)||month ~= fix(month)||day ~= fix(day)
valid=false;
else
if month==2
if ((rem(year,4)==0 && rem(year,100)~=0)||rem(year,400)==0)
if day>29
valid=false;
else
valid=true;
end
else
if day>28
valid=false;
else
valid=true;
end
end
else
if (month==4||month==6||month==9||month==11) && day==31
valid=false;
else
valid=true;
end
end
end
end

Sign in to comment.


Amina Sadia
Amina Sadia on 14 Jun 2020
function valid = valid_date(year,month,date)
a = rem(year,400);
b = rem(year,4);
c = rem(year,100);
%check if either year, month, day is not a scalar
if ~isscalar(year) || ~isscalar(month) || ~isscalar(date)
valid =false
return
end
e = rem(year,1)+rem(month,1)+rem(date,1)
if year > 0 && month > 0 && month <=12 && date > 0 && date <= 31 && e==0
if (month == 4 || month == 6 || month == 9 || month == 11) && date <=30
valid = true;
elseif (month == 1 || month == 3 ||month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && date <=31
valid = true;
elseif month == 2 && a == 0 && date <=29
valid = true;
elseif month == 2 && b == 0 && c ~= 0 && date <=29
valid = true;
elseif month == 2 && date <=28
valid = true;
else
valid = false;
end
else
valid = false;
end

  0 Comments

Sign in to comment.


Purushottam Shrestha
Purushottam Shrestha on 29 Jul 2020 at 8:59
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

  0 Comments

Sign in to comment.


AKSHAY  K J
AKSHAY K J ungefär 7 timmar ago
function valid= valid_date(year,month,date)
if nargin~=3
valid=false;
elseif ~isscalar(year) | ~isscalar(month) | ~isscalar(date)
valid=false;
elseif year<1 || month<1 || date<1
valid=false;
elseif year~=fix(year) || month~=fix(month) || date~=fix(date)
valid=false;
elseif month>12 || date>31
valid=false;
elseif month==2
if (rem(year,400)==0 || (rem(year,4)==0 && rem(year,100)~=0)) && date<30
valid=true;
elseif (rem(year,400)~=0 || (rem(year,4)~=0 && rem(year,100)==0)) && date<29
valid=true;
else
valid=false;
end
else
days = [31 29 31 30 31 30 31 31 30 31 30 31];
totaldays = days(month);
if date>totaldays
valid=false;
else
valid=true;
end
end
end

  0 Comments

Sign in to comment.