Calendar Program. Just need to fix on more thing!!!

This is a discussion on Calendar Program. Just need to fix on more thing!!! within the C Programming forums, part of the General Programming Boards category; So my program basically asks for a user input of 1-12 and 1978-3000, for the month and year respectively. The ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    13

    Calendar Program. Just need to fix on more thing!!!

    So my program basically asks for a user input of 1-12 and 1978-3000, for the month and year respectively.
    The output should be a calendar of the specified month and year.
    I've already gotten the output format correctly, but I can't see to get the position of the first day of the month right.
    Can anyone please help me fix this?


    Code:
    #include <stdio.h> char *months[] = { " ", "\nJanuary", "\nFebruary", "\nMarch", "\nApril", "\nMay", "\nJune", "\nJuly", "\nAugust", "\nSeptember", "\nOctober", "\nNovember", "\nDecember" }; int month_days[] = {0, 31, 28, 31, 30, 31, 30, 31 ,31 ,30, 31, 30, 31}; int first_day_year(int year) { int first_day; int x; int y; int z; x = (year - 1.)/4.0; y = (year - 1.)/100.; z = (year - 1.)/400.; first_day = (year + x - y + z) %7; return first_day; } int leapyear(int year) { if(year%4 == 0 && year%100 != 0 || year%400 == 0) { month_days[2] = 29; return 1; } else { month_days[2] = 28; return 0; } } int calendar(int month, int year, int first_day) { int i; printf("%s %d\n\n", months[month], year); printf("Sun Mon Tue Wed Thu Fri Sat\n"); for(i = 0; i < month; i++) { first_day = (first_day + month_days[month]) % 7; } for(i = 1; i <= 1 + first_day * 5; i++) { printf(" "); } for(i = 1; i <= month_days[month]; i++) { printf("%2d", i); if((i + first_day)%7 > 0) printf(" "); else printf("\n "); } } int main(void) { int year; int month; int first_day; do{ printf("Enter a month (1 - 12): "); scanf("%d", &month); } while (month < 1 || month > 12); do{ printf("Enter a year (1978 - 3000): "); scanf("%d", &year); } while (year < 1978 || year > 3000); first_day = first_day_year(year); leapyear(year); calendar(month, year, first_day); printf("\n"); return 0; }
    Last edited by Karyumi; 03-04-2012 at 07:22 PM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You need to figure out how many days have passed since your "start of time" date, then modulo 7 that, and perhaps add an offset to get the week day right. I don't see how you can get the correct week day from only inputing a year. Look at this to get an ideas of what might be involved: Julian day - Wikipedia, the free encyclopedia

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    @Subsonics, I have this in my calendar function

    Code:
    for(i = 0; i < month; i++)
    {
    first_day = (first_day + month_days[month]) % 7;
    }


    Thanks for the link.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Karyumi View Post
    @Subsonics, I have this in my calendar function
    Right, but you first_day variable is calculated without regard to any relation to Jan 1 1978. What you need is something like days_since_jan_1_1978, and that would also be similar to existing established systems like JDN and unix epoch (Although the latter counts seconds).

    Edit: To clarify, this is important since you know that Jan 1 1978 was a Sunday, without any relation to a known date and weekday you can't get the weekday of arbitrary dates. It's calculated in relation to your picked epoch (01-01-1978). Since this is also a sunday you will probably need to offset your day by 1 after your modulo 7 operation, since you weekday array start at monday.
    Last edited by Subsonics; 03-04-2012 at 09:03 PM.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Quote Originally Posted by Subsonics View Post
    Right, but you first_day variable is calculated without regard to any relation to Jan 1 1978. What you need is something like days_since_jan_1_1978, and that would also be similar to existing established systems like JDN and unix epoch (Although the latter counts seconds).
    I see. But doesn't my first_day_year function calculate the first day of the year? So by adding the days of the months until the month before the month specified by the user, I should be able to get the 1st day of the specified month, right?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Karyumi View Post
    I see. But doesn't my first_day_year function calculate the first day of the year? So by adding the days of the months until the month before the month specified by the user, I should be able to get the 1st day of the specified month, right?
    This would be correct if you knew what weekday Jan 1 was for any arbitrarily picked year, which you can not know. You have picked a date in the past where you do know, this is your reference you have to hold on to and use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to convert from solar calendar to lunar calendar??
    By zaracattle in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2006, 06:17 AM
  2. Help with calendar program please
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 10-31-2002, 10:11 AM
  3. Calendar program
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2002, 08:44 PM
  4. calendar program
    By ds123@rediffmail.com in forum C Programming
    Replies: 1
    Last Post: 02-11-2002, 09:44 AM
  5. calendar program
    By dharmastum in forum C Programming
    Replies: 3
    Last Post: 01-25-2002, 04:35 AM

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