# Calender(start date of the month)

Page 1 of 1

## 5 Replies - 1299 Views - Last Post: 14 February 2010 - 10:24 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=155956&amp;s=b65e91d7baf9768e5ef2244d8688933b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 azizknot

Reputation: 0
• Posts: 26
• Joined: 09-November 09

# Calender(start date of the month)

Posted 14 February 2010 - 09:30 AM

Heres my code,
everything is done except how to find the start date of the month and the total number of days since Jan 1, 1800,
can anyone help me out here.

#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void printMonth(int year, int month);
void printMonthTitle(int year, int month);
void printMonthName(int month);
void printMonthBody(int year, int month);
int getStartDay(int year, int month);
int getTotalNumberOfDays(int year, int month);
int getNumberOfDaysInMonth(int year, int month);
bool isLeapYear(int year);
int main()
{
// Prompt the user to enter year
cout << "Enter full year (e.g., 2001): ";
int year;
cin >> year;
// Prompt the user to enter month
cout << "Enter month in number between 1 and 12: ";
int month;
cin >> month;
// Print calendar for the month of the year
printMonth(year, month);
return 0;
}

// Print the calendar for a month in a year
void printMonth(int year, int month)
{

// Print the headings of the calendar
printMonthTitle(year, month);
// Print the body of the calendar
printMonthBody(year, month);
}

// Print the month title, e.g., May, 1999
void printMonthTitle(int year, int month)
{
printMonthName(month);
cout << " " << year << endl;
cout << "-----------------------------" << endl;
cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
}

// Get the English name for the month
void printMonthName(int month)
{
switch (month)
{
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
case 4:
cout << "April";
break;
case 5:
cout << "May";
break;
case 6:
cout << "June";
break;
case 7:
cout << "July";
break;
case 8:
cout << "August";
break;
case 9:
cout << "September";
break;
case 10:
cout << "October";
break;
case 11:
cout << "November";
break;
case 12:
cout << setw(16) << "December";
} }

// Print month body
void printMonthBody(int year, int month)
{

// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);
// Get number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
cout << " ";
for (i = 1; i <= numberOfDaysInMonth; i++)
{
cout << setw(4) << i;
if ((i + startDay) % 7 == 0)
cout << endl;
}}

// Get the start day of the first day in a month
int getStartDay(int year, int month)
{

}
// Get the total number of days since January 1, 1800
int getTotalNumberOfDays(int year, int month)
{

}

// Get the number of days in a month
int getNumberOfDaysInMonth(int year, int month)
{
if(month == 1)
return 31;
else if (month == 2)
{ if(isLeapYear(year))
return 29;
else
return 28;
}
else if(month == 3)
return 31;
else if(month == 4)
return 30;
else if(month == 5)
return 31;
else if(month == 6)
return 30;
else if(month == 7)
return 31;
else if(month == 8)
return 31;
else if(month == 9)
return 30;
else if(month == 10)
return 31;
else if(month == 11)
return 30;
else if(month == 12)
return 31;
}

// Determine if it is a leap year
bool isLeapYear(int year)
{ if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return true;
else
return false;
}

Is This A Good Question/Topic? 0

## Replies To: Calender(start date of the month)

### #2 Martyn.Rae

• The programming dinosaur

Reputation: 545
• Posts: 1,420
• Joined: 22-August 09

## Re: Calender(start date of the month)

Posted 14 February 2010 - 09:33 AM

Also, have a go it yourself first, we will help you only if we can see you have put in some effort first yourself.

This post has been edited by Martyn.Rae: 14 February 2010 - 09:35 AM

### #3 azizknot

Reputation: 0
• Posts: 26
• Joined: 09-November 09

## Re: Calender(start date of the month)

Posted 14 February 2010 - 09:45 AM

Martyn.Rae, on 14 February 2010 - 08:33 AM, said:

Also, have a go it yourself first, we will help you only if we can see you have put in some effort first yourself.

