Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab Assume that in some parallel universe Matlab exists but the leapyear func

ID: 3672841 • Letter: M

Question

Matlab

Assume that in some parallel universe Matlab exists but the leapyear function is missing. Your job is to create an equivalent function. Study the help for the Matlab built-in function called leapyear. Write your own version of the function and name it my_leap_year; make it compatible with the Matlab function. Include help in the function. Hint: the rules for determining leap years are more complex than just every fourth year. Put comments inside your function (not as part of the help) listing the rules and providing a reference for them.

Explanation / Answer

program:

function [leap]=my_leap_year(years)
[r,c]=size(years);
for i=1:1:c
if mod(years(i),400)==0 %the year which divisible by 400 is leap year
leap(i)=1;
elseif mod(years(i),100)==0 %the year which divisible by 100 is not a leap year
leap(i)=0;
elseif mod(years(i),4)==0 %the year which divisible by 4 is leap year
leap(i)=1;
else %the remaining all years are not leap years
leap(i)=0;
end
end
end

output:

my_leap_year([2000,2001,2002,2003,2004])

ans =

1 0 0 0 1