Prev: ICA
Next: snr
From: Nathan Christensen on
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!
From: Walter Roberson on
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?
From: Nathan Christensen on
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....
From: Nathan Christensen on
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

From: Walter Roberson on
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.
 |  Next  |  Last
Pages: 1 2
Prev: ICA
Next: snr