Wednesday, December 5, 2012

Array

Array:
The numerical values which are generally elements of an associated group is known as array.
There are several dimensional array
1. One dimensional array
2. Two dimensional array
3. Multidimensional array
One dimensional array:
           One dimensional array can be written as
     x1 x2 x3  x4 ………….xn
But in Fortran it can be written as
 x(1) x(2) x(3) ………..x(n)
Two dimensional array:
The variable which has two subscripts is known as two dimensional array. It is written as
 x1,1     x1,2
 x2,1       x2,2
Here 1,1 1,2 2,1 2,2 are the subscripted variable.
But in Fortran it can be written as x(1,1) x(2,2) x(2,1) x(2,2)
Multidimensional array:
The variable which is more then two subscripts is known as multidimensional array.
Let’s start calculation with Dimension statement. To solve a problem we first have to declare the dimension through the dimension statement.
Example:
Find Mean Variance Standard deviation absolute deviation of following array.
x
12
23
16
14
18
24
25
30
35
20


Solution :
1234567890
                Dimension x(10)
                Write(*,*) 'Enter the value of N'
                Read(*,*) N
                Do i = 1,N
                Write(*,*) 'Enter the value of the array x(',i,')'
                Read(*,*) x(i)
                End do
                sum = 0.0
                Do j = 1,N
                sum = sum+ x(j)
                End do
                Ave = sum / N
                Var = 0.0
                Do k = 1,N
                Var = Var + (x(k)-Ave)**2/N
                End do
                 Absd=0.0
                Do M = 1,N
                Absd = Absd+ abs(x(M)-Ave)
                End do
                sd = sqrt(Var)
                write(*,*) ' The variance of the array is = ', Var ,Ave
                Write(*,*) 'The standard deviation of the array is =', sd
                Write(*,*) ' The absolute deviation is = ' ,Absd
                End
Here the output will be like this:
Enter the value of N
10
 Enter the value of the array x( 1)
12
 Enter the value of the array x( 2)
23
 Enter the value of the array x( 3)
16
 Enter the value of the array x( 4)
14
 Enter the value of the array x( 5)
18
 Enter the value of the array x( 6)
24
 Enter the value of the array x( 7)
25
 Enter the value of the array x( 8)
30
 Enter the value of the array x( 9)
35
 Enter the value of the array x( 10)
20
  The variance of the array is =   46.6100006
 The standard deviation of the array is =  6.82715178
  The absolute deviation is =   57.

**Find the correlation coefficient and  regression coefficient of Y on X of the following array.
x
7
3
5
3
8
9
2
6
12
6
y
5
3
8
9
4
2
4
3
7
10
x
13
3
5
9
6
4
7
3
5
12
y
15
7
8
6
5
16
13
2
7
1
x
16
12
17
19
14
3
7
9
7
3
y
12
3
16
19
10
12
5
9
10
7


Solution:
We know that
Correlation coefficient is
 







And regression coefficient of Y on X is




From the above formula we see that we need sum of x , sum of y , sum square of x , sum square of y and sum product of xy. So at first we have to calculate following terms
1234567890
                  dimension x(100)  ,Y(100)
                  do i = 1,30
                  print*, 'enter value of array x'
                  read(*,*) x(i)
                 Write(*,*) 'Enter the value of array y'
                 Read(*,*) y(i)
                 end do
                 sumx=0
                 sumy=0
                 do i=1,30
                 sumx=sumx+x(i)
                 sumy=sumy+y(i)
                 enddo
                avex=sumx/10.0
                avey=sumy/10.0
                sum1=0
                sum2=0
                 spxy=0
                 do i=1,30
                  sum1=sum1+(x(i)-avex)**2
                 sum2=sum2+(y(i)-avey)**2
                 spxy=spxy+(x(i)-avex)*(y(i)-avey)
                 end do
                 correlation= spxy/sqrt(sum1*sum2)
                  Byx=spxy/sum1
                   print*, 'the value of corr',correlation
                    print*,  'Byx is=',Byx
                   stop
                    End
Description:
Here we use do I = 1,30 because there are 30 observation in array x and 30 observation in array y. So to read and calculation we have to use do I = 1,30.
** Find the multiplication  of following matrix





 Solution:
1234567890
              Dimension A(3,2),B(2,3),C(3,3)
        write(*,*) 'Enter the value of matrix A'
        Do 10 k = 1,3
        Do 10 j = 1,2
        Read(*,*) A(k,j)
  10    Continue
        Write(*,*) 'Enter the value of matrix B'
        Do 20 i = 1,2
        Do 20 l = 1,3
        Read(*,*) B(i,l)
  20    Continue
        Do 30 i = 1,3
        Do 30 j = 1,3
        C(i,j) = 0.0
        Do 30 k = 1,2
        C(i,j) = C(i,j) + A(i,k)*B(k,j)
  30    Continue
        Write(*,*) C( 1,1)  , C(1,2)  ,C(1,3)
        Write(*,*) C( 2,1)  , C(2,2)  ,C(2,3)
        Write(*,*) C(3,1)   , C(3,2)  ,C(3,3)
        End


Here the output will be like this.
         
  
                    Enter the value of matrix A
4
3
6
5
8
9
 Enter the value of matrix B
4
6
4
7
7
5
  37.  45.  31.
  59.  71.  49.
  95.  111.  77.
Here we have to put the values row wise.

No comments:

Post a Comment