MatLab multiple functions question
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Jan
on 24 Jan 2021
The instructions ask for: "The output argument should be a Boolean variable". Your function IsLeapYear does not reply anything yet. This can be done by:
function T = IsLeapYear(Year)
T = ~mod(Year, 4) & (mod(Year, 100) | ~mod(Year, 400))
end
In the 2nd function think twice if this is correct:
m = endYear - startYear;
If endYear equals startYear, m is 0, but it should be 1.
You create v already, but do not use it, but it is a good solution:
for c = 1:m
b(c) = IsLeapYear(v(c));
end
Now b is a logical vector, which is TRUE for the leap years. You can use it directly to reply the leap years only:
outputArg1 = v(b);
2 Comments
Jan
on 24 Jan 2021
The 2nd function GetLeapYears should reply a list of leap years. b is a logical vector of the same size as v=startYear:endYear . Then v(b) is the vector of leap years. This is the "logical indexing".
Another option:
function LeapYears = GetLeapYears(startYear, endYear)
LeapYears = [];
for Year = startYear:endYear
if IsLeapYear(Year)
LeapYears(end + 1) = Year;
end
end
end
For small problems this is sufficient, but for real applications letting an array grow iteratively wastes too many ressources.
More Answers (0)
See Also
Categories
Find more on Unit Conversions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!