standards - Not declaring a loop counter up front in Fortran -


as part of assignment, i've been given (old) code fortran program. have experience c++ (and oberon) , knowledge have of fortran, i've obtained through various (parts of) tutorials, aren't consistent 1 another. however, despite basic knowledge code hurts eyes lack of elegance or consistency. more disturbing: seems full of programming malpractices.

the particular piece of code want ask looks similar example:

program foo    implicit none    call bar(0.0,-1)  end program foo  subroutine bar(t,i)    dimension(5) :: r = 0.5   real :: s = 0.5    k = 1, 5      r(k) = k *   end   call random(s) ! random well-behaved pseudorandom number generator                  ! on interval ]0,1[ implemented elsewhere.   k = 1 + (s * 5)   t = r(k) end subroutine bar 

i'll explain believe idea is. random(s) stores value between 0.0 , 1.0 in s. means 1 + (s * 5) yield real number between 1 , 5 (some testing of prng shows maximum leads ~5.992 , minimum ~1.0016, say). if left hand side integer, automatic type conversion of right hand side occur , mantissa chopped off, leaving integer value on interval [1,5]. k isn't declared integer anywhere. instead, k has been initialized loop counter do-loop. of course means must integer, think bad practice. right? additionally, frown upon declaration of array r, since there no explicit type specifier. i've made clear store reals initializing array, not done in actual code...

of course i've adjusted actual code more initialization, adhering higher standard. (imagine implicit variables, inconsistent capitalization , goto's on place.) also, perhaps can ask while i'm @ it: modern standard end programs, subroutines , functions syntax end <program/subroutine/function> name? in code given used end. i've been assuming correct modern syntax 1 mentioned, i'm not sure. i'm not sure if names of programs etc. should uncapitalized. (in code given in all-caps)

you largely correct in write, there things worth noting.

r , k (and t , i) victims of implicit typing should noted isn't in way because of how used or initialized. specifically, r real regardless of =0.5.

further, before fortran 95 loop variable needn't have been integer. do a=1,5 doesn't force implicitly typed a integer (but incorrect fortran 95+).

you correct modern taste have implicit none in every scoping unit - forcing explicitly typed.

i would, however, take exception "correct modern syntax": there's nothing incorrect ending subprograms end, prefer , use longer forms.

two more minor things note: explicit initialization implies save attribute (which has no real effect in example); random isn't standard intrinsic, , if cannot return 0 behaves differently standard random_number.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -