im looking at writing a file to show weather or not a specific year is a leap year or not. so far my attempts have been in vain.
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.
obviously the use of if and else statements are needed. The rem function is also needed.
here is the framework to work around.
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.
thanks for any help!
|
|
0
|
|
|
|
Reply
|
Nathan
|
8/13/2010 3:22:24 AM |
|
Nathan Christensen wrote:
> im looking at writing a file to show weather or not a specific year is a
> leap year or not. so far my attempts have been in vain.
OK.
> 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.
Historically inaccurate, but acceptable to current Western timekeeping;
completely prohibited by the Muslim calendar for example.
> obviously the use of if and else statements are needed.
Not true: there are ways to do it without those.
> The rem function is also needed.
Not true either: there are ways to do without it.
> here is the framework to work around.
>
> 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.
>
> thanks for any help!
What is your specific question? Are you having difficulty finding the
documentation for the "rem" function, or having difficulty in finding
the "getting started" portion of the documentation that illustrates how
to set up elementary if statements?
|
|
0
|
|
|
|
Reply
|
Walter
|
8/13/2010 3:41:26 AM
|
|
thanks for the reply walter,
i need to write an m file using the framework given, that determinues if a year is a leap year or not, using the conditions given (divisible by 4, 100, 400 etc), that utilises the rem function to do so....
|
|
0
|
|
|
|
Reply
|
Nathan
|
8/13/2010 4:35:05 AM
|
|
this is my current m file,
however when used an error returns relating to the use of the second else statement...
function result = is_leap_year(x)
% 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.
if rem (x,4) > 0;
disp 'is not a leap year'
else rem (x,100) >0;
disp 'is a leap year'
else rem(x,400) > 0;
disp 'is not a leap year'
end
|
|
0
|
|
|
|
Reply
|
Nathan
|
8/13/2010 4:56:04 AM
|
|
Nathan Christensen wrote:
> this is my current m file,
>
> however when used an error returns relating to the use of the second
> else statement...
>
> function result = is_leap_year(x)
> % 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.
>
> if rem (x,4) > 0;
> disp 'is not a leap year'
> else rem (x,100) >0;
> disp 'is a leap year'
> else rem(x,400) > 0;
> disp 'is not a leap year'
> end
>
The syntax is
if condition
true statement list
else
false statement list
end
So your code would be interpreted as
condition is "rem(x,4) > 0"
true statement list is "disp 'is not a leap year'"
false statement list is "rem (x,100) > 0; else rem(x,400) > 0; disp 'is
not a leap year'"
The first part of that, rem(x,100) > 0; is a perfectly valid calculation
that returns a logical result that happens to get thrown away.
The second part of that is an "else" statement that has no matching "if"
statement, so you have a syntax error.
I suggest you go back to the documentation of "if" and look at the
"elseif" clause.
By the way, notice that your code never calculates a true or false value
like it is supposed to.
|
|
0
|
|
|
|
Reply
|
Walter
|
8/13/2010 5:07:32 AM
|
|
Dear Nathan,
Of course, trying to create an own function is really helpful for learning Matlab. On the other hand you can look how others have implemented this: Go to the FileExchange pages and search for the term "leap":
http://www.mathworks.com/matlabcentral/fileexchange
Good luck, Jan
|
|
0
|
|
|
|
Reply
|
Jan
|
8/13/2010 2:33:04 PM
|
|
This should help:
function result = is_leap_year(x)
if rem (x,400)==0
disp 'is a leap year'
elseif rem (x,100)==0
disp 'is not a leap year'
elseif rem(x,4)==0;
disp 'is a leap year'
else
disp 'is not a leap year'
end
|
|
0
|
|
|
|
Reply
|
nahin (1)
|
11/23/2011 8:53:09 AM
|
|
See what I did with strtok(). I removed the data and month in the date leaving the year. The year was checked with modular. It can as well be checked with the boolean islaepyear()
++++++++++++++++++++++++++++++++++++
[day, rest] = strtok(date, '-'); %removes the date
[ny1, ny2] = strtok(rest, '-'); %removes the month leaving the year with separator
ny3 = strtok(ny2, '-'); %removes the separator leaving year only
if ny3 % 4 == 0
yrs = 366; %assigns 366 days to leap-year
else
yrs = 365; %The regular 365days of a year
end
++++++++++++++++++++++++++++++++++++
"Nathan Christensen" wrote in message <i42dpg$17j$1@fred.mathworks.com>...
> im looking at writing a file to show weather or not a specific year is a leap year or not. so far my attempts have been in vain.
>
> 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.
>
> obviously the use of if and else statements are needed. The rem function is also needed.
>
> here is the framework to work around.
>
> 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.
>
> thanks for any help!
|
|
0
|
|
|
|
Reply
|
dadaikhuomoregbe (1)
|
6/9/2012 6:10:08 PM
|
|
|
7 Replies
421 Views
(page loaded in 0.113 seconds)
Similiar Articles: basic questions about the leapsecond - comp.protocols.time.ntp ...... is to install the NIST leap >second file. See: >http://support ... in order to evaluate the leap second file. >Unless I'm ... in error. >> How about a leap minute every 240 years ... Convert UTC seconds to actual time. - comp.lang.c... The allowed syntax for file names in ... too: Some machines lack hardware support for integer division, so `m ... are (365 * 4) + 1 = 1461 days in a leap year > > I'm ... Ruh, roh. Leap Second? - comp.protocols.time.ntp> To help > spot feckless fingers, recent versions have a protostats file ... if explicitely configured in ntp.conf, similar to the other stats files. Since such faulty leap ... seconds to YYYYMMDDHHMMSS - comp.unix.programmer... e., i do need to develop my own. May some one help me? ... 60 T["m"] += 3 if (T["m"] > 12) { T["Y"]++; T["m"] -= 12; T["j"] -= 365 } if (T["m"] > 2 && is_leap_year(T ... On converting modified julian date (MJD) to decimal year - comp ...Any ideas on how to do this and account for leap years/days/hours? I appreciate any help that can be ... soft-sys.matlab On converting modified ... Especially if files Question about 2008 leap second - comp.protocols.time.ntp ...... all I am writing an article about the "leap second" of this year. ... formal bug report is in order; with log files ... comp.protocols.time.ntp ..... does not support leap ... Leap Second - comp.protocols.time.ntp... 24th century once we need more than 4 leap sec a year ... As far as I know, the Windows kernel does not support leap ... The NIST leap second file does not only >contain ... ntpd and leap seconds on Windows - comp.protocols.time.ntp ...As far as I know, the Windows kernel does not support leap seconds. Does the Windows port ... an upstream server - from a reference clock - from the NIST leap seconds file ... AWK question - split string into variables - comp.unix.shell ...Thanks for the help and advice... Scott ... for specific day maxs for specific months and leap years if ... awk script to split text file content to multiple ... Cronjob on the last sunday of every month - comp.unix.admin ...Need > > some help. > > man (1) crontab The only way I know how to do this is to ... week of each month as it's variable, depending on the month and if it's a leap year. leap year m file help... - Newsreader - MATLAB Centralthanks for the reply walter, i need to write an m file using the framework given, that determinues if a year is a leap year or not, using the conditions given ... Leap year - Wikipedia, the free encyclopediaPseudocode to determine whether a year is a leap year or not in ... Help; About Wikipedia; Community portal; Recent changes ... Upload file; Special pages; Permanent link; Cite this page 7/27/2012 3:50:16 PM
|