Question
C++ Write a program that asks a user to enter a date in month day year format (for example 1012 2016) and displays the day
C++
Write a program that asks a user to enter a date in month day year format (for example 1012 2016) and displays the day of the week for that date, in this case, Wednesday.
Be sure to validate month and day to be sure they are in range. To validate 2/29/yyyy, you will have to call the is Leap year function.
The algorithm discussed here is based upon the article athttp://en.wikipedia.org/wiki/Calculating_the_day_of_the_weekunder the heading: A tabular method to calculate the day of the week
You will require several functions:
Bool is Leap Year(int year);
This function will return true if the year passed is a leap year and false if not. The pseudocode to determine if a given year is a leap year is:
Leap Year = ((year evenly divisible by 400) or ( year evenly divisible by 4 and year NOT evenly divisible by 100))
int get Century Value(int year);
This function should take the first two digits of the year (ie the century), divide by 4 and save the remainder. Subtract the remainder from 3, multiply the result by 2 and return that value. For example the year 2013 becomes (20/4) = 5, remainder 0. 3 0 = 3 return 3 * 2 = 6. Hint, to get the century divide by 100.
Int get Year Value(idnt year);
This function computes a value based on the years since the beginning of the century. First, extract the last two digits of the year (year%100), in this case 13. Next, factor in leap years. Divide the last two digits by 4 and add that back to the result and return that. Eg (13/4) = 3. 3 + 13 = 16.
Int get Month Value(int month, int year);
This function should return a value based on the table below and will require calling the is Leap Year function.
Month | Return (if not leap year) | Return (if leap year) |
1 | 0 | 6 |
2 | 3 | 2 |
3 | 3 | 3 |
4 | 6 | 6 |
5 | 1 | 1 |
6 | 4 | 4 |
7 | 6 | 6 |
8 | 2 | 2 |
9 | 5 | 5 |
10 | 0 | 0 |
11 | 3 | 3 |
12 | 5 | 5 |
Finally, to compute the day of the week, add together the dayand the values returned by getMonthValue, getyearValue, and getCenturyValue. Divide that sum modulo by 7. 0 indicates Sunday, 1 Monday, 2 Tuesday, etc.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started