Fortran accept anything but, High school final essays of Physics

Nothing in it accept fortran in coding and etc

Typology: High school final essays

Pre 2010

Uploaded on 12/15/2023

prakhar-tripathi-4
prakhar-tripathi-4 🇮🇳

3 documents

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Fortran90Arrays
Fortran
90
Arrays
Programtestingcanbeusedtoshowthepresenceofbugs
Program
testing
can
be
used
to
show
the
presence
of
bugs
,but never to show their absenceEdsger W. Dijkstra1Fall 2009
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download Fortran accept anything but and more High school final essays Physics in PDF only on Docsity!

Fortran 90 ArraysFortran

90 Arrays Program testing can be used to show the presence of bugsProgram

testing can be used to show the presence of bugs,

but never to show their absence

Edsger W. Dijkstra

Fall 2009

The

DIMENSION

Attribute:

The

DIMENSION

Attribute: 1/

z A Fortran 90 program uses the

DIMENSION

p^

g

attribute to declare arrays. z The

DIMENSION

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.”

The

DIMENSION

Attribute:

The

DIMENSION

Attribute: 3/

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

INTEGER

s. We saw this in the

SELECT

CASE

,^ 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^

The

DIMENSION

Attribute:

The

DIMENSION

Attribute: 4/

z The

DIMENSION

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

DIMENSION

attribute,

determine its shape.

p

The

DIMENSION

Attribute:

The

DIMENSION

Attribute: 6/

z Array declaration is simple. Add the

y^

p

DIMENSION

attribute to a type declaration.

z Values in the

DIMENSION

attribute are usually

y

PARAMETER

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

Use of Arrays:

Use

of Arrays: 1/

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

INTEGER

or an

expression evaluated to an

INTEGER

, 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.

Use of Arrays:

Use

of Arrays: 3/

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)

The Implied

DO

:^ 1/

The

Implied

DO

z Fortran has the implied

DO

that can generate

p^

g

efficiently a set of values and/or elements. z The implied DO is a variation of the

DO

-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

INTEGER

variables or expressions,

v^

is an

INTEGER

variable, and

initial

,^ final

, and

step

are

INTEGER

expressions.

p

z

v=initial,final,step

” is exactly what we

saw in a

DO

-loop.

p

The Implied

DO

:^ 3/

The

Implied

DO

z Implied

DO

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

DO

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

The Implied

DO

:^ 4/

The

Implied

DO

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

The Implied

DO

:^ 6/

The

Implied

DO

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

The Implied

DO

:^ 7/

The

Implied

DO

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)

,^

,^

,^

Array Input/Output:

Array

Input/Output: 2/

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

Array Input/Output:

Array

Input/Output: 3/

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

READ

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**