John Horton Conway, a mathematician who worked at Princeton University, died of COVID-19 in April 2020 at the age of 82. In addition to inventing the Game of Life, his "Doomsday" rule is credited with computing the day of the week for any given date.

Here is an example of a dialog for the program I am asking you to create (user input is in boldface):

Provide a month: January
Provide a day: 1
Provide a year: 2021
January 1, 2021 was a Friday!

In addition to correctly calculating a day of the week answer, your program needs to also handle errors as shown below:

Provide a month: January january
Provide a day: 2
Provide a year: 2020
Invalid month!
Provide a month: February
Provide a day: -10
Provide a year: 2000
Invalid day!
Provide a month: March
Provide a day: 3
Provide a year: -101
Invalid year!
Provide a month: -101
Provide a day: -101
Provide a year: -101
Invalid month!

These are the data validation rules I want your program to enforce:

  • the first letter of the month must be capitalized
  • the day must be between 1 and 31
  • the year must be between 1 and 3000

Your program must gather the month, day and year, in that specific order. However, if you detect an error in an item, you do not have to request or get the remaining items if you don't want to; just be sure you write to cout the required error message and nothing more after that. If instead you choose to gather all input first before checking for any errors, and you detect more than one error, then write only the error message for the first erroneous input item as the last example shows.

You can test your understanding of this program's requirements by experimenting with the calculator we found at this website. http://www.howardstahl.com/ucla/winter21/project2

The algorithm your program should compute is documented below by working with a Month Code, Leap Year Offset (if applicable), Century Code, Year Code and Day Code. After describing the procedure, an example is shown below.

Month Code

determine a month code with the following table:

Month Month Code Month Month Code Month Month Code Month Month Code
January 1 February 4 March 4 April 0
May 2 June 5 July 0 August 3
September 6 October 1 November 4 December 6

Leap Year Offset

determine if a Leap Year Offset of -1 needs to be added to the Month Code using the following guidelines:

if the year entered by the user is a leap year and if the month entered by the user is January or February, add -1 to the month code to adjust for the leap year. (A leap year occurs in years that are evenly divisible by 4. However, years that are evenly divisible by 100 are not leap years with the exception that years evenly divisible by 400 are leap years. In other words, 1996 is a leap year, 2000 is a leap year, 2004 is a leap year. 1900 is not a leap year, 1800 is not a leap, 2001 is not a leap year).

Century Code

determine a century code with the following table (the division specified below should be int division):

Century Value = the year value entered by the user divided by 100 modulo 4

Century Value Century Code Century Value Century Code Century Value Century Code Century Value Century Code
0 -2 1 3 2 1 3 -1

Year Code

determine a Year Code using the following formula:

Year Code = the year value entered by the user modulo 100 divided by 4 plus the year value entered by the user modulo 100. (Again, the division specified here should be int division)

Day Code

determine a Day Code using the following formula:

Day Code = ( Century Code + day value entered by the user + Year Code + Month Code ) modulo 7

Day Of The Week

based on the Day Code, determine a day of the week using the following table:

0 Sunday
1, -6 Monday
2, -5 Tuesday
3, -4 Wednesday
4, -3 Thursday
5, -2 Friday
6, -1 Saturday

Based on all this information, let's take an example. For example, consider January 1, 2021 which was a Friday.

  • the Month Code value for January is 1
  • 2021 is not a leap year so there is no Leap Year Offset adjustment needed
  • the century value is (2021 / 100) % 4 = 20 % 4 = 0 which leads to a Century Code of -2
  • the Year Code value is (2021 % 100) / 4 + (2021 % 100) = (21 / 4) + 21 = 5 + 21 = 26
  • the Day Code is (Century Code + day entered + Year Code + Month Code) = (-2 + 1 + 26 + 1) % 7 = (26 % 7) = 5 which means Friday! Tah-Dah!

Here is an example. For example, consider January 1, 2000 which was a Saturday.

  • the Month Code value for January is 1
  • 2000 is a leap year so in January or February there is a Leap Year Offset adjustment needed of -1. So the Month Code we need to use here is 0.
  • the century value is (2000 / 100) % 4 = 20 % 4 = 0 which leads to a Century Code of -2
  • the Year Code value is (2000 % 100) / 4 + (2000 % 100) = (0 / 4) + 0 = 0
  • the Day Code is (Century Code + day entered + Year Code + Month Code) = (-2 + 1 + 0 + 0) % 7 = (-1 % 7) = -1 which means Saturday! Tah-Dah!

Your program must collect the information for one day in the manner indicated by the examples, and then write to cout exactly one line in a format required below. Our grading tool will judge the correctness of your program by examining your output. Your program's output must be in one of the following four forms; the text must be identical to what is shown (except that the italicized portions described below ):

  • If the user enters a month value which is not "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" or "December": Invalid month!
  • If the user enters a day value less than 1 or greater than 31: Invalid day!
  • If the user enters a year value less than 1 or greater than 3000: Invalid year!
  • Otherwise: mmmm dd, yyyy was a wwww! where mmmm is month value that the user entered originally, dd is the day value that the user entered originally, yyyy is the year value that the user entered originally and wwww is the day of the week your program calculated.

Your program's output must not start with any spaces. If you are not a good speller or typist, or if English is not your first language, be especially careful about duplicating the messages exactly . Here are some foolish mistakes that may cause you to get no points for correctness on this project, no matter how much time you put into it:

  • Writing any extra spaces on the output line.
  • Writing more than one line of output. Don't, for example, add a gratuitous "Thank you for using my great day of the week calculator!"
  • Writing lines to cerr instead of cout .
  • Writing lines like these:
January 1, 2020 was a Friday missing exclamation
January 1, 2020 was a Frday! misspelling
January 1, 2020 was a friday! wrong capitalization
January 1, 2020 was a Friday ! extra spaces
January 1 2020 was a Friday! no comma

Your program must gather the month, day and year, in that specific order. However, if you detect an error in an item, you do not have to request or get the remaining items if you don't want to; just be sure you write to cout the required error message and nothing more after that. If instead you choose to gather all input first before checking for any errors, and you detect more than one error, then write only the error message for the first erroneous input item.

You will not write any loops in this program. This means that each time you run the program, it handles only one date calculation. It also means that in the case of bad input, you must not keep prompting the user until you get something acceptable; our grading tool will not recognize that you're doing that.

The correctness of your program must not depend on undefined program behavior. Your program could not, for example, assume anything about n 's value at the point indicated:

int main()
{
int n;
int m = 42 * n; // n's value is undefined
...
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.