! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

! Example 4.8. [File output.] Simple harmonic motion
!  (coupled first-order differential equations).
  program E08
  implicit none
  real :: H, T, X, V
  real, parameter :: Omega = 0.5
  integer :: Steps, Loop
! start program E08
    open (unit = 2, file = "e08.zzz", status = "new", action = "write")
    do
      write (unit = *, fmt = *) " Please enter Time step (or 0 to quit). "
      read (unit = *, fmt = *) H
      if (H <= 0) then
        stop
      end if
      write (unit = *, fmt = *) " Please enter initial values for T, X, and V. "
      read (unit = *, fmt = *) T, X, V
      do
        write (unit = *, fmt = *) " How many more steps? "
        write (unit = *, fmt = *) " Enter zero to exit. "
        read (unit = *, fmt = *) Steps
        if (Steps <= 0) then
          exit
        end if
        do Loop = 1, Steps
          T = T + H
          X = X + H * V
          V = V - H * Omega ** 2 * X
          write (unit = 2, fmt = *) T, X, V
        end do
      end do
    end do
    stop
  end program E08
