Sunteți pe pagina 1din 31

Array

An array is a set of scalar data, all of the


same type and type parameters, whose
individual elements are arranged in a
rectangular pattern. An array element is one
of the individual elements in the array and
is a scalar. An array section is a subset of
the elements of an array and is itself an
array.
Why Arrays programming

In real programs we often need to handle a large


amount of data in the same way, e.g. to find the mean
of a set of numbers, or to sort a list of numbers or
names, or to analyze a set of students' test results, or
to solve a system of linear equations. To avoid an
enormously clumsy program, where perhaps
hundreds of variable names are needed, we can use
subscripted variables, or arrays. These may be
regarded as variables with components, rather like
vectors or matrices.
X(3), Y(I + 2 * N)
2
They are written in the normal way, except
that the subscripts are enclosed in
parentheses e.g. subscripted Xi from
commonly used in mathematics. In this
notation, Xi describes all variables
(X+,.....,X_2,X_1,X0,X1,X2,....,X)
collectively , since there are no subscripts in
the FORTRAN character set ,parenthesis are
used instead and the equivalent of Xi is
written X(I),This called in FORTRAN 90
Arrays.
3
Rank and size of arrays
An array may have up to seven dimensions, and
any extent (number of elements) in any
dimension.
The rank of the array is the number of
dimensions; its size is the total number of
elements, which is equal to the product of the
extents. An array may have zero size. The shape
of an array is determined by its rank and its
extent in each dimension, and may be
represented as a rank-one array whose elements
are the extents.
4
Rank of arrays:
The rank is the number of dimensions of array it has:
- zero for scalar.
- one for vector( one dimensional array )
- two for array( two dimensional array )
- three for array_1
for example : array_1
Real , Dimension(5,4,3)::array_1

 The size of the array: is the product of the elements, in ithe


above case there are 60 elements.
 Arrays of the same shape can be grouped in declarations using
the Dimention alternative.
For example:
Real , Dimension(5,4,3)::array_1 , array_2 , array_3
180 real location in the computer have storred for the elements of
arrays: array-1 , array-2 , array-3
5
EX//
To illustrate the basic principles, let's compute the
sample mean and standard deviation of a set of N
observations. The mean is defined as
N
1
X 
N
 Xi
i 1

where Xi is the i-th observation.


The standard deviation s is defined as

 
N
1 2
s 
2

N  1 i 1
Xi  X
6
This program computes these two quantities from data
read from file DATA. The first item
IMPLICIT NONE
INTEGER :: I, N
REAL :: Std = 0
REAL, DIMENSION(5) :: X
REAL :: XBar = 0
OPEN (1, FILE = 'DATA.txt')
READ (1, *) N
DO I = 1, N
READ (1, *) X(I)
XBar = Xbar + X(I)
END DO
XBar = XBar / N
DO I = 1, N
Std = Std + (X(I) - XBar) ** 2
END DO
Std = SQRT( Std / (N - 1) )
WRITE(*,*) 'Mean: ', XBar
WRITE(*,*) 'Std deviation: ', Std 7
END
Arrays Declarations
For a simple variable of types (Real and Integer) we have:
1- One dimensional array of real called (Vector)

REAL,Dimension(10)::vector
REAL, DIMENSION(10) :: X

or
Real :: Vector(10)
Real :: X(10)
This is declarations creates 10 Real variables:
vector(1),vector(2),vector(3),.........
.., vector(10)
which are stored contiguously. 8
2- Two dimensional array of an integer called (Array)
Integer,Dimension(5,20)::array
or
Integer :: Array(5,20)
This is mean that the declaration creates 100 Integer
variables:
array(1,1),array(1,2),array(2,1)..........
,array(5,20)

9
Array(1,1) Array(1,2) Array(1,20)
Array(2,1)
Array(3,1)

Array(5,1) Array(5,20)

1. array(1,2) would follow array(1,1). HORIZONTALLY

