Fortran 77 back polski | english

up

Program arguments

There are 2 functions avialable in g77 and f95 compilers:


IARGC
  integer iargc()
  - gives number of program arguments
  eg. ./a.out ala ma kota
  -<  3

GETARG
  call getarg(i,arg)
  - to obtain value of i-th argument < character*(*) arg
  i=0,iargc

Test program looks as folows:

      program test_arg
      integer iargc
      integer argc
      character*50 argv(10)

      argc=iargc()
      write (*,*) 'Number of arguments:', argc
      do i=1,min(argc,10)
        call getarg(i,argv(i))
        write (*,*) 'Argument no',i,'=',argv(i)
      enddo

      end

up

Memory alignment of arrays

If someone likes XY notation he will be thinking about Fortran arrays in this way:

 dimension table(4,10)
 table(i,j) = i - j
    i ---->
 j
 |
 |
\|/
        0     1     2     3
       -1     0     1     2
       -2    -1     0     1
       -3    -2    -1     0
       -4    -3    -2    -1
       -5    -4    -3    -2
       -6    -5    -4    -3
       -7    -6    -5    -4
       -8    -7    -6    -5
       -9    -8    -7    -6


In memory:                                   __ tab(1,3)
                                  /
                                 /
        0  1  2  3 -1  0  1  2 -2 -1  0  1 -3 -2 -1  0 ...
       \         / \         / \         / \         /
          row 1       row 2       row 3       row 4
           j=1         j=2         j=3         j=4
        


table(1,j) ---  it is the begining of j-th row

Example usage:

  subroutine test(tab)
    dimension tab(4)
    print *,(tab(i),i=1,4)
  end

  call test(table(1,j))
  

will pass j-th row to printing subroutine

If someone likes different order of indices, he should change names row>-<columns only, in memory it will stay as is.


up

Pseudo-random generators

W Fortranie 77 nie ma standartu dotyczącego generatorów liczb pseudo-losowych, dlatego każdy kompilator ma własne funkcje i wlasny ich sposób wywoływania. Poniżej przedstawiam dwa sposoby na otrzymanie liczb pseudo-losowych działające w kompilatorze GNU:

1. Inicjalizujemy generator podając ziarno za pomocą procedury srand

        program random_test1
        implicit none
        real a,b,c,d

        call srand(22231)
        a=rand(0)
        b=rand(0)
        c=rand(0)
        d=rand(0)
        print *, "first 4 numbers:", a, b, c, d
        end

2. Inicjalizujemy generator podając ziarno w pierwszym wywołaniu funkcji rand

        program random_test2
        implicit none
        real a,b,c,d

        a=rand(22231)
        b=rand(0)
        c=rand(0)
        d=rand(0)
        print *, "first 4 numbers:", a, b, c, d
        end

Uwaga, program wywołany z tym samym ziarnem da te same wyniki!

Do prawdziwych obliczeń należy ziarno zaczerpnąć z jakiegos niezależnego żródła. Polecam czytanie z urządzenia /dev/random, ale najłatwiejszym i najszybszym sposobem jest pobranie czasu systemowego i użycie jego jako ziarna generatora. Ciekawostką jest to, że programy uruchomione w tej samej sekundzie, dadzą te same wyniki.

        program random_test3
        implicit none
        real a,b,c,d
        integer seed

c       get system time as seed (number of seconds from 1970)
        seed=time()

c       start generator with seed
        call srand(seed)

c       take successive numbers
        a=rand(0)
        b=rand(0)
        c=rand(0)
        d=rand(0)
        print *, "first 4 numbers:", a, b, c, d 
        print *, "with seed=",seed
        end


webmaster of this page is Jan Skowron, page is hosted on server astrouw.edu.pl
Valid HTML! Valid CSS!