Vorige Seite Kein Verweis Übersicht Kein Verweis content index Hilfeseiten  

Matrixaddition

Programm zur Addition zweier Matrizen.
module mysubs

contains

  subroutine init(a, b, c)
    use kinds
    implicit none
    real(kind=REAL8), dimension(:,:), pointer :: a, b, c
    
    integer, parameter  :: fileid = 1
    character(len=256)  :: fileName
    integer             :: status
    integer             :: n, m, i
    
    write (*,*) 'Darstellung der Addition zweier Matritzen'
    write (*,*)
    write (*,*) 'In welcher Datei liegen die Matrizen (Beispiel in matrix.dat)?'
    read (*,*) fileName

    open(unit=fileid, file=fileName, action='read', iostat=status)
    if (status /= 0) then
       write (*,*) 'Konnte Datei ', fileName, 'nicht öffnen.'
       stop
    end if

    read (fileid, *, iostat=status) m, n
    if (status /= 0) then
       write (*,*) 'Konnte Dimension nicht lesen.'
       stop
    end if

    allocate(a(m, n))
    allocate(b(m, n))
    allocate(c(m, n))

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

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

    close(unit=fileid)
    return
  end subroutine init

  subroutine result(c)
    use kinds
    implicit none
    real(kind=REAL8), dimension(:,:), pointer :: c
    
    integer           :: i, n, m
    character(len=80) :: format
    
    m = size(c, 1)
    n = size(c, 2)
    write (format, '(A,I4,A)')   '(', n, '(F12.3," "))'
    write (*, format) (c(i,1:n), i=1,m)
    return
  end subroutine result

end module mysubs


program Matrixaddition
  ! Addition zweier Matritzen
 
  use kinds
  use mysubs
  implicit none

  real(kind=REAL8), dimension(:,:), pointer :: a, b, c

  call init(a, b, c)
  c = a + b
  call result(c)
  stop
end program Matrixaddition