I have a question to ask, albeit a silly one. After nearly blowing the fuse in my brain, I have sort of sussed out how to do the leap year algorithm, well, the first bit of it. The line of code below calculates if a year such as 1600 or 1700 is a leap year:- if (((num % 100) == 0 ) && ((num % 400) == 0))
But the thing is, I am having a problem with the other part of it, i.e., incorporating the ((num % 4)==0) bit, to do the check on whether a year is divisible by four. I thought this would work, but the syntax is wrong:- if (((num % 100) == 0 ) && ((num % 400) == 0)) || ((num % 4)==0) and doing this:- ((((num % 100) == 0 ) && ((num % 400) == 0)) || ((num % 4)==0)) just makes the logic of the algorithm completely wrong!!
I am seriously miffed as to how to combine them, using the correct bracket logic.
John Bonham was stronger, but Keith Moon was faster.
The algorithm in the style guide is wrong, so I have been told.
Joined: Jan 19, 2001
That's news to me. Who told you that? Either way, the style tips you get there, if applied to your code above, would make it a lot easier to check for logic (as opposed to fighting with parens and syntax). Maybe a good starting place for correct "bracket logic" would be to reduce the amount of brackets (parentheses) that you need to deal with... [ December 10, 2002: Message edited by: Pauline McNamara ]
Originally posted by Steve Jensen: The algorithm in the style guide is wrong, so I have been told.
I tried the easy option on this one and "borrowed heavily" from the Style Guide. Even from such a start, it still took 3 attempts to get through. :roll: The logic is correct, but you can improve on the readability ! And the algorithm is presented in a number of styles, so choose wisely and you may get through more quickly.
The algorithm in the style guide is wrong, so I have been told. Well, of the three examples shown, the first does not compile due to inconsistency over whether the variable is named "y" or "year". Once this is corrected, all three algorithms execute correctly for any possible argument. The only other "problems" with the methods are minor complaints: (1) The methods should really be declared static since they make no use of any instance variables. (2) The methods usually perform a few more calculations than necessary. From a performance-is-everything point of view, they could be reworked to be slightly faster, by assuming that ( year % 4 == 0 ) will be false more often than it's true, and that ( year % 400 == 0 ) will be false more often than true. It's possible to reorder the test so that in many cases only one is executed, and it's quite rare to require all three. [Code deleted to protect the innocent] This might be what someone meant when they said the algorithms shown were "wrong" - but they really should've said "a tiny bit inefficient". Practically speaking it doesn't matter much. [ December 12, 2002: Message edited by: Jim Yingst ]