I am new here, i dont know how to put the code betwen code tags.
regarding the start dateof the month, what I thought of was keeping a track of the starting spaces of the previous month and then add it to the previous month date and then divide it by seven and get the remainder which is th start of the month.
this one is hard because need to keep track of the previous spaces since 1800
heres another way
for(year = 1800 ; year < newyear; year++){
thedays = thedays + 1;
(startday of the first month of 1800 + thedays) % 7 = start day of the month

### #4 Astraeus

Reputation: 23
• Posts: 205
• Joined: 17-November 09

## Re: Calender(start date of the month)

Posted 14 February 2010 - 09:48 AM

To post in code tags, post like this

### #5 azizknot

Reputation: 0
• Posts: 26
• Joined: 09-November 09

## Re: Calender(start date of the month)

Posted 14 February 2010 - 09:53 AM

```#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void printMonth(int year, int month);
void printMonthTitle(int year, int month);
void printMonthName(int month);
void printMonthBody(int year, int month);
int getStartDay(int year, int month);
int getTotalNumberOfDays(int year, int month);
int getNumberOfDaysInMonth(int year, int month);
bool isLeapYear(int year);
int main()
{
// Prompt the user to enter year
cout << "Enter full year (e.g., 2001): ";
int year;
cin >> year;
// Prompt the user to enter month
cout << "Enter month in number between 1 and 12: ";
int month;
cin >> month;
// Print calendar for the month of the year
printMonth(year, month);
return 0;
}

// Print the calendar for a month in a year
void printMonth(int year, int month)
{

// Print the headings of the calendar
printMonthTitle(year, month);
// Print the body of the calendar
printMonthBody(year, month);
}

// Print the month title, e.g., May, 1999
void printMonthTitle(int year, int month)
{
printMonthName(month);
cout << " " << year << endl;
cout << "-----------------------------" << endl;
cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
}

// Get the English name for the month
void printMonthName(int month)
{
switch (month)
{
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
case 4:
cout << "April";
break;
case 5:
cout << "May";
break;
case 6:
cout << "June";
break;
case 7:
cout << "July";
break;
case 8:
cout << "August";
break;
case 9:
cout << "September";
break;
case 10:
cout << "October";
break;
case 11:
cout << "November";
break;
case 12:
cout << setw(16) << "December";
} }

// Print month body
void printMonthBody(int year, int month)
{

// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);
// Get number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
cout << " ";
for (i = 1; i <= numberOfDaysInMonth; i++)
{
cout << setw(4) << i;
if ((i + startDay) % 7 == 0)
cout << endl;
}}

// Get the start day of the first day in a month
int getStartDay(int year, int month)
{

}
// Get the total number of days since January 1, 1800
int getTotalNumberOfDays(int year, int month)
{

}

// Get the number of days in a month
int getNumberOfDaysInMonth(int year, int month)
{
if(month == 1)
return 31;
else if (month == 2)
{ if(isLeapYear(year))
return 29;
else
return 28;
}
else if(month == 3)
return 31;
else if(month == 4)
return 30;
else if(month == 5)
return 31;
else if(month == 6)
return 30;
else if(month == 7)
return 31;
else if(month == 8)
return 31;
else if(month == 9)
return 30;
else if(month == 10)
return 31;
else if(month == 11)
return 30;
else if(month == 12)
return 31;
}

// Determine if it is a leap year
bool isLeapYear(int year)
{ if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return true;
else
return false;
}

```

```#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void printMonth(int year, int month);
void printMonthTitle(int year, int month);
void printMonthName(int month);
void printMonthBody(int year, int month);
int getStartDay(int year, int month);
int getTotalNumberOfDays(int year, int month);
int getNumberOfDaysInMonth(int year, int month);
bool isLeapYear(int year);
int main()
{
// Prompt the user to enter year
cout << "Enter full year (e.g., 2001): ";
int year;
cin >> year;
// Prompt the user to enter month
cout << "Enter month in number between 1 and 12: ";
int month;
cin >> month;
// Print calendar for the month of the year
printMonth(year, month);
return 0;
}

// Print the calendar for a month in a year
void printMonth(int year, int month)
{

// Print the headings of the calendar
printMonthTitle(year, month);
// Print the body of the calendar
printMonthBody(year, month);
}

// Print the month title, e.g., May, 1999
void printMonthTitle(int year, int month)
{
printMonthName(month);
cout << " " << year << endl;
cout << "-----------------------------" << endl;
cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
}

// Get the English name for the month
void printMonthName(int month)
{
switch (month)
{
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
case 4:
cout << "April";
break;
case 5:
cout << "May";
break;
case 6:
cout << "June";
break;
case 7:
cout << "July";
break;
case 8:
cout << "August";
break;
case 9:
cout << "September";
break;
case 10:
cout << "October";
break;
case 11:
cout << "November";
break;
case 12:
cout << setw(16) << "December";
} }

// Print month body
void printMonthBody(int year, int month)
{

// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);
// Get number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
cout << " ";
for (i = 1; i <= numberOfDaysInMonth; i++)
{
cout << setw(4) << i;
if ((i + startDay) % 7 == 0)
cout << endl;
}}

// Get the start day of the first day in a month
int getStartDay(int year, int month)
{

}
// Get the total number of days since January 1, 1800
int getTotalNumberOfDays(int year, int month)
{

}

// Get the number of days in a month
int getNumberOfDaysInMonth(int year, int month)
{
if(month == 1)
return 31;
else if (month == 2)
{ if(isLeapYear(year))
return 29;
else
return 28;
}
else if(month == 3)
return 31;
else if(month == 4)
return 30;
else if(month == 5)
return 31;
else if(month == 6)
return 30;
else if(month == 7)
return 31;
else if(month == 8)
return 31;
else if(month == 9)
return 30;
else if(month == 10)
return 31;
else if(month == 11)
return 30;
else if(month == 12)
return 31;
}

// Determine if it is a leap year
bool isLeapYear(int year)
{ if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return true;
else
return false;
}

```

### #6 Martyn.Rae

• The programming dinosaur

Reputation: 545
• Posts: 1,420
• Joined: 22-August 09

## Re: Calender(start date of the month)

Posted 14 February 2010 - 10:24 AM

Hmmm ... I think I see where you are going on this one! Every year the start of the month starts on a day one greater than the previous year. So Feb 14 2009 was on a Saturday, this year it's on a Sunday. The problem is the leap years from 28th Feb. There on a leap year, it advances 2 days ... so

Feb 14 2008 the day was a Thurday ... it's a leap year so Feb 14 2009 is not a Friday but a Saturday.

So, for 1st Jan 1800, that was a Wednesday (I remember that day well ). So the 1st Jan 1900 would have been 100/7 = 2 days advanced from Wednesday which makes it a Friday. Then we have to add on the 25 leap years which is 24/7 = 3 so Friday, Saturday, Sunday, Monday. So 1st Jan 1900 would have been on a Monday if 1st Jan 1800, was a Wednesday. I think the logic is sound. All you need to do now is to put that into code