


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Nothing in it accept fortran in coding and etc
Typology: High school final essays
1 / 42
This page cannot be seen from the preview
Don't miss anything!



































testing can be used to show the presence of bugs,
but never to show their absence
Edsger W. Dijkstra
Fall 2009
z A Fortran 90 program uses the
p^
g
attribute to declare arrays. z The
attribute requires three
q
components in order to complete an arrayspecification,
rank
,^ shape
, and
extent
z The
rank
of an array is the number of “indices”
or “subscripts.” The maximum rank is 7 (
i.e
seven-dimensional). z The
shape
of an array indicates the number of
elements in each “dimension.”
z The
extent
is written as
m
: n , where
m
and
n^
( m
The
extent
is written as
m
: n , where
m
and
n^
( m
≤^ n
) are
s. We saw this in the
,^ substring, etc.,^
g,
z Each dimension has its own extent. z A
t^
t^
f^
di^
i^
i^ th
f it
z An extent of a dimension is the range of itsindex. If
m
: is omitted, the default is 1.
i^
i^
i^
means possible indices are -3, -2 , -1, 0, 1, 2 5:
means possible indices are 5,6,7, ^7
means possible indices are 1,2,3,4,5,6,
p^
z The
attribute has the following
g
form:
DIMENSION
(extent-1, extent-2, …, extent-
n )
(^
,^
,^
,^
)
z Here, extent-
i^ is the extent of dimension
i****.
z This means an array of dimension
n^
( i.e
.,^ n
z This means an array of dimension
n^
( i.e
.,^ n
indices) whose
i -th dimension index has a range
given by extent-
i****.
g^
y
z Just a reminder: Fortran 90 only allowsmaximum 7 dimensions. z Exercise: given a
attribute,
determine its shape.
p
z Array declaration is simple. Add the
y^
p
attribute to a type declaration.
z Values in the
attribute are usually
y
s to make program modifications
easier. INTEGER,
PARAMETER
::
SIZE=5,
LOWER=3,
UPPER
=^
5
INTEGER,
PARAMETER
::
SMALL
=^
10,
LARGE
=^
15
REAL,
DIMENSION(1:SIZE)
::
x
INTEGER,
DIMENSION(LOWER:UPPER,SMALL:LARGE)
::
a,b
,^
(^
,^
)^
,
LOGICAL,
DIMENSION(2,2)
::
Truth_Table
z^
Fortran 90 has, in general, three different ways
g^
y
to use arrays: referring to
individual array
element
, referring to the
whole array
, and
referring to a
section of an array
z^
The first one is very easy. One just starts withthe array name, followed by
between which
are the
indices
separated by
z^
Note that each index must be an
or an
expression evaluated to an
, and the
l^
f^
i d
t b
i th
f th
value of an index must be
in the range of the
corresponding extent
. But, Fortran 90 won’t
check it for you check
it for you.
z^ Suppose we have the following declarations:^ INTEGER, PARAMETER :: L_BOUND = 3, U_BOUND = 10INTEGER, DIMENSION(L_BOUND:U_BOUND,
&
L_BOUND:U_BOUND) :: a
DO^
i^ =
L_BOUND,
U_BOUND
DO^
j^ =
L_BOUND,
U_BOUND
a(i
j)
=^
0
DO i = L_BOUND, U_BOUNDDO j = i+1, U_BOUND
t^
= a(i,j)
a(i,j)
=^
0
END
DO a(i,i)
=^
1
t^
a(i,j)
a(i,j) = a(j,i)a(j,i) = tEND DO
END DO
END DO
generate an identity matrix
Swapping the lower andupper diagonal parts (
i e
upper
diagonal parts (
i.e .,
the^
transpose
of a matrix)
z Fortran has the implied
that can generate
p^
g
efficiently a set of values and/or elements. z The implied DO is a variation of the
-loop.
p^
p
z The implied DO has the following syntax:^ (
item
-^
item-
item-n
v=initial
final
step)
(item
1,
item
2,
…,item
n,
v=initial,final,step)
z Here,
item-
,^ item-
item-n
are
variables or expressions,
v^
is an
variables or expressions,
v^
is an
variable, and
initial
,^ final
, and
step
are
expressions.
p
z “
v=initial,final,step
” is exactly what we
saw in a
-loop.
p
z Implied
may be nested.
p^
y
(ik,(jj,ij,j=1,3), i=2,4)* z In the above
(jj ij j 1 3)**
is nested in
z In
the above,
(j*
j,ij,j=1,3)*
is^
nested in
the implied
i^
loop.
z Here are the results:
When
i^
= 2, the implied
generates
2k, (jj,2j,j=1,3)* Then
j
goes from 1 to 3 and generates
Then
,^ j
goes from 1 to 3 and generates
2k, 11, 21, 22, 22, 33, 2***
j = 1
j = 2
j = 3
j = 1
j = 2
j = 3
z Continue with the previous example
p^
p
(ik,(jj,ij,j=1,3), i=2,4)* z When
i^
= 3, it generates the following:
g^
g
3k, (jj,3j,j=1,3)* z Expanding the
j^
loop yields:
p^
g^
j^
p y
3k, 11, 31, 22, 32, 33, 3*** z When
i^
= 4, the
i^
loop generates
,^
p g
4k, (jj, 4j, j=1,3)* z Expanding the
j^
loop yields
p^
g^
j^
p y
4k, 11, 41, 22, 42, 33, 4***
j = 1
j = 2
j = 3
z The following produces all upper triangularThe
following produces all upper triangular entries,
row-by-row
, of a 2-dimensional array:
((a(p q) q = p n) p = 1 n)((a(p
,q),q = p,n),p = 1,n)
z When
p^
= 1, the inner
q^
loop produces
a(1,1)
a(1 2)
a(1 n)
a(
a(
,n)
z When
p=2, the inner
q^
loop produces
a(2,2)
a(2,3)
a(2,n)
z When
p=3, the inner
q^
loop produces
a(3,3)
a(3,4)
a(3,n)
z When
p=
n,^
the inner
q^
loop produces
a(n,n)
p^
q^
p p
z The following produces all upper triangularThe
following produces all upper triangular entries,
column-by-column
((a(p q) p = 1 q) q = 1 n)((a(p
,q),p = 1,q),q = 1,n)
z When
q=1, the inner
p^
loop produces
a(1,1)
z When
q=2, the inner
p^
loop produces
a(1,2)
a(2,2) z When
q=3, the inner
p^
loop produces
a(1,3)
a(2,3)
a(3,3)
z When
q=
n, the inner
p^
loop produces
a(1,n)
a(2,n)
,^ a(3,n)
a(n,n)
z The following shows three ways of reading
n
g^
y^
g
data items into an one dimensional array
a()
z Are they the same? z Are they the same?
READ(,) n,(a(i),i=1,n) (1)**
READ(,) n**
i^
i
( ) (2)
READ(,) (a(i),i=1,n)READ(* ) n (3)*
READ(,) n DO i = 1, n**
READ(,) a(i)**
(3)
END DO
z Suppose we wish to fill
a(1)
,^ a(2)
and
a(3)
pp
with 10, 20 and 30. The input may be:^3
10
20
30
3
10
20
30
z Each
starts from a new line!
OK
READ(,) n,(a(i),i=1,n)READ(* ) n (1) (2)*
OK Wrong!
n^ gets 3 and
READ(*
,) n*
READ(,) (a(i),i=1,n) (2)**
Wrong!
n^ gets 3 and the second
READ
fails
READ(,) nDO i = 1, n (3)**
Wrong!
n^ gets 3 and the three
READ
s fail
READ(,) a(i) END DO**