Equations

Monday 15 February 2010

Some FORTRAN

Swapping numbers...

!====================================
subroutine swap(m,n)
implicit none
integer::m,n
integer temp
temp=m    !store first number in temp
m=n    !store n in m
n=temp    !store temp in n
end subroutine swap
!====================================


Computing factorials...

!=============================
!subroutine name: factorial
!computes the factorial, fac,
!of integer, n
!=============================
subroutine factorial(n,fac)
implicit none
integer::i,n
double precision::fac
!
fac=1
do i=n,1,-1
fac=fac*i
end do
!
end subroutine factorial
!=============================

Computing average...

!=========================================
subroutine avgcalc(A,n,mean)
!Usage: call avgcalc(data_set,size,average)
!=========================================
!In the main program the array
!can be an allocatable array
!but in the subroutine it is
!a fixed array
!=========================================
integer::n
real::sum,mean
integer::i
real,dimension(n)::A
!
sum=0.0
!
sumdata:    do i=1,n
            sum=sum+A(i)
        end do sumdata
!
mean=sum/n
!
end subroutine avgcalc

NB: These subroutines were written as programming exercises so please take care while using these.

No comments:

Post a Comment