Thread: leap year function

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    22

    leap year function

    alright, now I need to get the following function to tell me the number of leap years between two dates. It reads
    Code:
    int leap(int year, int year_1)
    {
    for((year%400 ==0 || (year%100 != 0 && year%4 == 0));year<year_1;year++)
    leapy+=leapy;
    }
    As it is, it reads that every year is a leap. so for the distance from dates 1/1/1 to 1/1/100 it reads 99 leaps. Any suggestions?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by cameuth View Post
    alright, now I need to get the following function to tell me the number of leap years between two dates. It reads
    Code:
    for((year%400 ==0 || (year%100 != 0 && year%4 == 0));year<year_1;year++)
    a for() statement is of the form...
    Code:
    for( initialization ; test ; increment ) {}
    In other words you put a test condition in the initialization position where C does no test and acts on the expression only once.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    Code:
    int leap(int year, int year_1)
    {
    for(year<year_1;(year%400 ==0 || (year%100 != 0 && year%4 == 0));year++)
    leapy++;
    }
    this reads out 0 instead of 100, how am i wrong now?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The most obvious way you are wrong is that you are just trying things at random, rather than actually trying to understand. (Before you deny that characterisation, I point out that the evidence for it is easily recognisable in your posts within this thread, so even a moderately experienced C programmer will KNOW that is exactly what you are doing. Denial will simply prove my point.)

    The post by Tclausex is not a complete answer to your question. However, if you actually apply some effort to actually understanding that post then you will be one step closer to solving your problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    yep, realized it after I posted. got it now though.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You need to ask yourself, why am I looping? what am I doing in each loop? what is controlling the number of times I loop?

    You are trying to do too much with the for() expressions. There's a lot to say about for() loops, omitting expressions, and the comma operator, so I just gave you a short reply. I would recommend you just use a while() loop for this particular problem. It will force you to clean up your code and realize the logic needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. leap year program
    By camel-man in forum C Programming
    Replies: 5
    Last Post: 05-23-2011, 07:50 PM
  2. Leap Year Problems
    By jc99 in forum C Programming
    Replies: 13
    Last Post: 06-21-2009, 04:02 AM
  3. Program showing if a year its leap year or not.
    By Cyberman86 in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2008, 08:00 AM
  4. checking a leap year
    By elton_fan in forum C Programming
    Replies: 7
    Last Post: 04-10-2007, 05:59 AM
  5. Help with leap year program.
    By IxTanGxI in forum C Programming
    Replies: 8
    Last Post: 02-20-2006, 08:49 PM