Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

TWO functions to fulfill the requirements of this question: Please comment all l

ID: 3566202 • Letter: T

Question

TWO functions to fulfill the requirements of this question:

Please comment all lines so I can understand the program flow.

Write a C program that prints a one month calendar. The user specifies the month and year of
which he/she would like to print:

Enter the month: October
Enter the year: 2014

Mon Tue Wed Thu Fri Sat Sun
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

Hint: To find the first day of the month, the following formula might help, which is for the
Gregorian calendar only, may be more convenient for computer programming. Note that in
some programming languages the remainder operation can yield a negative result if given a
negative operand, so mod 7 may not translate to a simple remainder.


k is day (1 to 31)
m is month (1 = March, ..., 10 = December, 11 = Jan, 12 = Feb) treat Jan & Feb as months
of the preceding year
C is century (2014 has C = 20)
Y is year (2014 has Y = 14 except Y = 13 for Jan & Feb)
W is week day (0 = Sunday, ..., 6 = Saturday)

Once done, please double check the program output with different months and years for several times.

TWO functions to fulfill the requirements of this question: Write a C program that prints a one month calendar. The user specifies the month and year of which he/she would like to print: Enter the month: October Enter the year: 2014 Mon Tue Wed Thu Fri Sat Sun 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 Hint: To find the first day of the month, the following formula might help, which is for the Gregorian calendar only, may be more convenient for computer programming. Note that in some programming languages the remainder operation can yield a negative result if given a negative operand, so mod 7 may not translate to a simple remainder. W=(k+[2.6m-0.2]-2C+Y+[Y/4]+[C/4])mod7 k is day (1 to 31) m is month (1 = March, ..., 10 = December, 11 = Jan, 12 = Feb) treat Jan & Feb as months of the preceding year C is century (2014 has C = 20) Y is year (2014 has Y = 14 except Y = 13 for Jan & Feb) W is week day (0 = Sunday, ..., 6 = Saturday)

Explanation / Answer

#include "stdio.h"

void main()
{
int f=0,year,month,mod,i,j,k,m;
char z[1];

clrscr();
while(1)
{
if(f==0)
{ gotoxy(20,2);
printf(" Key in the year");
scanf("%d",&year);
gotoxy(20,4);
printf(" Key in the month(digits)");
scanf("%d",&month);
clrscr();
if(year<1900||year>3500)
{
printf(" Calendar out of range");
continue;
}
if(month>12||month<1)
{
printf(" Invalid month");
continue;
}
creategrid();
header(year,month);
f=1;
}


if(year<1900)
break;

mod=dayofmonth(year,month);

if(mod==0)
mod=7;

j=daysinmonth(year,month);


for(i=1,k=mod;i<=j;i++,k++)
{
gotoarr(k);
printf("%d",i);
}


m=getkey();

if(m==72)
year=year+1;

if(m==80)
year=year-1;

if(m==75)
month=month-1;

if(m==77)
month=month+1;

if(month==13)
{
month=1;
year=year+1;
}

if(month==0)
{
month=12;
year=year-1;
}

if(year<1900)
break;

clrscr();
creategrid();
header(year,month);


}
}

creategrid()
{
int i;

for(i=17;i<=59;i++)
{
if(i==17)
{

gotoxy(i,1);
printf("%c",218);
gotoxy(i,22);
printf("%c",192);
}
else if(i==59)
{
gotoxy(i,1);
printf("%c",191);
gotoxy(i,22);
printf("%c",217);
}
else
{
gotoxy(i,1);
printf("%c",196);
gotoxy(i,22);
printf("%c",196);
}
}

for(i=20;i<=56;i++)
{
if(i==20)
{
gotoxy(i,4);
printf("%c",218);
gotoxy(i,21);
printf("%c",192);
}
else if(i==56)
{
gotoxy(i,4);
printf("%c",191);
gotoxy(i,21);
printf("%c",217);
}
else
{
gotoxy(i,4);
printf("%c",196);
gotoxy(i,21);
printf("%c",196);
}
}

for(i=2;i<=21;i++)
{
gotoxy(17,i);
printf("%c",179);
gotoxy(59,i);
printf("%c",179);
}

for(i=5;i<=20;i++)
{
gotoxy(20,i);
printf("%c",179);
gotoxy(56,i);
printf("%c",179);
}

gotoxy(22,7);
printf("Mon");

gotoxy(27,7);
printf("Tue");

gotoxy(32,7);
printf("Wed");

gotoxy(37,7);
printf("Thu");

gotoxy(42,7);
printf("Fri");

gotoxy(47,7);
printf("Sat");

gotoxy(52,7);
printf("Sun");

gotoxy(21,23);
printf("%c=Next year",30);

gotoxy(39,23);
printf("%c=Previous year",31);

gotoxy(21,24);
printf("%c=Next month",16);

gotoxy(39,24);
printf("%c=Previous month",17);

}

getkey()
{
union REGS i,o;
while(!kbhit())
;
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}

header(int year,int month)
{
int len;
char *m;

switch(month)
{
case 1:
m="January";
break;

case 2:
m="February";
break;

case 3:
m="March";
break;

case 4:
m="April";
break;

case 5:
m="May";
break;

case 6:
m="June";
break;

case 7:
m="July";
break;

case 8:
m="August";
break;

case 9:
m="September";
break;

case 10:
m="October";
break;

case 11:
m="November";
break;

case 12:
m="December";
break;
}

len=strlen(m)+5;
len=len/2;
gotoxy(38-len,5);
printf("%s %d",m,year);
}

dayofmonth(int year,int month)
{
int leap,mod,check=0,i,diff;
float days;
long days1;

if((year%4==0&&year%100!=0)||year%400==0)

check=1;


year=year-1;
diff=year-1900;
leap=(year-1900)/4-(year-1900)/100+((year/400)-4);
days=((diff-leap)*365.0)+(leap*366.0)+365+1;


for(i=1;i<=12;i++)
{
if(i==month+1)
break;

else if(i==1)
continue;

else if(i==3)
{
if(check==0)
days=days+28;

else if(check==1)
days=days+29;
}

else if(i<9)
{
if(i%2==0)
days=days+31;

else
days=days+30;
}

else if(i==9)
days=days+31;

else if(i>9)
{
if(i%2==0)
days=days+30;

else
days=days+31;
}
}

days1=days;

mod=days1%7;
return(mod);
}

daysinmonth(int year,int month)
{
int days;
if(((year%4==0&&year%100!=0)||year%400==0)&&month==2)
days=29;

else if(month==2)
days=28;

else if(month<8)
{
if(month%2==0)
days=30;
else
days=31;
}

else if(month==8)
days=31;

else if(month>8)
{
if(month%2==0)
days=31;
else
days=30;
}

return(days);
}

gotoarr(int i)
{
int row,col,x,y;

row=((i-1)/7)+1;

if(i<=7)
i=i+7;

col=i%7;

if(col==0)
col=7;

y=7+(2*row);
x=17+(5*col);
gotoxy(x,y);
}