Silverfrost Forums

Welcome to our forums

array constructor query

23 Jan 2025 11:16 (Edited: 30 Jan 2025 12:35) #31844

Hello

Can I just check if this array constructor , using square brackets is valid. Both methods work. Could not see documentation in the ftn95 help, is this part of 2003?

module array_flipping
  implicit none
contains

  ! Function to flip an array up-down
  function flipud(array) result(flipped)
    implicit none
    real, intent(in) :: array(:,:)
    real :: flipped(size(array,1), size(array,2))
    integer :: i, j, rows, cols

    rows = size(array, 1)
    cols = size(array, 2)

    do i = 1, rows
      do j = 1, cols
        flipped(i, j) = array(rows - i + 1, j)
      end do
    end do
  end function flipud

  ! Function to flip an array left-right
  function fliplr(array) result(flipped)
    implicit none
    real, intent(in) :: array(:,:)
    real :: flipped(size(array,1), size(array,2))
    integer :: i, j, rows, cols

    rows = size(array, 1)
    cols = size(array, 2)

    do i = 1, rows
      do j = 1, cols
        flipped(i, j) = array(i, cols - j + 1)
      end do
    end do
  end function fliplr

end module array_flipping

program test_flipping
  
  use array_flipping
  
  implicit none

  real :: array(3,3)
  real :: flipped_ud(3, 3), flipped_lr(3, 3)
  integer :: i

  array=reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3])
  !array=reshape((/1, 2, 3, 4, 5, 6, 7, 8, 9/), (/3, 3/))

! For a 3x3 array dim1 = dim2
  print *, 'Original Array:'
  do i = 1, size(array,1)
    write(*, '(3F10.5)') array(i, :)
  end do

  flipped_ud = flipud(array)
  print *, 'Flipped Up-Down:'
  do i = 1, size(array,1)
    write(*, '(3F10.5)') flipped_ud(i, :)
  end do

  flipped_lr = fliplr(array)
  print *, 'Flipped Left-Right:'
  do i = 1, size(array,1)
    write(*, '(3F10.5)') flipped_lr(i, :)
  end do
  
end program test_flipping

Testing functions to replicate the Matlab/Scilab style flipup/fliplr.

Lester

23 Jan 2025 11:37 #31847

Google AI says square brackets were introduced in Fortran 2003.

23 Jan 2025 11:44 #31848

Good to know Paul

Please login to reply.