Hi all,
Slowly working through the example problems in a Fortran book (an old one D Monro from 1981).
This is all just for fun, but would like comments on how I could have done things differently.
The problem is to calculate the day of the week (Mon, Tues, Wed, etc) from a date which the user is asked to enter.
(I got really stuck, with my days off by 1, since I’m so used to arrays starting at 0 and not 1. That kept me busy for a while. )
Cheers,
Bob.
program dayoftheweek
implicit none
! D Monro
! Computing with Fortran IV (1981 reprint)
! Problem 2.12
! 31 Dec 1899 was a Sunday.
! Write a program to find the day of the week for any date in the
! 20th century.
!
! www.timeanddate.com/date/durationresult.html used to check results.
!
! 31 Dec 1899 was a Sunday
! Leap year if year mod 4 == 0
! Century is leap year if century mod 400 == 0; 1900 not, 2000 yes
! Jan(01) 31 Feb(02) 28 Mar(03) 31 Apr(04) 30 May(05) 31 Jun(06) 30
! Jul(07) 31 Aug(08) 31 Sep(09) 30 Oct(10) 31 Nov(11) 30 Dec(12) 31
integer :: n
integer :: date_in_month
integer :: month_in_year
integer :: year, track_year
integer :: days_in_year
integer, dimension(12) :: days_in_month
character(len=3), dimension(7) :: days_in_week
integer :: start_year, start_month, start_day
integer :: zeroday ! value of 0 is Sunday, see days_in_week below.
integer :: acc_days ! accumulated days
days_in_month=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
days_in_week=[character(len=3) :: 'SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT']
acc_days = 0
days_in_year = 0
do n = 1, 12
days_in_year = days_in_year + days_in_month(n)
end do
start_year = 1899
start_month = 12
start_day = 31
zeroday = 0
print *, 'Calculate the weekday for an entered data DD, MM, YYYY:'
print *, ' Enter date in the month (DD)'
read *, date_in_month
if ((date_in_month < 1) .OR. (date_in_month > 31)) then
print *, 'Error. DD not in range 1..31'
stop
end if
print *, ' Enter month in the year (MM)'
read *, month_in_year
if ((month_in_year < 1) .OR. (month_in_year > 12)) then
print *, 'Error. MM not in range 1..12'
stop
end if
print *, ' Enter year (YYYY)'
read *, year
if (year .LE. 1899) then
print *, 'Error. YYYY to be > 1899'
stop
end if
!
! YEAR section
!
n = start_year + 1
acc_days = 0
do while (n .NE. year)
acc_days = acc_days + days_in_year
print *, ' Yeaar section: Added ', days_in_year, ' for year ', n
if (mod(n, 4) .EQ. 0) then ! leap year
if (mod(n, 400) .NE. 0) then ! leap century
acc_days = acc_days + 1 ! add in the leap day
print *, ' Year section: Added leap day'
end if
end if
n = n + 1 ! increment year index
end do
track_year = n
!
! MONTH section
!
n = 1
do while (n .NE. month_in_year)
acc_days = acc_days + days_in_month(n)
print *, ' Month section: ', days_in_month(n), ' day for month ', n
n = n + 1 ! increment month index
end do
!
! DAY section
!
acc_days = acc_days + (date_in_month - 1)
print *, ' Day section: Added ', date_in_month-1, ' days to end.'
if ( (mod(track_year, 4).EQ.0) .AND. (month_in_year .GT. 2)) then ! leap year
if (mod(track_year, 400) .NE. 0) then ! leap century
acc_days = acc_days + 1 ! add in the leap day
print *, ' Day section: Added leap day'
end if
end if
print *, ' Debug: ACC_DAYS = ', acc_days
zeroday = zeroday + mod(acc_days, 7)
n = acc_days - ((acc_days / 7) * 7)
print *, 'User day lands on ', days_in_week(zeroday+1)
print *, ' MOD days is ', mod(acc_days, 7)
print *, ' Integer day is ', n
end program dayoftheweek