Thread: day number in yaer

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    43

    Lightbulb day number in yaer

    I'm conducting self-study C++ and currently reaching functions (void, returning-value function, etc).
    the requirement is that users will input date in form of "month day year" like 12 25 2002 and the output will show that date's day number, which is day 359 ( 1 1 2002 to be the reference, or day 1).
    I'm done with this, but I'm wondering is there any more efficient way to determine how many days in a month

    I used this function
    Code:
    int daysinMonth(int INmonth, int INyear) //Calculate days in months.
    {
    	
    	switch(INmonth)
    	{
    		case(1): case(3): case(5): case(7): 
    		case(8): case(10): case(12): 
    			return 31;
    			break;
    		case(2):
    			if (isLeapYear(INyear))
    			
    				return 29;
    			else
    				return 28;
    			break;
    		case(4): case(6): case(9): case(11):
    			return 30;
    			break;
    	}
    }
    I've been thinking about a loop but don't know how to alternate return value between 30 and 31

    Any insight will be so great!
    thanks!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Looks good to me. I wouldn't worry too much about efficiency here beyond not doing anything horribly inefficient. Worry more about clarity. For example, perhaps use an enum for the months so it is clearer which months are in which case.

    BTW, do you check for an invalid month elsewhere in the code?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    43
    no..i din't implement that.
    Will try.
    I have not reached enum type yet!


    thanks!

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Just a thought -
    How about an array?

    Code:
    daysInMonth_noleap[12] = { 31, 28, 31 .... };
    daysInMonth_leap[12] = { 31, 29, 31 .... };
    Remember to make it 0 - 11 (January = 0, December = 11).

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    43
    I thought abt it too.
    It surely will be better. I'm getting there yet, however
    thanks

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I've use a hybrid of the two:
    Code:
    // This function assumes that month and year are correct values. 
    // Month is 1..12!
    int daysInMonth(int month, int year)
    {
        static const int dayArray[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int days = dayArray[month-1];
        if (month == 2 && leapYear(year))
           days = 29;
        return days;
    }
    The reason for the hybrid is that the extra check of month == 2 is probably no bigger than the overhead of the second array, and you still need an if-statement anyways.

    Edit: Fix up bug with off-by-one.
    Last edited by matsp; 05-24-2008 at 03:03 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > and you still need an if-statement anyways.
    Perhaps
    return days + (month == 2 && leapYear(year));
    since the result of a boolean expression is 0 or 1

    > but I'm wondering is there any more efficient way to determine how many days in a month
    Maybe, but there are plenty of increasingly obscure (see above) shorter answers.

    Consider your code, written as
    Code:
    enum {
      January = 1, February, March, April, // you get the idea
    };
    int daysInMonth ( int month, int year ) {
      int result = 0;
      switch ( month ) {
        case January: case March:
          result = 31;
          break;
        // and so on
        default:
        // error trap
      }
    }
    How easy would it be for the average reader of the code to say whether the function was correct or not?

    Arrays need bound checking, and as cyberfish points out, there's always the 'off-by-1' problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  4. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  5. Airline Booking System - PLZ HELP
    By Gecko2099 in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:17 PM