MatLab multiple functions question
显示 更早的评论
Write two functions. The first function, with its file name IsLeapYear.m, accepts a year (as a numerical input argument) and determines whether the year is a leap year. The output argument should be a Boolean variable, which should be true if the year is a leap year and false otherwise. The rules for determining leap years in the Gregorian calendar are: • All years evenly divisible by 400 are leap years. • Years evenly divisible by 100 but not by 400 are not leap years. • Years divisible by 4 but not by 100 are leap years. • All other years are not leap years.
The second function, with its file name GetLeapYears.m, generates and outputs a list of leap years. This function should have two input arguments startYear and endYear, which are used to form a sequence of years. For example, if the startYear is 2020, and the endYear is 2120, then the sequence of years would be 2020:2120 (one dimensional array). Use a loop to go through all these years to check if a certain year is a leap year using the IsLeapYear function you have written. This function should have one output argument outputting a list of leap years selected from the array of years between startYear and endYear
This a question I was given and I have written some code. My code doesn't work, and Im just trying to figure out what im missing in the code, if anyone could help direct me?
My two function scripts are here!
function IsLeapYear(Year)
%UNTITLED11 Summary of this function goes here
% Detailed explanation goes here
if rem(Year,400) == 0
TOF=1;
elseif rem(Year,4)== 0 && rem(Year,100)~= 0
TOF=1;
elseif rem(Year,100) == 0 && rem(Year,400)~=0
TOF=0;
else
TOF=0;
end
if TOF == 1
disp('True')
elseif TOF == 0
disp('False')
end
----------------------------------------------------------------------------------------------------------------------------------------
function [outputArg1] = GetLeapYears(startYear,endYear)
%UNTITLED12 Summary of this function goes here
% Detailed explanation goes here
m = endYear - startYear;
v = [startYear:1:endYear];
b = zeros(1,m);
for c = 1:m
for a = startYear:endYear
b(c) = IsLeapYear(a);
end
end
end
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!