Skip to main content
Engineering LibreTexts

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.

    1. What are the two types of Fortran subprograms?
    2. How many values does a user-defined function typically return?
    3. In the function call, ans = power(x, y), what are x and y are called?
    4. In the function heading, integer function power(a, b), what are a and b are called?
    5. In the function, integer function power(a, b), what is the type of the value returned?
    6. Is it possible to pass integer arguments to a real function (yes/no)?
    7. The subprogram section (where functions and subprograms are defined) is marked by what keyword?
    8. 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
      
      1. with the input of \(a = 2\) and \(b = 3\)?
      2. with the input of \(a = 3\) and \(b = 2\)?
    9. 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
      
      1. What is the name of the function?
      2. Is the above program correct (yes/no)?
      3. Does the variable temp in the main and the variable temp in the function refer to the same value?
      4. What is the output?
    10. What is meant by the term variable scope?
    11. What is the correct intent for the following situations:
      1. A variable passed to a function that will not be changed in the function.
      2. A variable that will be set in the subroutine and returned. No value is passed in.
      3. A variable that will be passed into a subroutine, updated, and returned back to the calling routine.
    12. What is a meant by the term side-effect?

    Suggested Projects

    Below are some suggested projects.

    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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.
    6. 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.

    This page titled 15.9: Exercises is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?