2. array(2,1) would follow array(1,1). VERTICALLY

The convention is that first subscript of two dimensional array


is assuming to represented a row in the above matrix while
the second subscripts a column. 10
Some terms of Array
• Array is useful for representing and processing
large amounts data.
• An array is collection subscripted variable with
the same name.
• Number of an arrays are called element.
• The number of elements in array is its size.
• The number of dimensions of an array is its rank
,an array may have up to seven dimension

11
Rules for using dimension statement:
1- In one program we can use more than one
dimensional statement.
2- It,s not allowed to any control statements to
GOTO dimension statement .
3- If the subscripted variable is integer then all the
elements in it must be integer (also for real).
4- The subscripted must be integer X(i),X(J),....,also
it must be greater than zero.

12
5- The general form of diemsion statement of one
dimensional array is:
Real,Dimension(n)::X
Where: n: is the number of location stored.
x: is the name of variable
6- The general form of dimension for two dimension
array is:
Integer,Dimension(n,m)::X
Where: n: is the number of rows.
m: is the number of columns.
x: is the name of array.
13
Program(1):
Write a program in Fortran 90 to calculate the ages average of students in first
stage where the number of students is (10) when:-
10
age average =  Ai
i=1

Solution:

Real,dimension(10)::A
Open(4,file='data.txt')
Sum=0.0
Do I=1,10
Read(4,*)A(I)
Sum=Sum+A(I)
End Do
Average = Sum/10
Print*, 'The ages average of student is=',
Average
End
14
Program(2):
Write a program in Fortran 90 to find the average mark of class (10
students) for computer examination using dimension
statement.
Solution:
Integer::I
Real,dimension(10)::X
Real::total
S=0.0
Do I=1,10
Print*, 'Supply mark of student No.',I
Read*,X(I)
S=S+X(I)
End do
Print*, 'Average score is',S/10
End
15
Declarations
Literals and constants can be used in array declarations,
REAL, DIMENSION(100) :: R
REAL, DIMENSION(1:10,1:10) :: S
REAL :: T(10,10)
REAL, DIMENSION(-10:-1) :: X
INTEGER, PARAMETER :: lda = 5
REAL, DIMENSION(0:lda-1) :: Y
REAL, DIMENSION(1+lda*lda,10) :: Z

default lower bound is 1,


bounds can begin and end anywhere,
arrays can be zero-sized (if lda = 0),
16
Visualization of Arrays
REAL, DIMENSION(15) :: A
REAL, DIMENSION(-4:0,0:2) :: B
REAL, DIMENSION(5,3) :: C
REAL, DIMENSION(0:4,0:2) :: D

Individual array elements are denoted by


subscripting the array name by an INTEGER,
for example, A(7) element of A, or C(3,2), 3
elements down, 2 across.

17
Array Syntax
Can reference:
• whole arrays
A = 0.0
sets whole array A to zero.
B = C + D
adds C and D then assigns result to B.
• elements
A(1) = 0.0
sets one element to zero,
B(0,0) = A(3) + C(5,1)
sets an element of B to the sum of two other elements.
18
Array Syntax

• array sections
A(2:4) = 0.0
sets A(2), A(3) and A(4) to zero,

B(-1:0,1:2) = C(1:2,2:3) + 1.0

adds one to the subsection of C and assigns to


the subsection of B.

19
Array Sections
subscript-triplets specify sub-arrays. The general form is:
[< bound1 >]:[< bound2 >][:< stride >]
The section starts at < bound1 > and ends at or
before < bound2 >. < stride > is the
increment by which the locations are selected.
Example
A(:) ! the whole array
A(3:9) ! A(m) to A(n) in steps of 1
A(3:9:1) ! as above
A(m:n) ! A(m) to A(n)
A(m:n:k) ! A(m) to A(n) in steps of k
20
A(8:3:-1) ! A(8) to A(3) in steps of-1
A(8:3) ! A(8) to A(3) step 1=> Zero size
A(m:) ! from A(m) to default UPB
A(:n) ! from default LWB to A(n)
A(::2) ! from default LWB to UPB step 2
A(m:m) ! 1 element section
A(m) ! scalar element - not a
section
are all valid sections.

