简体   繁体   中英

Equivalent of enumerate list in Python for Fortran 77

I have written a python code as follows which is working and I would like some help to replicate this code to Fortran 77.

The main issue I am having is an equivalent of enumerate(x) for Fortran 77. I would really appreciate if someone who are used to programming in F77 to help me.

import math

x = [1., -6., 7., -9., 8., 9., 5., 7., -7., 0.]

for i, x[i] in enumerate(x):
  print 'Values of x =', x[i]


y = [3., 6., 3., 7., 1., 7., -2., 0., 3., 7.]

for i, y[i] in enumerate(y):

  print 'Values of y =', y[i]

br  = [1., 2., 8., 0., 7., 3., 6., 9., 0., 2.]

for i, br[i] in enumerate(br):

   print 'corresponding brightness, br = ',br[i]

r_cut = input("Please give me a value of r_cut:  ")

total = 0

for i in xrange(0,10):
    if (x[i]**2 + y[i]**2)**0.5 < r_cut:
        total += br[i]

print 'total brightness = ', total

There is not any equivalent to the python enumerate , but in your case you can use a simple do loop, by the way I'm writing it in F90, that is some features doesn't belong to F77, on the other and almost all compilers support modern Fortran.

program test
implicit none
integer :: i
real :: x(10), y(10), br(10), total, r_cut

r_cut = 8.0

x = [1., -6., 7., -9., 8., 9., 5., 7., -7., 0.]
do i = 1, size(x)
   print *, 'Value of x is:', x(i)
enddo

y = [3., 6., 3., 7., 1., 7., -2., 0., 3., 7.]
print *,'Value of y is ', y

br  = [1., 2., 8., 0., 7., 3., 6., 9., 0., 2.]
print *, 'Value of br is:', br

print *, 'Give me a value for r_cut'
read(*,*) r_cut

total = sum(br, mask = x**2 + y**2 < r_cut**2)

print *, 'Total', total

end program

if you want to stick to F77, you have to write:

      real x(10), y(10), br(10), total, r_cut
      integer i

      x(1) = 1.0
      x(2) = -6.0

      ....

      do i = 1, 10
          write(*,*) 'Value of x is', x(i)
      enddo

      y(1) = 3.0
      y(2) = 6.0
      .....

      br(1) = 1.0
      br(2) = 2.0
      br(3) = 8.0
      .....

      write(*,*) 'Give me a value for r_cut'
      read(*,*) r_cut

      total = 0
      do i=1, 10
          if (x(i)**2 + y(i)**2 .lt. r_cut**2) then
               total = total +  br(i)
          endif
      enddo

      print *, 'Total'
      end

Basically every statement should start at the 7th column, you don't have anymore the vectorial instructions (so you have to fill the vectors element by element, unless you read it from a file).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM