Skip to content
Fortran

Arrays et Opérations Vectorielles

Créer et opérer sur des arrays en Fortran moderne.

#array#vector

Code

fortran
program arrays
  implicit none

  ! 1D array
  real, dimension(10) :: a
  real :: b(5) = [1.0, 2.0, 3.0, 4.0, 5.0]

  ! 2D array
  real :: m(3, 4)

  ! Array constructor
  a = [(real(i), i=1, 10)]  ! 1.0, 2.0, ..., 10.0

  ! Whole-array operations (vectorized)
  a = a * 2.0
  a = a + b  ! must conform (here both size 10... adjust)

  ! Intrinsic functions
  print *, sum(b)        ! 15.0
  print *, maxval(b)     ! 5.0
  print *, size(b)       ! 5
  print *, sum(b, mask=b>2)  ! 12.0

  ! Array sections
  print *, b(2:4)        ! [2.0, 3.0, 4.0]
  print *, b(::2)        ! [1.0, 3.0, 5.0] (stride 2)
end program