Create function to calculate leap year
49 次查看(过去 30 天)
显示 更早的评论
Hi I need to create a function that determines a leap year with the conditions
function result = is_leap_year(year)
% IS_LEAP_YEAR Determine if a given year is a leap year
% RESULT = IS_LEAP_YEAR(YEAR) returns TRUE if YEAR is a leap year, and
% FALSE otherwise.
% An error is raised if YEAR is not a positive integer.
a year is a leap year if it is divisible by four, unless it is also divisible by 100, unless it is also divisible by 400. I have been able to create a function however cannot work out how to do the false otherwise statement. Also with true and false I get 1(true) and 0 (false). Is this acceptable? Here's my function
function [ status ] = leapyear( year )
if mod(year, 400) == 0
status = true;
elseif mod(year, 4) == 0 && mod(year, 100) ~= 0
status = true;
else
status = false;
end
end
5 个评论
chantsal bymba
2020-11-7
How to describe it
(Write a script which calls your function with a range from [1952 to 2020] and displays the output.)
Walter Roberson
2020-11-8
years = 1795:1810;
for yidx = 1 : length(years)
result(yidx) = leapyear(years(yidx));
end
回答(8 个)
Fangjun Jiang
2011-8-31
TF=~(datenum(year,2,29)==datenum(year,3,1))
Using your approach, it should be like below. This is primarily for learning the if-else statement using only the necessary condition checking.
function [ status ] = leapyear( year )
if mod(year, 4) == 0
status = true;
if mod(year, 100) == 0
status=false;
if mod(year,400)==0
status = true;
end
end
else
status=false;
end
true and false are displayed as 1 and 0, but they are different than the double precision number 1 and 0. They are logical. Type this to find out.
class(true)
class(false)
class(1)
class(0)
To make it work for vector, I recommend Andrei's approach.
>> ly=@(yr)~rem(yr,400)|rem(yr,100)&~rem(yr,4);
>> ly(1999:2005)
ans =
0 1 0 0 0 1 0
4 个评论
Walter Roberson
2011-8-31
Renaming variables doesn't count in golfing, not until you need more than 26 variables (in which case it would be fair game to code so that the variables used most frequently had the shorter names.)
2 digit year numbers are considered to be within the current century in string representations, not in numeric representations.
The terms of the original assignment called for the function to error if the year was not a positive integer; your code does not make that test. Your code will return 1 (true) for an input of 0 -- but of course there was no year 0 so it could not have been a leap year.
Rebecca
2011-8-31
Hi - @Jack, I have the same assignment (we might have class together)
I think I must have some settings weird, when I type
function [ status ] = leapyear (year)
Error: Function definitions are not permitted in this context.
...and would always error when year = 23.
Error using ==> year at 23
Please enter D.
I figured it seemed to think year was to do with the date and clock so I substituted my own word for practise (star, beep, whatever), but then it wanted me to define this word before I started my input in the first place. I know you guys are down to the core pieces of the assignment, but I could use some baby step help.
I'm also pretty sure we're meant to use the rem function, but from all the research I've done so far, mod seems to work similarly - if not better, anyone want to explain?
4 个评论
Walter Roberson
2011-8-31
function [ status ] = leapyear (year)
and the rest of your code that implements the test for any one year should go in leapyear.m
The parts of the file that you had _before_ the "function" line should stay where they were... unless, that is, your original file happened to be named leapyear.m in which case you need to choose a different name for the parts before the "function" line.
Rebecca
2011-9-1
fantastic! - I had a friend help and learnt the difference between the scrip screen etc. (thank you!)
I added an extra line in to the above to make sure that year was an integer:
if year <=0 || rem(year,1) ~=0
error('either not positive or non integer')
end
(next I'll learn how to make it look light grey and code for this site)
Walter Roberson
2011-8-31
http://www.nmm.ac.uk/explore/astronomy-and-time/time-facts/leap-years and http://en.wikipedia.org/wiki/Gregorian_calendar
Julian Calendar established 46 BC, but leap years not properly added up through 7 CE
8 CE to 1581 CE - every year divisible by 4 was a leap year; the Julian Calendar.
1582 CE - Gregorian Calendar decreed, with the 4 except 100 except-except 400 structure. Some countries adopt this in October 1582 CE
1582 CE onward - various countries adopt at different times. Some of the changes were strange; some areas went back to the Julian calendar
1752 - Britain and colonies adopt Gregorian Calendar in September 1752 CE
1752 CE onward - more changes in various countries
1912 CE - 1928 CE: Republic of China is officially Gregorian but in practice local rules are followed
1918 CE Russia becomes second last major country to change. The "October Revolution" of 1917 was October in Julian calendar but November in Gregorian
1926 CE Turkey becomes last western country to change.
1929 CE Republic of China goes officially Gregorian again
The next 700 years: arguments from the Orthodox Christian churches as they have not adopted the Gregorian Calendar for some purposes; it will next make a difference in 2800 CE...
0 个评论
Fahad alshammari
2018-10-21
编辑:Walter Roberson
2019-6-11
function year
year = input (' Enter the year : ')
if mod(year,4) == 0 disp ('true')
elseif mod(year,100) == 0 disp ('falus')
elseif mod(year,400) == 0 disp ('true')
else disp ('falus')
end end
3 个评论
Walter Roberson
2018-10-21
Reverse the order of your tests. Multiple of 400 is a leap year, multiple of 100 is not a leap year unless you already passed the 400 test, multiple of 4 is a leap year unless you already failed the 100 test.
Shubham Agrawal
2019-6-11
a=input('Enter the year');
if (((a/400-floor(a/400))==0) | (((a/4-floor(a/4))==0)&(a/100-floor(a/100)~=0)))==1
disp('This is a leap year');
else
disp('This is not a leap year');
end
0 个评论
Shanjul Shrivastava
2020-6-11
编辑:Shanjul Shrivastava
2020-6-11
function result = is_leap_year(year)
if year<=0; error("year is not a positive integer");
elseif(mod(year,4))~=0 | (mod(year,4)==0 & mod(year,100)==0 & mod(year,400)~=0)
result="false";
else
result="true";
end
end
4 个评论
Walter Roberson
2020-6-11
The string() object "true" is not the same thing as TRUE, which is a logical value.
Also, there are no circumstances under which the code is expected to error(). Years before 46 BCE are not leap years. See the list at https://www.mathworks.com/matlabcentral/answers/14859-create-function-to-calculate-leap-year#answer_20236
Dongchi Jiang
2022-2-17
编辑:Dongchi Jiang
2022-2-17
clc;
year=input('Enter a year: \n');
if (rem(year,400)==0 | rem(year,4)==0 & rem(year,100)~=0)
disp('The year is a leap year')
else
disp('The year is not a leap year')
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!