Project Task #4: Determine the day of the week for a given date.
COMMENT: 1 January 1900 was a Monday. Knowing the date of your birthday, what day of the week was it?
Let W stand for a weekday, and think of the weekdays numbered as follows:
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday
Use 1 January 1900 as the starting date, and record this as D1 / M1 / Y1 = 1 / 1 / 1900 with W1 = 1.
Assume your birthday is D2 / M2 / Y2, and we want to work out W2.
Outline #1:
Start at Monday, 1 January 1900.
The next day is Tuesday, 2 January 1900.
and just count your way up.
Outline #2:
Initialize D / M / Y to D1 / M1 / Y1 and W to W1.
Until you reach the desired date
Increase the weekday by 1
if the weekday is 7, reset it to 0
Increase the day by 1
If the day number is too big for the month
set the day number back to 1
increase the month
if the month is more than 12
set the month to January
increase the year
You will need function value = year_is_leap(y) which returns true if year y is a leap year. Use the following logic to design your function:
if year is not divisible by 4 then it is not a leap year
but if year is not divisible by 100 then it is a leap year
but if year is not divisible by 400 then it is not a leap year
otherwise it is a leap year
You will need function days = month_length(m,y) which returns days, the number of days in month m, in year y.
function days = month_length ( m, y )
day_list = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
days = day_list ( m );
if the month is February
if the year is leap
days = 29
Test your year_is_leap() and month_length() functions first, before trying the whole program!
INSTRUCTIONS:
function w = weekday ( D2, M2, Y2 )
Initialize D / M / Y to D1 / M1 / Y1 and W to W1.
WHILE ( D ~= D2 || M ~= M2 || Y ~= Y2 )
Increase W by 1.
Replace W by mod ( W, 7 ).
Increase D by 1.
If D is greater than the number of days in month M
D = 1
M = M + 1
if M is greater than 12
M = 1
Y = Y + 1
end
end
end
return
end
CHECK: 25 August 1944 is a Friday. In other words,
w = weekday ( 25, 8, 1944 )
should return w = 5.
SUBMIT: Your work should be stored in a script file called "p4.m", and should begin:
% p4.m
% YOUR NAME
% This script (describe what it does)