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}}\)
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.