If Else problem Valid_date code.
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. Note that your solution must not contain any of the date related built-in MATLAB functions.
So ive seen other peoples code and ive been trying to get it on my own but to no luck fails all test but non-scalar and random non-leap years
function [valid] = valid_date(year, month, day);
leap_year = false;
valid = true;
if nargin ~= 3
valid = false;
else
end
if isscalar(year) && isscalar(month) && isscalar(day)
%%if isinteger(year) && isinteger(month) && isinteger(day)
if (year>=0) && (13>month>0) && (32>day>0)
%%fix(year), fix(month), fix(day)
if month == 2 && day == 29
leap_year = true;
elseif rem(year,400) == 0
leap_year = true;
elseif rem(year,4) == 0 && rem(year,100) ~= 0
leap_year = true;
else
leap_year = false;
end
if month ~= 1 || month ~= 3 || month ~= 5 || month ~= 7 || month ~= 9 month ~= 12 && day == 31
valid = false;
elseif month == 2 && day > 29
valid = false;
end
else
valid = false;
end
else
valid = false;
endWrite 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. Note that your solution must not contain any of the date related built-in MATLAB functions.
So ive seen other peoples code and ive been trying to get it on my own but to no luck fails all test but non-scalar and random non-leap years
function [valid] = valid_date(year, month, day);
leap_year = false;
valid = true;
if nargin ~= 3
valid = false;
else
end
if isscalar(year) && isscalar(month) && isscalar(day)
%%if isinteger(year) && isinteger(month) && isinteger(day)
if (year>=0) && (13>month>0) && (32>day>0)
%%fix(year), fix(month), fix(day)
if month == 2 && day == 29
leap_year = true;
elseif rem(year,400) == 0
leap_year = true;
elseif rem(year,4) == 0 && rem(year,100) ~= 0
leap_year = true;
else
leap_year = false;
end
if month ~= 1 || month ~= 3 || month ~= 5 || month ~= 7 || month ~= 9 month ~= 12 && day == 31
valid = false;
elseif month == 2 && day > 29
valid = false;
end
else
valid = false;
end
else
valid = false;
end 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. Note that your solution must not contain any of the date related built-in MATLAB functions.
So ive seen other peoples code and ive been trying to get it on my own but to no luck fails all test but non-scalar and random non-leap years
function [valid] = valid_date(year, month, day);
leap_year = false;
valid = true;
if nargin ~= 3
valid = false;
else
end
if isscalar(year) && isscalar(month) && isscalar(day)
%%if isinteger(year) && isinteger(month) && isinteger(day)
if (year>=0) && (13>month>0) && (32>day>0)
%%fix(year), fix(month), fix(day)
if month == 2 && day == 29
leap_year = true;
elseif rem(year,400) == 0
leap_year = true;
elseif rem(year,4) == 0 && rem(year,100) ~= 0
leap_year = true;
else
leap_year = false;
end
if month ~= 1 || month ~= 3 || month ~= 5 || month ~= 7 || month ~= 9 month ~= 12 && day == 31
valid = false;
elseif month == 2 && day > 29
valid = false;
end
else
valid = false;
end
else
valid = false;
end if statement MATLAB Answers — New Questions