Modern Fortran sample code

I know there are mixed feelings about it, but long descriptive names make the code more readable in some regions, but then become ugly in dense formulas. ASSOCIATE lets you
have the best of both worlds; and is a “modern” feature that might be demonstrated here.

program mandel
    implicit none
    integer, parameter :: N = 99  ! smaller than half of terminal width
    character(3) :: N_str
    complex :: coordinates(N,N) 
    complex :: values_for_iteration(N,N)
    integer :: i, j
    coordinates = 0.0
    values_for_iteration = 0.0

    associate(z=>values_for_iteration,c=>coordinates)
     do i = 1, N
         c(:, i) = c(:, i) + [ ( cmplx(3.0* j / (N-1) - 2, 0.), j=0,N-1) ]
         c(i, :) = c(i, :) + [ ( cmplx(0.0, 3.0*j / (N-1) - 1.5),j =0,N-1) ]
     end do
     do i = 1, 100
         z = z**2 + c
     end do
    end associate

    write(N_str, "(i3)") N
    print "("//trim(N_str)//"(a))", merge('##', '  ', abs(values_for_iteration)<2)
end program mandel
5 Likes