how to make CALENDAR in C++

can anyone finish this program for me?
ive tried my best, but i thats all i can do!

the program is that your input in the month and year,
and its output is of course the calendar of the
exact month and year which is asked!

int newYearDay(int year)
{
const newYear1901=2;
int elapseYear=year-1901;
int leapYear=elapseYear/4;
return(newYear1901+elapseYear+leapYear)%7;
}
int monthDays(int month, int year)
{
int days=0;
switch(month);
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:days=31;break;
case 4:case 6:case 9:case 11:days=30;break;
case 2:days=(year%4==0)?29:28;
}
return days;
}
int firstDay(int month, int year)
{
int elapseDay=newYearDay(year);
int i=1;
if(month>1)
{
for(;i<month;i++)
{
elapseDay=monthDays(month, year);
}
}
return elapseDay%7;
}


We're not going to do your homework for you, however we will help guide you to the solution.

So what information do you need to be able to print the calendar for the given month? You need to know two things:

1) The day of the week that the 1st of the month falls on; and
2) How many days are in the month.

You already have a function that tells you what day January 1st falls on in any given year. And you already have a function that tells you how many days are in a given month in a given year.

So how can you use those two functions to figure out what day of the week the 1st falls on for any given month/year? If you know the algorithm but are having trouble writing the code, then please post your algorithm and your non-working code. If you can't figure out the algorithm, then try this exercise and see if the algorithm becomes obvious to you:

1) Figure out what day of the week January 1st, 1901 falls on.
2) Now figure out what day of the week February 1st, 1901 falls on.
3) Now figure out what day of the week March 1st, 1901 falls on.
4) Now figure out what day of the week April 1st, 1901 falls on.

Do you see a pattern?


this is the latest that code that i did!
anyone can help me?

here:
int newYearDay(int year)
{
const newYear1901=2;
int elapseYear=year-1901;
int leapYear=elapseYear/4;
return(newYear1901+elapseYear+leapYear)%7;
}

int monthDays(int month, int year)
{
int days=0;
switch(month)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:days=31;break;
case 4:case 6:case 9:case 11:days=30;break;
case 2:days=(year%4==0)?29:28;
}
return days;
}
int firstDay(int month, int year)
{
int elapseDay=newYearDay(year);
int i=1;
if(month>1)
{
for(;i<month;i++)
{
elapseDay=monthDays(month, year);
}
}
return elapseDay%7;
}

void chart(int monthDays,int startDay)
{
int i=1;
int j=1;
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
for(;i<=42;i++)
{
if(i<=startDay)
{
printf("\t");
}
else
{
if(j<=monthDays)
{
if(i%7==0)
{
printf("%3d\n", j);
}
else
{
printf("%3d\t", j);
}
j++;
}
}
}
}

void main()
{

int month=0;
int year=0;
clrscr();
printf("enter month\t(1-12):\t\t");scanf("%d", &month);
printf("enter year\t(1901-2008):\t");scanf("%d", &year);
chart(31,1);
getch();
}
Certainly your call to chart() in main is wrong since it does not use the user's input.

Also, your firstDay function is wrong.

If you know that, for example, January 1st, 1901 fell on a Tuesday, then you can
compute that February 1st 1901 fell on a Friday, because January has 31 days,
which is equal to four full weeks plus three additional days. Then you can
compute that March 1st 1901 fell on a Friday also, because February of that year
had 28 days, which is exactly equal to four weeks.

Having said that,

1
2
3
4
5
6
7
8
9
10
int firstDay( int month, int year )
{
    int firstDayOfYear = newYearDay( year );

    // Compute the total number of days in the year in all of the
    // months prior to the one the user specified.
    // HINT: use a foor loop

    return elapseDay%7;
}


Please when (if) you post your code again, use code tags so that it is easier to read.
ohh. im sorry.
i cant just barely undertand that!

our professor made us crazy.
He havent taught us about that!

ive tried it many times
in our laboratory!
and even research!
still i cant.

no one in our class get the progrom correctly!
Ok, forget C++ for the moment. Can you explain the math to me first? If you don't understand the mathematics of the problem, then there is no point to trying to code it.
Topic archived. No new replies allowed.