# Thread: Calendar Program. Just need to fix on more thing!!!

1. ## 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.

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;
}
```

2. 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. @Subsonics, I have this in my calendar function

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

4. Originally Posted by Karyumi
@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.

5. Originally Posted by Subsonics
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. Originally Posted by Karyumi
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.