10.9: Example
- Page ID
- 54293
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)This example will read a date from the user (month, day, and year, on the same line), determine the day of week (for that month/day/year). Then, the program will display the original input date (numeric form) and the formatted date. The original input date will be displayed with the applicable fields including the leading 0's (i.e., 01/01/2010).
Understand the Problem
For this problem, we will read the three numbers for the date from the user. The verification of the date information is left as an exercise.
To calculate the day on which a particular date falls, the following algorithm may be used (the divisions are integer divisions):
a = (14 - month) / 12 y = year - a m = month + 12*a - 2 daynum = [ date + y + y/4 - y/100 + y/400 + (31*m/12) ] mod 7
The value of daynum
is 0 for a Sunday, 1 for a Monday, 2 for a Tuesday, etc.
Create the Algorithm
For this problem, first we will need to read the date. The verification of the date entered and error checking is left as an exercise. Then, the original input date can be displayed, in numeric form, formatted appropriately. For a date, this would mean two digits for the month, a “/”, two digits for the day, a “/”, and four digits for the year. When the day is only one digit, for example 5, it is customary to display a “05” so the program will ensure this occurs.
! declare variables ! integer -> month, day, year ! integer -> a, y, m, daynum (for calculations) ! display initial header ! prompt for month, day, and year ! read month, day, and year ! display formatted numeric month/day/year
Then the program can calculate the day of the week (based on the formula) and convert the resulting number (0-6) into a date string and display the result.
! calculate day of week ! convert day of week (0-6) to string ! convert month (1-12) to string ! display formatted string for day, month, and year
For convenience, the steps are written as program comments.
Implement the Program
Based on the algorithm, the below program could be created.
program dateFormatter ! declare variables implicit none integer :: month, day, year integer :: a, m, y, d character(9) :: amonth, day_of_week ! ---------- ! display initial header write (*,*) "Date Formatting Example" ! prompt for month, day, and year write (*,'(a)',advance="no") "Date (month, day, year):" ! read month, day, and year read (*,*) month, day, year ! ---------- ! display formatted numeric month/day/year write (*,*) "----------------------------------------" write (*,*) "Input Date: " write (*,'(5x, i2.2, a, i2.2, a, i4)') month, "/", & day, "/", year ! ---------- ! calculate day of week a = (14 - month) / 12 y = year - a m = month + 12 * a - 2 d = mod ( (day + y + y/4 - y/100 + y/400 + (31*m/12)), 7) ! ---------- ! convert day-of-week integer to day-of-week string select case (d) case (0) day_of_week = "Sunday " case (1) day_of_week = "Monday " case (2) day_of_week = "Tuesday " case (3) day_of_week = "Wednesday" case (4) day_of_week = "Thursday " case (5) day_of_week = "Friday " case (6) day_of_week = "Saturday " end select ! ---------- ! convert month (1-12) to string select case (month) case (1) amonth = "January " case (2) amonth = "February " case (3) amonth = "March " case (4) amonth = "April " case (5) amonth = "May " case (6) amonth = "June " case (7) amonth = "July " case (8) amonth = "August " case (9) amonth = "September" case (10) amonth = "October " case (11) amonth = "November " case (12) amonth = "December " end select ! ---------- ! display formatted string for day, month, and year write (*,'(/a)') "Formatted Date:" write (*,'(5x, a, a, a, 1x, i2.2, a, i4/)') & trim(day_of_week), ", ", trim(amonth), & day, ", ", year end program dateFormatter
The spacing and indentation is not required, but help to make the program more readable. The trim()
intrinsic function removes any trailing spaces from the input string. Additional information regarding handling character data types is provided in the following section.
Test/Debug the Program
For this problem, the testing would be to ensure that the output formatting is correct. Since there is no error checking on the input, only correct dates should be entered. Test the program on a series of different input values and verify that the output is correct for those input values.