Thread: Day of year?

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Day of year?

    Hey guys...Happy Thanksgiving!!

    Well as for me I am at home doing homework, and I ran into a bit of a snag. The problem is I have to return an integer value ( 1 to 366) when entering the month, day, and year.

    I am lost on the algorithm I would use!!!

    I thought about entering the number of days for each month in an array and add those with the amount of days. Would that work? Or is there a better way to do this problem?

    Thanks in advance!
    CJ

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Day of year?

    Originally posted by CrackerJack
    I thought about entering the number of days for each month in an array and add those with the amount of days. Would that work?
    Try it and find out. Run through the logic in your head or on paper. Then turn it into code.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Well it seems like it would work. Trying to work on the code now. Except then I really would not need the year except for the leap years right?

    Which would add a day if I am not mistaken.

    CJ

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. You'll need to figure out some way to ask if the year is a leap year or what not. Otherwise everything past Feb 28th will be 1 day off.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    It seems as though it is working, so I am hapy there. However, where would I start to through in the leap year if?

    CJ

    Code:
    #include <stdio.h>
    
    int day_of_year(int a, int b, int c);
    
    main()
    {
    	int day=0, month=0, year=0;
    
    	printf("Enter the month in number form (ex. January being '1') : ");
    	scanf("%d", &month);
    	printf("Enter the date : ");
    	scanf("%d", &day);
    	printf("Enter the year : ");
    	scanf("%d", &year);
    
    	printf("The day of the year you selected was %d \n", day_of_year(month, day, year) );
    
    }
    
    int day_of_year(int a, int b, int c)
    {
    	int i=0, daymon=0, dayday=0;
    	int mth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
    
    	dayday = b;
    	
    	for (i = 0; i < a; i++)
    	{
    		daymon += mth[i];
    	}
    
    
    return (daymon + dayday);
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well since you're actually asking the year, you can simply determine if it's leap year from that number.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    Several points:

    Learning calendrics is nice, but it is already a done deal.
    Plus, most people do not understand the Gregorian calendar shift\
    and the interesting and varying state of dates after the 1580's in Europe.

    More important - why does your prof have you doing basic stuff that standard C handles? - see struct tm in time.h

    Most important point: you guys should NOT be doing time and date operations with algorithms you create, you should use proven code for that. In the real world, messing with money and dates - and screwing either one up - gets you fired. Fast.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by jim mcnamara
    Most important point: you guys should NOT be doing time and date operations with algorithms you create, you should use proven code for that. In the real world, messing with money and dates - and screwing either one up - gets you fired. Fast.
    Get real. The lesson is to teach how to think a bit for yourself, and how to do various basic C lessons all rolled into one.

    It's nice that you're a teacher and all, except for the fact that you're a moron.

    By your logic, people should never learn the standard "Hello World" application, because "it's been done".

    How the hell are people supposed to learn to program if people like you tell them not to cover the basics?

    "Just jump right into kernel programming for your first assignent. All that stuff on linked lists, dynamic memory allocation, arrays, basic data types, basic IO, all that's been done. Just skip it."

    Yeah, that makes lots of sense.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Jim I understand where your coming from, however I am a newbie to all this stuff. So, I think for me to understand all the logic is the best thing to accomplish first. I can always go back and learn functions later.

    OK I have done this piece of code after searching. And from what I read the extra day is added on to the end of year total correct? So when entering 12/31/1996 it should give off a 366 day total.

    CJ

    Code:
    #include <stdio.h>
    
    int day_of_year(int a, int b, int c);
    
    main()
    {
    	int mth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	int day=0, month=0, year=0;
    
    	printf("Enter the month in number form (ex. January being '1') : ");
    	scanf("%d", &month);
    	
    	printf("Enter the date : ");
    	
    while ( scanf("%d", &day) != (day <= mth[month] + 1) || (day >= mth[month] + 1) )
    	printf("Not a valid date inside selected month! Try again! :"); 
    
    	printf("Enter the year : ");
    	scanf("%d", &year);
    	
    	printf("The day of the year you selected was %d \n", day_of_year(month, day, year) );
    
    }
    
    int day_of_year(int a, int b, int c)
    {
    	int i=0, daymon=0, dayday=0;
    	int mth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
    
    	if ( (c % 4) || (c % 100) && (c % 400) )
    		mth[3] = mth[3] + 1;
    	
    	for (i = 0; i < a; i++)
    	{
    		daymon += mth[i];
    	}
    
    	dayday = b;
    
    
    return (daymon + dayday);
    }
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if ( (c % 4) || (c % 100) && (c % 400) )
    		mth[3] = mth[3] + 1;
    You'll want to fix this check. Otherwise what happens is c%4 is checked first, and the rest is ignored. Thus, those special cases dealing with centuries and leap year won't be accurate.

    Also, don't you mean:
    Code:
    if( ((c % 4 ) == 0) ...
    Otherwise, three out of four years will be a leap year.

    And finally, the line:
    Code:
    mth[3] = mth[3] + 1;
    Can be simplified as:
    Code:
    daymon = 1;
    Just start your total at 1 instead of zero and you don't have to modify your array.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int mth[13] = &#123;0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31&#125;;
    You've written this array out twice

    > printf("Not a valid date inside selected month! Try again! :");
    You cannot enter Feb 29th, even if it's a leap year.
    You need to input the year before you can properly validate the days within a month.

    > mth[3] = mth[3] + 1;
    I thought Feb was month 2
    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.

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by quzah
    And finally, the line:
    Code:
    mth[3] = mth[3] + 1;
    Can be simplified as:
    Code:
    daymon = 1;
    Just start your total at 1 instead of zero and you don't have to modify your array.

    Quzah.
    1) Not so. This will make all of January and February one day off. But you don't want March to have 32 days, so you want to use either:
    1a) mth[2]++; instead.
    1b) if leap year AND month > 2, add 1 to daymon.

    2) Quzah's first comment is correct

    3) Your input will not allow you to enter Feb 29 even during a leap year. You need to check for valid information after the year is entered.

    4) Nitpicky, but your for loop could start at 1 instead of 0 since there is no month 0.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calendar Problem
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-29-2002, 03:36 PM
  2. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  3. Printing weekday
    By sworc66 in forum C Programming
    Replies: 12
    Last Post: 09-13-2002, 07:03 AM
  4. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM