hello,
i have an assignment to create and print a calendar programme
the prob is, we have to add in various additional features in order to get higher marks
since i'm new to C, i need help and suggestions to improve on my code
also, can anyone please show me how to use the DOWHILE in my code?
thank you very much i really appreciate any help
this is the code we have written:
#include <stdio.h>
#include <stdlib.h>
int DayOfWeek(int year, int month);
int CenturyTable(int century);
int MonthTable(int month, int leapStatus);
int leapYear(int year);
//int numDays(int y);
void printMonth (int startDay, int days);
int main()
{
int year, month;
int y;
int numDays;
printf("Enter the year for your calender (YYYY): ");
scanf("%d",&year);
printf("Enter the month for your calender (MM): ");
scanf("%d",&month);
if(year >= 1800 && year <= 2699)
{
y = DayOfWeek(year, month);
printf("\n\t\t");
if(y==0)
printf("1/%d/%d is a Sunday\n",month,year);
else if(y==1)
printf("1/%d/%d is a Monday\n",month,year);
else if(y==2)
printf("1/%d/%d is a Tuesday\n",month,year);
else if(y==3)
printf("1/%d/%d is a Wednesday\n",month,year);
else if(y==4)
printf("1/%d/%d is a Thursday\n",month,year);
else if(y==5)
printf("1/%d/%d is a Friday\n",month,year);
else if(y==6)
printf("1/%d/%d is a Saturday\n",month,year);
}
else
printf("Out of Range. Please try again.\n");
if(month == 2)
{
if(leapYear(year) == 0)
numDays = 28;
else
numDays = 29;
}
else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
numDays = 31;
else
numDays = 30;
printMonth(y , numDays);
system("PAUSE");
return 0;
}
int DayOfWeek(int year, int month)
{
int CoEcentury,CoEmonth, leapStatus, sum, a;
CoEcentury = CenturyTable(year);
leapStatus = leapYear(year);
CoEmonth = MonthTable(month, leapStatus);
sum = CoEcentury + CoEmonth + 1;
a = sum%7;
return a;
}
int CenturyTable(int year)
{
int century, century1, year1,year2,result;
century = year/100;//to get the 1st 2 digits of the year eg. 1970=19
//printf("year/100=%d\n",century);
year1 = year%100;//to get the last 2 digits of the year eg. 1970=70
//printf("year%100=%d\n",year1);
year2 = year1 + (year1/4);//year* table formula
//printf("year + (year/4)=%d\n",year2);
century1 = 2*(3 - (century%4));//century table formmula
//printf("centurytable=%d\n",century1);
result = century1 + year2;//answer of century + year
//printf("result=%d\n",result);
return result;
}
int MonthTable(int month,int leapStatus)
{
int a;
if((month==1 && leapStatus==0)||month==10)
a = 0;
else if(month==3||month==11||(month==2 && leapStatus==0))
a = 3;
else if(month==4||month==7||(month==1 && leapStatus==1))
a = 6;
else if(month==5)
a = 1;
else if(month==6)
a = 4;
else if(month==8||(month==2 && leapStatus==1))
a = 2;
else if(month==9||month==12)
a = 5;
return a;
/*
January 0 (in leap year 6)
February 3 (in leap year 2)
March 3
April 6
May 1
June 4
July 6
August 2
September 5
October 0
November 3
December 5 */
}
int leapYear(int year)
{
int mod4, mod100, mod400, leap;
mod4 = year%4;
mod100 = year%100;
mod400 = year%400;
if((mod4==0&&mod100!=0)||(mod400==0))
leap = 1;//leap year
else
leap = 0;//not a leap year
return leap;
}
void printMonth (int startDay, int days)
{
int weekDay, dayCount;
printf("\t\tSun Mon Tue Wed Thu Fri Sat\n");
printf("\t\t--- --- --- --- --- --- ---\n");
printf("\t\t");
for (weekDay = 0; weekDay < startDay; weekDay++)
printf(" ");
for (dayCount = 1; dayCount <= days; dayCount++)
{
if (weekDay > 6)
{
printf("\n\t\t");
weekDay = 1;
}
else
weekDay++;
printf("%3d ",dayCount);
}
printf("\n\t\t--- --- --- --- --- --- ---\n");
return 0;
}