#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    49
    Rep Power
    9

    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*/
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    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

    • DaWei_M disagrees : It's not that simple, sorry.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    185
    Rep Power
    82
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    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!=0||year%400==0)
        {
            printf("leap year\n");
        }
        else{printf("not leap year.\n");}
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    67
    Rep Power
    0
    only year%4 is invalid .....
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    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 over-kill 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

    • DaWei_M agrees : A single point of return, while a good idea, can be ineffably stupid in some circumstances.
    Last edited by dwise1_aol; January 22nd, 2013 at 12:44 AM.
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    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.mail-archive.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
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    49
    Rep Power
    9

    Solved


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

IMN logo majestic logo threadwatch logo seochat tools logo