I need to write a program that only accepts a month and year between 1/1901 and 12/2099 and print out a monthly calendar (January 1, 1901 is a Tuesday). I know how factor in a leap year. What I'm having trouble with is starting off...
Enter a month and year between 1/1901 and 12/2099: 10/2014 (entered by user)
Calendar: October, 2014
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Print a calendar for another month? (Y/N) y
Enter a month and year between 1/1901 and 12/2099: 2/2012 (entered in by user)
Calendar: February, 2012
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29
Print a calendar for another month? (Y/N) n
Press any key to continue . . .
This is what I have so far (not complete), I realize there are some parts that do not make sense, those are the places I need the most help.
what is this? string dayName = "Sun Mon Tue Wed Thu Fri Sat;
What is this? numDays = (year - 1901) * 365 + (year - 1901)/ 4;
what is this? firstDay = (2 + numDays) % 7;
what are you doing?
@azl4182
I wrote a calendar program awhile back, and used this function to find the first day of any given month in a year
1 2 3 4 5 6 7 8
int FirstDayOfMonth(int year,int month)
{
int t[12] = {0,3,2,5,0,3,5,1,4,6,2,4};
int y = year - (month < 2);
y=(y + y/4 - y/100 + y/400 + t[month]+1) % 7;
// y = 0 for Sunday, 1 for Monday.. etc.
return y;
}
And this let's you know if the year is a leap year or not.
1 2 3 4 5 6 7 8 9 10 11
int GetDaysInMonth(int year,int month)
{
int The_months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
// Days In Each Month;
bool A_Leap_Year = ((year%400 == 0) || (year%4==0 && year%100 !=0));
if( A_Leap_Year)
The_months[1] = 29;
return The_months[month];
}
@whitenite
I also wrote that program about 2 years ago.
I used this function.
1 2 3 4 5 6 7 8
int day(int d,int m, int y)
{
/*The following, invented by Mike Keith and published in Journal of Recreational Mathematics,
Vol. 22, No. 4, 1990, p. 280,
is conjectured to be the shortest expression of a day-of-the-week algorithm: */
return (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7 ;
}
Yours is,
1 2 3 4 5 6 7 8
int FirstDayOfMonth(int year,int month)
{
int t[12] = {0,3,2,5,0,3,5,1,4,6,2,4};
int y = year - (month < 2);
y=(y + y/4 - y/100 + y/400 + t[month]+1) % 7;
// y = 0 for Sunday, 1 for Monday.. etc.
return y;
}
The problem is, i have never understood the arithmetic in my function and in yours likewise.
Can you please explain the arithmetic??
My mistake, i left out that the 1st day of January 1901 is a Tuesday
No, you mentioned that in your original post.
@shadowCODE
The int t[] array is for how many years to add to the year. (y)
My mistake. It should read..
The int t[] array is for how many years to subtract from the year. (y)
y will equal the year minus the month sent IF it's less than 2. (0 or 1)
Add 1 to year if it's leap year, plus the value of t[month] plus 1, then get the modulus 7 of it.
That value (y), will be the first day of the given month.
That's the way I understand it. If I'm mistaken, sorry. All I know, is it works.