leap year m file help...

  • Follow


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:













7/27/2012 3:50:16 PM


Reply: