Project 1
Write a program to compute the day of the week for any date of the Gregorian calendar. Here is the formula:
W=(C+Y+L+M+D ) mod 7
Y is the last two digits of the actual year and D is the actual day. You need to obtain the value of C from the following rule for the years:
- If year is in the 1400s, 1800s, 2200s, C=2
- If year is in the 1500s, 1900s, 2300s, C=0
- If year is in the 1600s, 2000s, 2400s, C=5
- If year is in the 1700s, 2100s, 2500s, C=4 Months are numbered from 1 in the usual way, but (from January) M is 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5
The only tricky part of this algorithm is L, the number of leap days that have occurred since the beginning of the century of the given date. To obtain this:
- Integer divide the last two digits of the year by 4 to obtain the number of “ordinary” leap years in the century up to that year, not counting the century year itself if applicable.
- Obtain the remainder of the two digits and 4. If it is not a century year and the remainder is 0 the year is a leap year, otherwise it is not. If the year itself is a century year see Step 3.
- If the century (1400, 1500, etc.) was evenly divisible by 400 then the century year is a leap year, otherwise it is not. Thus 2000 was a leap year but 1900 was not. So add 1 for centuries divisible by 400 and 0 otherwise.
- If your date is January 1-February 29 of a leap year, subtract 1. Try to devise a method to obtain the last two digits on your own. Print the day of the week as a word (Monday, Tuesday, etc.). Remember that Sunday is the first day of the week and it will be counted as 0 in this algorithm.
Test your program first with your own birthdate. Then test with the following dates:
- Today’s date
- December 25, 1642 (Note: this is Newton’s birthdate in the Julian calendar, but use it as a Gregorian date)
- October 12, 1492
- January 20, 2000
- December 11, 2525
Try to write and test your own program before peeking at the sample solution.
Sample solution
#include <iostream>
using namespace std;
/*******************************************************************************
! This program computes the day of the week given a date in the
! Gregorian calendar.
!
! Author: K. Holcomb
! Changelog: 2013-06-05 Initial version
! 2014-01-29 Bug fix to correct leap_year test for century
! 2014-01-29 Added a loop for user input
! 2015-01-29 Modification to use only conditionals (no arrays)
*******************************************************************************/
int main() {
int maxYear, minYear;
int day, month, year, century;
int D, M, Y, C, L, W;
bool leapYear, centuryLeapYear;
day=2;
month=3;
year=2016;
D=day;
century=100*(int(year)/100);
Y=year-century;
centuryLeapYear=(century%400)==0;
leapYear=false;
if (Y>0) {
leapYear=(year%4)==0;
}
else if (centuryLeapYear) {
leapYear=true;
}
L=int(Y)/4;
if (centuryLeapYear) L+=1;
if (leapYear && month<3) L-=1;
if (month==1 || month==10) {
M=0;
}
else if (month==2 || month==3 || month==11) {
M=3;
}
else if (month==4 || month==7) {
M=6;
}
else if (month==5) {
M=1;
}
else if (month==6) {
M=4;
}
else if (month==8) {
M=2;
}
else {
M=5;
}
if ( century==1400 || century==1800 || century==2200) {
C=2;
}
else if ( century==1500 || century==1900 || century==2300) {
C=0;
}
else if ( century==1600 || century==2000 || century==2400) {
C=5;
}
else if ( century==1700 || century==2100 || century==2500) {
C=4;
}
else {
cout<<"This algorithm doesn't cover the century requested.\n";
}
W=(C+Y+L+M+D)%7;
switch (W) {
case(0):
cout<<"Sunday\n";
break;
case(1):
cout<<"Monday\n";
break;
case(2):
cout<<"Tuesday\n";
break;
case(3):
cout<<"Wednesday\n";
break;
case(4):
cout<<"Thursday\n";
break;
case(5):
cout<<"Friday\n";
break;
case(6):
cout<<"Saturday\n";
break;
case(7):
cout<<"Sunday\n";
break;
}
return 0;
}