January 21st, 2013, 07:31 PM

Getting leap year
Can anyone tell me whay the following funtion that should be returning true if the year is leap year and false if it is not, is givign me unexpected results? when I entered 1985 I got true? when I entered 2012 I got false. When I entered 2000 I got true, it was actually right on that one. I'M building a program that will validate if a date is valid or not based on the number of days in each month, I need leap year to determin the max days in FEB, etc... I frist wrote the program in bash and it worked just fine. Now that I've converted the formula over to C++ it's not working out.
Code:
bool is_leap_year( int year )
{
bool result = false;
int local_year = year;
if ( 0 == (( local_year % 4 ) && 0 != ( local_year % 100 ))  0 != ( local_year % 400 ) )
{
result = true;
}
return result;
}
/*END*/
January 21st, 2013, 09:09 PM

Originally Posted by Garrett85
Can anyone tell me whay the following funtion that should be returning true if the year is leap year and false if it is not, is givign me unexpected results? when I entered 1985 I got true? when I entered 2012 I got false. When I entered 2000 I got true, it was actually right on that one. I'M building a program that will validate if a date is valid or not based on the number of days in each month, I need leap year to determin the max days in FEB, etc... I frist wrote the program in bash and it worked just fine. Now that I've converted the formula over to C++ it's not working out.
Code:
bool is_leap_year( int year )
{
bool result = false;
int local_year = year;
if ( 0 == (( local_year % 4 ) && 0 != ( local_year % 100 ))  0 != ( local_year % 400 ) )
{
result = true;
}
return result;
}
/*END*/
A year completely divisible by 4 is a leap year.
look at the following code, its much simpler than your's...
Code:
main()
{
int year;
printf("enter year\n");
scanf("%d",&year);
if(year%4==0)
{
printf("leap year");
}
else{printf("not leap year.");}
}
Comments on this post
January 21st, 2013, 09:37 PM

I believe your leap year formula is flawed.
The Gregorian calendar a typical year has 365 97/400 days = 365.2425 days. There are 97 leap years in every 400 years.
Thus, a year is a leap year if it meets one of the following two conditions:
A. The year is divisible by 4 but not divisible by 100.
B. The year is divisible by 400.
Your logic does not properly address condition B.
January 21st, 2013, 10:19 PM

Originally Posted by BobS0327
I believe your leap year formula is flawed.
The Gregorian calendar a typical year has 365 97/400 days = 365.2425 days. There are 97 leap years in every 400 years.
Thus, a year is a leap year if it meets one of the following two conditions:
A. The year is divisible by 4 but not divisible by 100.
B. The year is divisible by 400.
Your logic does not properly address condition B.
this should work.
Code:
main(){
int year;
printf("enter year\n");
scanf("%d",&year);
if(year%4==0&&year%100!=0year%400==0)
{
printf("leap year\n");
}
else{printf("not leap year.\n");}
}
January 21st, 2013, 11:40 PM

only year%4 is invalid .....
January 22nd, 2013, 01:28 AM

You know, despite the Dogma of Structured Programming, it is not absolutely required to have only one single return from a function, just a generally good idea.
Rather than have one single complex conditional expression that you could easily make a mistake with, break it up into separate simple tests; eg:
Code:
bool is_leap_year( int year )
{
if (year % 4)
return false;
if (!(year % 400))
return true;
if (year % 100)
return true;
else
return false;
}
Of course, if your teacher imposes the Dogma of Structured Programming, then you'd have to do something a bit different.
PS
If you need to create a big hairy complex if statement, then design it with Boolean algebra. Create a truth table with all the conditions and construct a Boolean equation from it. You can simplify equation by combining lines in the truth table, or through algebraic manipulation of the equation, or with another tool such as a Karnaugh map.
I remember a programming assignment in school where I was faced with a horribly complex if statement that seemed impossible to write, so I constructed a truth table and it all just fell right into place. And worked, first time.
Might be overkill in this case, but well worth looking into for future projects. Would also work for proving that a given conditional expression actually does what you want it to do.
Comments on this post
Last edited by dwise1_aol; January 22nd, 2013 at 01:44 AM.
January 22nd, 2013, 02:15 AM

The funny thing about leap years is that the POSIX.1 standard that was finalized in 1988 actually treated a leap year as any year that would have been divisible by 4 and that's it. And this was actually a deliberate design decision taken at that time, not an oversight. Luckily 2000 was a leap year, but 2100 is not. At that time, the justification given for the bogus calculation was that time_t was a 32 bit int and since epoch time is # of seconds since 1/1/1970, the int would overflow well before 2100!
You can read some of the gory details about why they used the wrong calculation here:
http://www.mailarchive.com/leapsecs.../msg00109.html
The POSIX standard was later fixed in 2001 to treat leap years the way that the Gregorian calendar says.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!"  Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website."  Nilpo
January 22nd, 2013, 05:21 PM

Solved
It's solved. I tried to find solved in the thread tools buttons but I was unsuccessful.