Like Tree3Likes
  • 2 Post By iMalc
  • 1 Post By AndiPersti

Leap Year Header File

This is a discussion on Leap Year Header File within the C Programming forums, part of the General Programming Boards category; if year modulo 4 is 0 then if year modulo 100 is 0 then if year modulo 400 is 0 ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    4

    Leap Year Header File

    if year modulo 4 is 0 then if year modulo 100 is 0 then if year modulo 400 is 0 then is_leap_year else not_leap_year else is_leap_year else not_leap_year
    You are to create a small C library (i.e. a header and a C code file) of functions involving dates. The first function will indicate if a given year is a leap year or not using a function build from the pseudocode above. It should return a 1 if true or 0 if false. The second function you will write will determine the convert a given date (given in the form year, month, and day) into the number of the day. For example 2013 January 01, is day 1 of 2013, 2013 February 01 is day 32, and 2013 December 31 is day 365. Of course, if 2013 had been a leap year, 2013 December 31 would have been day 366. You will need to know the number of days in each month. Lastly, given a start date after 1582 in the form year, month, day and an end date in the same form, determine the number of days in between. So 2012 January 01 to 2013 January 01 was 366 days (day 1 + 366 days in 2012 day 1 = 366 days) since 2012 was a leap year. Make sure you name the functions and variables to enhance readability. Add comments for clarity.

    If you have main() or printf() in there you are not doing what I am asking.

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    The second function you will write will determine the convert a given date (given in the form year, month, and day) into the number of the day.

    Then?

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    Hey, trial123.

    Firstly, regarding "If you have main() or printf() in there you are not doing what I am asking." What this is referring to, I believe, is that you are not to make an executable program, but rather a library C file which may be utilized by a program by being included using #include.

    Secondly, I believe that all of these functions can greatly benefit from the strtok(3) function. There is a "man page" for this available either in the terminal (if you are using UNIX) by typing "man strtok" into the command line. Alternatively, google is your friend. The 3 inside of the parentheses indicates that this function is passed 3 parameters to utilize in its operations. The strtok(3) function will take the year, month, and day string and tokenize it into words which can be evaluated separately, once each time you call the function. You could nest this inside of a loop and evaluate each token with separate code depending on what format it is in, which token in the string it is, etc.

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    You need two arrays one for a normal year and one for a leap year that contain the number of days in each month so you can add up the days in the year.

    January will be zero because you don't have to add and days for previous months, but februrary will be 31 because the first of feb is day 32 ie 31+1
    j f m a m j j a s o n d
    0, 31 28 31 30 31 30 31 31 30 31 30
    so for 18 july (7th month) you add up the first 7 numbers and add 18

    to save adding you coudl cummulatvely add the days
    0, 31, 59, 90, 120, 151 181 212 243 273 304 334

    SO from the above if you have 27 dec you add 27 to the the 12 element (334) to get 361

    Us this array for a leap year.
    0, 31, 60, 91, 121, 151 182 213 244 274 305 335

    Then using your function to determine is a year is a leap year populate a table add the number of days up between the two years in question and add the difference in the number of days (which will be negative in some cases)

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,377
    If you have main() or printf() in there you are not doing what I am asking.
    Announcements - General Programming Boards

    You must provide us with your attempt first.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by trial123 View Post
    You are to ...
    Explicitly demanding something of the reader huh, right I stop reading right there. You're the one coming here for help; you don't get to make demands, you get to politely ask questions.
    Those that don't bother to do anything but dump their work, verbatim, will get no help, and no sympathy, from me. In fact this even worse than most because the last sentence appears to be written by yourself.

    'nuf said.
    Crossfire and AndiPersti like this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    if year modulo 4 is 0 then if year modulo 100 is 0 then if year modulo 400 is 0 then is_leap_year else not_leap_year else is_leap_year else not_leap_year
    Huh?! Why?! This is all you need:
    Code:
    func is_leap(year)
        return !(year%4);
    end

  8. #8
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Not true, nonpuz. The rules on leap years are exactly as stated. There are some "fourth years" that are not leap years. The leap day is there to keep the date in line with earth's orbit around the sun and seasons, etc. and that's not as easy as "365.25 days", so there has to be adjustment every few centuries.

    And the rule's only been there since Gregorian times. Not like it's a new thing, only borne of modern science.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Derek Lake View Post
    Secondly, I believe that all of these functions can greatly benefit from the strtok(3) function. There is a "man page" for this available either in the terminal (if you are using UNIX) by typing "man strtok" into the command line. Alternatively, google is your friend. The 3 inside of the parentheses indicates that this function is passed 3 parameters to utilize in its operations.
    The '3' in 'strtok(3)' doesn't mean that strtok() takes 3 parameters (actually strtok() only takes 2 parameters).

    The number in the parentheses specifies the section in the UNIX man pages where you find the description for a command/function. Section 3 describes all library functions. For the other sections look at the Wikipedia page.

    Bye, Andreas
    laserlight likes this.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    1,098
    Corresponding unix command is

    man 3 strtok

    Searching this in google tends to work as well. But I like to install the man pages locally if possible.

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    Ah, ok. I am trying to only offer the advice that I'm sure is right, but I have a lot yet to learn! I think it is funny because every time I have seen that number in the parenthesis it has seemed to be my number of parameters :P. I am glad that you cleared that up, and apologize for the bad advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. leap year function
    By cameuth in forum C Programming
    Replies: 5
    Last Post: 11-26-2011, 12:05 AM
  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, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21