Given that 1 January 2017 is a Sunday, write a program to display the full month
ID: 3595931 • Letter: G
Question
Given that 1 January 2017 is a Sunday, write a program to display the full month view of the calendar given a month in 2017. For example, if the given month is March, your display could look like this: March 2017 Sun MonTue Wed ThFri Sat 11 14 15 16 1718 25 9 10 12 19 13 20 21 22 23 24 26 27 28 2930 31 Your program should be modular, with functions for the following: 1. A function to return the day of the week for the first day of the given month. 2. A function to print the header lines of the calendar, ie. the top two lines in the example above 3. A function to print the lines in the calendar You may provide more functions if you see the need. You need to provide a main program that asks the user for the required month, and then proceed to print the calendar as above. You may add other display features to make your calendar look good if you wish. Needless to say, neat formatting of the display is essential. The above will earn you a maximum of 70%. For an extra 20%, extend your program to allow printing of any month in any year, past, present or future. Remember in this case, you need to take care of leap years. Recall the leap year exercise that you did for one of the earlier tutorials? (You need another function for this, or extend the first function above.) The remaining 10% is for programs that are neat and easy to read and understand. As previously, please name your file with your own name. The first line of your program should be a comment containing your name also. Failure to do either of these will cost you 5%.Explanation / Answer
Below is your code:-
#include <iostream>
using namespace std;
int isLeapYear( int year ); /* True if leap year */
int leapYears( int year ); /* The number of leap year */
int todayOf( int y, int m, int d); /* The number of days since the beginning of the year */
long days( int y, int m, int d); /* Total number of days */
void calendar(int y, int m); /* display calendar at m y */
int main(void){
int year,month;
cout<<"Enter the month: ";
cin>>month;
year = 2017;
calendar(year, month);
return 0;
}
int isLeapYear( int y ) /* True if leap year */
{
return(y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0));
}
int leapYears( int y ) /* The number of leap year */
{
return y/4 - y/100 + y/400;
}
int todayOf( int y, int m, int d) /* The number of days since the beginning of the year */
{
static int DayOfMonth[] =
{ -1/*dummy*/,0,31,59,90,120,151,181,212,243,273,304,334};
return DayOfMonth[m] + d + ((m>2 && isLeapYear(y))? 1 : 0);
}
long days( int y, int m, int d) /* Total number of days */
{
int lastYear;
lastYear = y - 1;
return 365L * lastYear + leapYears(lastYear) + todayOf(y,m,d);
}
void calendar(int y, int m) /* display calendar at m y */
{
const char *NameOfMonth[] = { NULL/*dummp*/,
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
char Week[] = "Su Mo Tu We Th Fr Sa";
int DayOfMonth[] =
{ -1/*dummy*/,31,28,31,30,31,30,31,31,30,31,30,31 };
int weekOfTopDay;
int i,day;
weekOfTopDay = days(y, m, 1) % 7;
if(isLeapYear(y))
DayOfMonth[2] = 29;
cout<<" "<<NameOfMonth[m]<<" "<<y<<" "<<Week<<" ";
for(i=0;i<weekOfTopDay;i++)
cout<<" ";
for(i=weekOfTopDay,day=1;day <= DayOfMonth[m];i++,day++){
printf("%2d ",day);
if(i % 7 == 6)
cout<<endl;
}
cout<<endl;
}
UPDATE
#include <iostream>
#include<iomanip>
using namespace std;
int isLeapYear( int year ); /* True if leap year */
int leapYears( int year ); /* The number of leap year */
int todayOf( int y, int m, int d); /* The number of days since the beginning of the year */
long days( int y, int m, int d); /* Total number of days */
void calendar(int y, int m); /* display calendar at m y */
int main(void){
int year,month;
cout<<"Enter the month: ";
cin>>month;
year = 2017;
calendar(year, month);
return 0;
}
int isLeapYear( int y ) /* True if leap year */
{
return(y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0));
}
int leapYears( int y ) /* The number of leap year */
{
return y/4 - y/100 + y/400;
}
int todayOf( int y, int m, int d) /* The number of days since the beginning of the year */
{
static int DayOfMonth[] =
{ -1/*dummy*/,0,31,59,90,120,151,181,212,243,273,304,334};
return DayOfMonth[m] + d + ((m>2 && isLeapYear(y))? 1 : 0);
}
long days( int y, int m, int d) /* Total number of days */
{
int lastYear;
lastYear = y - 1;
return 365L * lastYear + leapYears(lastYear) + todayOf(y,m,d);
}
void calendar(int y, int m) /* display calendar at m y */
{
const char *NameOfMonth[] = { NULL/*dummp*/,
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
char Week[] = "Su Mo Tu We Th Fr Sa";
int DayOfMonth[] =
{ -1/*dummy*/,31,28,31,30,31,30,31,31,30,31,30,31 };
int weekOfTopDay;
int i,day;
weekOfTopDay = days(y, m, 1) % 7;
if(isLeapYear(y))
DayOfMonth[2] = 29;
cout<<" "<<NameOfMonth[m]<<" "<<y<<" "<<Week<<" ";
for(i=0;i<weekOfTopDay;i++)
cout<<" ";
for(i=weekOfTopDay,day=1;day <= DayOfMonth[m];i++,day++){
cout<< setw(2) << day<<" ";
if(i % 7 == 6)
cout<<endl;
}
cout<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.