20.3: Exercises
- Page ID
- 54389
\( \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}\)Below are some quiz questions and project suggestions based on this chapter.
Quiz Questions
Below are some quiz questions.
- Can the date alone be obtained (i.e., without obtaining the time) using the
get_date_time()
system service (yes/no)? - When using the
get_date_time()
system service to obtain the integer date and time values, what is the index number for the:- The month value.
- The hour value.
- The year value.
- How can the integer time zone value be converted to hours:minutes format?
- Provide the
get_date_time()
system service call to obtain the current date string into the already declared character variabledStr
. - Provide the
get_date_time()
system service call to obtain the current time string and time zone string into the already declared character variablestimeStr
andzoneStr
. - Provide the
get_date_time()
system service call to obtain the current date/time integer values, and the current date string into the already declared 8 element integervaluesArr
array. - If there are no arguments entered on the command line, what is returned by the
get_command_argument()
system service? - Provide an appropriate statement to obtain the 3rd command line argument, the argument length, and a status value. You may assume the variables for the command line argument (character), named
args
, the length (integer), namedarg3length
, and the status (integer), named,arg3stat
are already declared appropriately.
Suggested Projects
Below are some suggested projects.
- Type in the get date and time example program. Update the output to more clearly display only the current date and time on one line. Remove the unnecessary or redundant output.
- Type in the get command line arguments example program. Execute the program using a variety of different expected and unexpected arguments.
- Develop a program that reads command line arguments for a title and time (hours and minutes). Calculate and display the difference between the current time and the time from the command line. If the command line arguments are incomplete or incorrect, an error message should be displayed and the program, terminated. The program should use 24-hour format. One possible approach to determining the time difference would be to convert each time into minutes (from hours and minutes), compute the difference, and convert the difference in minutes back to hours and minutes. The final output should display the title, the system time, the entered time, and the time difference. All times should be displayed in hours:minutes format.
- Write a program to get the date from the system and display a formatted date. The formatted date should be: <day of week>, <month name> <day>, <year>. For example, 12/2/2013 should be displayed as Monday, December 2, 2013. Each string should include only the appropriate spacing. Specifically, there should be no extra spaces between the words or numbers (including the date value, 2 in the example).
To calculate the day on which a particular date falls, the following algorithm may be used.
Note, all variables are integers and the divisions are integer divisions.\[ a = \frac{14 - \textit{month}}{12} \nonumber \]\[ y = \textit{year} - a \nonumber \]\[ m = \textit{month} + 12 \times a - 2 \nonumber \]\[ \textit{dayNum} = \left[\textit{day} + y + \frac{y}{4} - \frac{y}{100} + \frac{y}{400} + \left(31 \times \frac{m}{12} \right) \right] \textit{mod }7\nonumber \]Where themonth
,day
, andyear
variables are the integer date values (12, 2, and 2013 in the previous example). The final value ofdayNum
is 0 for a Sunday, 1 for a Monday, 2 for a Tuesday, and so forth with the maximum value of 6 for Saturday. - Update the program from the previous question to write the formatted date to a file. The file name should be created based on the current date in the following format “file<MMDDYY>.txt” For example, for a date of 12/02/2013, the file should be named
file120213.txt
and contain the character string Monday, December 2, 2013. - Write a program to obtain the current date and attempt to open a file based on the date in the following format, “file<MMDDYY>.txt”. For example, for a date of 12/02/2013, the file should be named
file120213.txt
. If the file exists, the contents should be displayed to the screen. If the file does not exist, an appropriate error message should be displayed.