Skip to main content
Engineering LibreTexts

12.9: Exercises

  • Page ID
    54332
  • \( \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 must occur before a file can be read or written?
    2. What is the range of the valid unit numbers for a file open?
    3. For the following statements:
      integer :: opnstat
      character(20) :: filename="file.txt"
      
      open (14, file=filename, status="old", action="read",
           &            position="rewind", iostat=opnstat)
      
      if ( opnstat > 0 ) then
          write (*, *) "Error, can not open file."
          stop
      end if
      1. What is the name of the file being opened?
      2. What is the unit number that will be used for subsequent write operations?
      3. Does the error message get printed if the file does not exist (yes/no)?
      4. What does it mean when the status variable is > 0?
    4. Assume the file answers.txt exists, has been successfully opened using unit number 20, and contains the following data:
      "line 1 data1=23 data2 =034 data3 =05123"
      What is the read statement required to get data1, data2, and data3 into the integer variables num1, num2, and num3 respectively?

    Suggested Projects

    Below are some suggested projects.

    1. Type in the line numbers program, compile, and execute the program. Test the program on a number of different input values.
    2. Imagine the movement of a small circle that rolls on the outside of a rigid circle. Imagine now that the small circle has an arm, rigidly attached, with a plotting pen fixed at some point. That is a epicycloid, commonly called a Spirograph1. Write a Fortran 95/2003/2008 program to generate the (x,y) coordinates for a Spirograph drawing.
      A spirograph.
      First, the program should prompt for an output file name, read the file name, and open the file. If the file cannot be opened, the program should display an appropriate error message and re- prompt. Next, the program should prompt for a read radius 1 (fixed circle), radius 2 (moving circle), and offset position (rigid arm length). The radius 1, radius 2, and offset position values must be between -100 and +100 (inclusive). If any value is out of range, the program should re- prompt. If the user does not enter a valid file name or number of input values after three tries, the program should terminate with an appropriate error message. That is, three errors are acceptable, but if a fourth error is made, the program should terminate. The prompts and subsequent reads should be on the same line (see example).
      Then, the program should generate (x,y) points based on one the following formulas:\[ x = \left( \left( \textit{radius}_1 + \textit{radius}_2 \right) \cdot \cos \left( \textit{step} \right) \right) + \left( \textit{offsetposition} \cdot \cos \left( \left( \textit{radius}_1 + \textit{radius}_2 \right) \cdot \frac{\textit{step}}{\textit{radius}_2} \right) \right) \nonumber \]\[ y = \left( \left( \textit{radius}_1 + \textit{radius}_2 \right) \cdot \sin \left( \textit{step} \right) \right) + \left( \textit{offsetposition} \cdot \sin \left( \left( \textit{radius}_1 + \textit{radius}_2 \right) \cdot \frac{\textit{step}}{\textit{radius}_2} \right) \right) \nonumber \]The step should start at 0.0 and stop at 360.0, stepping by 0.1. All writes should use a formatted output (not the '*'). Then the program should close the output file and inform the user the plotting is completed. Test the program on a number of different input values.
    3. Write a Fortran program that plays the Chaos Game. To play the Chaos Game, plot 3 points A, B, C, and an arbitrary initial point X1. Then, generate a random number between 1 and 3 representing A (1), B (2), or C (3). If A comes up, plot the midpoint of the line joining X1 to A. If B comes up, plot the midpoint of the line joining X1 to B; the case with C is similar. Call this new point X2. Generate another random number between 1 and 3. Plot the midpoint of the line joining X2 to either A, B or C depending on the random number. Call this new point X3. Repeat this process N times. Test the program on a number of different input values.
      Refer to Appendix C for more information regarding generating random numbers.
      Note, the correct output of this program is shown on the cover page. The image is referred to as a Serpinski Triangle2.

    1. For more information, refer to: http://en.Wikipedia.org/wiki/Spirograph
    2. For more information, refer to: https://en.Wikipedia.org/wiki/Sierpinski_triangle

    This page titled 12.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?