15.9: Exercises
- Page ID
- 54365
\( \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.
- What are the two types of Fortran subprograms?
- How many values does a user-defined function typically return?
- In the function call,
ans = power(x, y)
, what arex
andy
are called? - In the function heading,
integer function power(a, b)
, what area
andb
are called? - In the function,
integer function power(a, b)
, what is the type of the value returned? - Is it possible to pass integer arguments to a real function (yes/no)?
- The subprogram section (where functions and subprograms are defined) is marked by what keyword?
- What is the output of the following function:
integer function power(a, b) integer, intent(in) :: a, b power = a ** b return end function power
- with the input of \(a = 2\) and \(b = 3\)?
- with the input of \(a = 3\) and \(b = 2\)?
- Given the following program?
program quiz implicit none real :: temp=80.0, temp1=50.0, temp2 write (*, '(f5.2, 2x, f5.2)') temp, temp1 temp2 = fahrToCelsius(temp1) write (*, '(f5.2, 2x, f5.2)') temp, temp2 contains real function fahrToCelsius(temp) real, intent(in) :: temp fahrToCelsius = (temp – 32.0) / 1.8 return end function fahrToCelsius end program quiz
- What is the name of the function?
- Is the above program correct (yes/no)?
- Does the variable
temp
in the main and the variabletemp
in the function refer to the same value? - What is the output?
- What is meant by the term variable scope?
- What is the correct intent for the following situations:
- A variable passed to a function that will not be changed in the function.
- A variable that will be set in the subroutine and returned. No value is passed in.
- A variable that will be passed into a subroutine, updated, and returned back to the calling routine.
- What is a meant by the term side-effect?
Suggested Projects
Below are some suggested projects.
- Type in the dice game program example, compile, and execute the program. Test the program by playing it for a series of rounds. Ensure the scoring is correct.
- Write a main program and an integer Fortran function,
gSeries()
, to compute the following geometric series:\[ g = \sum_{n=0}^{n-1} x^n = 1 + x + x^2 + x^3 + \dots + x^{(n-1)} \nonumber \]The arguments for the call, in order, are as follows: \(n\) (integer value). The function should return an integer result (of the formula based on the \(n\) value). The main should call the function with several different values. - Write a main program and a real function,
harmonicMean()
, to compute the harmonic mean of a series of real numbers. The real numbers are pass to the function in an array along with the count.\[ \textit{harmonic mean} = \frac{N}{\left( \frac{1}{x_1} + \frac{1}{x_2} + \ldots + \frac{1}{x_N} \right)} \nonumber \]The arguments for the call, in order, are as follows; array of numbers (with count real values), count (integer). The function should return a real result (of the formula). The main should call the function with several different values. - Write a main program and a subroutine,
CircleStats()
, that, given an array containing a series of circle diameter's (real values), will compute the area of each circle in a series of circles and store them into a different array (real values). The subroutine should also compute the real average of the circle areas. The arguments for the call, in order, are as follows; circle diameter's array (count real values), circle areas array (count real values), count (integer), areas average (real). The main program should declare the array and initialize the array with a series of values for circle areas. The program results should be verified with a calculator. - Write a main program and a subroutine,
ReadCoord()
, to read an \((x, y, z)\) coordinate from the user. The subroutine must prompt for and read \((x, y, z)\) and ensure that the \(x\), \(y\), and \(z\) values are between 0 and 100 (inclusive). The values may be prompted for and read together, but the prompt should leave the cursor on the same line. The subroutine should re-prompt for all three if the input data is not correct. If the user provides valid data, the \((x, y, z)\) values should be returned with a logical for valid data set to true. If the user does not provide valid data entry after three tries, the subroutine should display an error message and a set the logical for valid data to false. The arguments for the call, in order, are as follows; \(x\) value (integer), \(y\) value (integer), \(z\) value (integer), and valid data flag (logical value). The main program should call the subroutine three times and display the results for each call. - Write a main program and a subroutine,
Stats()
, that, given an array containing a series of numbers (real values), will find and display the following real values; minimum, median, maximum, sum, and average. The display must use a formatted write(). The real values will not exceed 100.0 and should display three digits decimal values (i.e., \(nnn.xxx\)). The arguments for the call, in order, are as follows; array of numbers (count real values), count (integer). The main program should populate the array with random numbers and call the subroutine.