21
Array Input/out put statements
Implied Do-loops:
A compact way of reading in (or writting out) the
elements of an array is by use of an implied Do-
loop , which is structuer a variable only for dealing
with I/O
1- For one dimension array :
To read and write array A(I) using Do-loops

Do I=1,5
Read(*,*)A(I) Do I=1,5
End Do write(*,*)A(I)
End Do 22
Other types of array I/O
R= (10,20,30,40,50) DIRECT
Read(*,*)(R(I),I = 1,5) without do-loop
This is mean read the element in subscripted variable R(I).

Write(*,*)(P(I),I = 1,5)
This is mean that the program print output as follows:
P(1),P(2),P(3),P(4),P(5)

2.For two dimension array:


There are two ways to I/O two dimensional arrays:
1)- Read array by columns:,for an array B(I,J)
Do J=1,3
Do I=1,3
Read(*,*)B(I,J)
End Do
End Do 23
2)- Read array by rows:
Do I=1,3
Do J=1,2
Read(*,*)B(I,J)
End Do
End Do 24
Other types of array I/O:
1)- Read input by columns:
Do I=1,3
Read(*,*)(B(I,J),J=1,2)
End Do

2)- Write output by rows:


Do J=1,2
Write(*,*)(B(I,J),I=1,3)
End Do

25
Rules for matrix multiplication:

1)- Multiplication allowed when :


A(I,J) , B(I,J) is two different matrix
,columns in A=N , must equal Rows in B=N .
A(I,N) , B(N,J)
Then C(I,J) = A(I,N)*B(N,J)
Where C(I,J) is the result matrix has:
(Rows=rows in A),(Columns = Columns in B).
2)- Each element in matrix C(I,J) making from
multiplication of elements in Row matrix A by
elements in column matrix B.
26
EX(3):
Write a program in F.90 to calculate the result matrix
C(I,J) from [A(I,J) + B(I,J)] when:

1 4 7  10 40 70 
  
A( I , J )  2 5 8 , B( I , J )  20 50 80

3 6 9 
30 60 90

27
!This program to find the result of summation of
!Two matrix
Program Sum_arrays
Integer,dimension(3,3)::A,B,C
Do
Do J=1,3
Do I=1,3
Read(*,*)A(I,J), B(I,J)
End do
End do
Do J=1,3
Do I=1,3
C(I,J)=A(I,J)+B(I,J)
End do
End do
Do I=1,3
Write(*,*)(C(I,J),J=1,3)
End do
End do 28
Program(2):
Write a program in Fortran 90 to transpose the following matrix.
!Fortran 90
!This program to find the result of
!Transpose of matrix
Program transpose_array
Integer,dimension(3,4):: N
Integer,dimension(4,3):: M
Do
Do I=1,3
Read(*,*)(N(I,J),J=1,4)
End do
Do J=1,3
Do I=1,4
M(I,J) = N(J,I)
End do
End do
Do J=1,3
Write(*,*)(M(I,J),I=1,4)
End do
End do
End Program transpose_array
29
Program(2):
Write a program in F.90 to find the results of:

4 5
B  K  
6 3 

30
!Fortran 90
!This program to find the result of two dimension array
Program B_array
Integer,dimension(2,2)::A,B
Do
Read(*,*)K
Do J=1,2
Do I=1,2
Read(*,*)A(I,J)
B(I,J)=K*A(I,J)
End do
End do
Print*, “ The result of matrix”
Do I=1,2
Write(*,*)(B(I,J),J=1,2)
End do
End do
End Program B_array 31

S-ar putea să vă placă și