Write a program that inputs a date and outputs the day of the week for that date
ID: 2989692 • Letter: W
Question
Write a program that inputs a date and outputs the day of the week for that date. Your program should work as follows: Enter the year (1801 .. 2199) : 2007 Enter the month number (January = 1): 1 Enter the day (1 .. 31) : 25 Thursday The program should work for dates from January 1, 1801 to December 31, 2199. You do not have to reject values like February 31, 2007 which are potentially legal values but do not represent actual dates.
In your program you may assume Year, Month, and Day are the input
values with January = 1, February = 2, etc.
Calculate the value of variable D that will contain the day of the week,
encoded with Sunday = 1, Monday =2, etc.
Many algorithms use integer division. In Matlab, using the floor function, we can simulate the behavior of standard integer division.
Use floor to determine the century (C) from the Year:
C = floor (Year/100)
You will find useful Matlab mod function that performs standard modulo division.
Use an array to output the text matching the correct day. Create an array with one entry for each day of the week, and display item D from
that array.
Pick a few well-chosen values and test your program. Try dates around the first of the year and the end of February, and dates which are easy to verify are correct (today for example).
Explanation / Answer
Year = input('Enter the year (1801 .. 2199) ');
Month = input('Enter the month number (January = 1) ');
Day = input('Enter the day (1 .. 31) ');
week = cellstr(["Sunday";"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday"]);
C = floor(Year/100);
L = floor(Year/4) - floor((3*C +3)/4);
C1 = (Year -1)/100;
Leap = L - floor((Year -1)/4) + floor((3*C1 + 3)/4);
Adj = 1 - floor((Month + 9)/12);
M = floor((13*Month + 3 + 17*Adj)/5);
M = M - (Leap +1)*Adj;
D = pmodulo((Year + L + M + Day + 1),7) +1;
disp(week(D).entries);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.