Vorige Seite Kein Verweis Übersicht Kein Verweis content index Hilfeseiten  

Bestimmung des Maximums und Minimums einer Zahlenfolge

Programm zur numerischen Berechnung des Maximums und des Minimums einer Folge von n Zahlen.
module mysubs

contains

  subroutine init(x)
    use kinds
    use fileutil
    implicit none
    real(kind=REAL8), dimension(:), pointer :: x
  
    integer, parameter  :: fileId = 1
    character(len=256)  :: fileName
    integer             :: anzahl, status

    write (*,*) "Auffinden der Extrema von n Zahlen"
    write (*,*)
    write (*,*) "Welche Datei enthaelt Zahlen? (Beispiel in minimax.dat .)"
    read (*,*) fileName
  
    open(unit=fileId, file=fileName, action='read', iostat=status)
    if (status /= 0) then
       write (*,*) 'Kann Datei ', fileName, 'nicht öffnen.'
       stop
    end if

    anzahl = anzahlReals(fileId)
    allocate(x(anzahl))

    read(fileid, *, iostat=status) x(1:anzahl)
    if (status /= 0) then
       write (*,*) 'Konnte Array nicht lesen.'
       stop
    end if

    close(unit=fileid)

    write (*,*) "Insgesamt wurden ", anzahl, " Zahlen gelesen."
    return
  end subroutine init

end module mysubs


program MiniMax
  ! Auffinden der Extrema von n Zahlen
  
  use kinds
  use mysubs
  implicit none

  real(kind=REAL8), dimension(:), pointer :: x
  real(kind=REAL8)                        :: xmax, xmin

  call init(x)
  xmax = maxval(x)
  xmin = minval(x)

  write (*, '(A, F10.2, A, F10.2)') 'Maximum: ', xmax, ', Minimum: ', xmin
  stop
end program MiniMax