f 201array

Upload: hmxa91

Post on 07-Jul-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/18/2019 F 201Array

    1/22

    Instructor: S/L Naveed

    Arrays

  • 8/18/2019 F 201Array

    2/22

    Array An array is a collection of data of the same type. Array elements are indexed or subscripted, just like

    x1, x2, ..., xn in mathematics. A Fortran 90 program uses the !"#$%!&$attribute to declare arrays.

  • 8/18/2019 F 201Array

    3/22

    The DIMENSION Attribute'he !"#$%!&$ attribute re(uires threecomponents in order to complete an arrayspecification) rank, shape, and extent.'he rank of an array is the number of *indices+ or*subscripts.+ 'he maximum rank is -i.e., se en/dimensional array .

    'he shape of an array indicates the number ofelements in each *dimension.+

  • 8/18/2019 F 201Array

    4/22

    The DIMENSION Attribute'he rank and shape of an array is represented as-s1,s2, ,sn , here n is the rank of the array and si-13 i 3 n is the number of elements in the i/thdimension.- means a rank 1 array ith elements-4,9 means a rank 2 array -i.e., a table hose first

    and second dimensions ha e 4 and 9 elements,respecti ely.-10,10,10,10 means a rank 5 array that has 10elements in each dimension.

  • 8/18/2019 F 201Array

    5/22

    The DIMENSION Attribute'he extent is ritten as m)n, here m and n -m3nare !$'#6#7s.

    #ach dimension has its o n extent. An extent of a dimension is the range of its index. !fm) is omitted, the default is 1./8)2 means possible indices are /8, /2 , /1, 0, 1, 2

    4) means possible indices are 4,:, , means possible indices are 1,2,8,5,4,:,

  • 8/18/2019 F 201Array

    6/22

    The DIMENSION Attribute'he !"#$%!&$ attribute has the follo ing form)!"#$%!&$-extent/1, extent/2, , extent/n

  • 8/18/2019 F 201Array

    7/22

    The DIMENSION Attribute;ere are some examples)

    !"#$%!&$-/1)1 is a 1/dimensional array ith

    possible indices /1,0,1!"#$%!&$-0)2,8 is a 2/dimensional array -i.e., a

    table .

  • 8/18/2019 F 201Array

    8/22

    The DIMENSION Attribute Array declaration is simple. Add the !"#$%!&$ attribute to atype declaration.=alues in the !"#$%!&$ attribute are usually

  • 8/18/2019 F 201Array

    9/22

    Use of ArraysFortran has, in general, three different ays to usearrays) referring to indi idual array element, referringto the hole array, and referring to a section of anarray.'he first one is ery easy. &ne just starts ith thearray name, follo ed by - bet een hich are the

    indices separated by ,.$ote that each index must be an !$'#6#7 or anexpression e aluated to an !$'#6#7, and the alueof an index must be in the range of the

    corresponding extent.

  • 8/18/2019 F 201Array

    10/22

    Use of Arrays%uppose e ha e the follo ing declarations!$'#6#7,

  • 8/18/2019 F 201Array

    11/22

    Use of Arrays%uppose e ha e the follo ing declarations)!$'#6#7,

  • 8/18/2019 F 201Array

    12/22

    Initialization of Arrays Array can be initialiGed by defining e ery element )!nteger, imension -4 )) A

    o !?1,4

    A-! ? 0#nd o!t is also possible to initialiGe array in the declaration

    statement itself7eal, imension-4 )) A?-H1.,2.,8.,5.,4.H

  • 8/18/2019 F 201Array

    13/22

    Initialization of Arrays!nitialiGation can be done through 7#%;A

  • 8/18/2019 F 201Array

    14/22

    Initialization of Arrays'7A$%

  • 8/18/2019 F 201Array

    15/22

    Initialization of Arrays!nitialiGe through an implicit dointeger, dimension-4 )) A?-H-i, i?1,4 HinitialiGes A? 1,2,8,5,4For efficiently generating the set of alues of anarray !mplied o statement is used.

  • 8/18/2019 F 201Array

    16/22

    The Implied DOFortran has the implied & that can generateefficiently a set of alues andHor elements.'he implied & is a ariation of the &/loop.'he implied & has the follo ing syntax)-item/1, item/2, ,item/n, ?initial,final,step;ere, item/1, item/2, , item/n are ariables or

    expressions, is an !$'#6#7 ariable, and initial,final, and step are !$'#6#7 expressions.

    ?initial,final,step+ is exactly the same as in Counter&/loop.

  • 8/18/2019 F 201Array

    17/22

    The Implied DO'he execution of an implied & belo lets ariable to start ithinitial, and step through to final ith a step siGe step.

    -item/1, item/2, ,item/n, ?initial,final,step

    'he result is a se(uence of items.-iJ1, i?1,8 generates 2, 8, 5.-iKk, iJkKi, i?1, ,2 generates k, 1Jk - hen i?1 ,8Kk, 8JkK8 - hen i?8 , 4Kk, 4JkK4 - hen i ? 4 ,

    Kk, JkK - hen i ? .-a-i ,a-iJ2 ,a-iK8/1 ,iK5,i?8,4 generatesa-8 , a-4 , a- , 12 -i?8 , a-5 , a-: , a-11 , 1: -i?5 , a-4 , a- , a-15 ,20.

  • 8/18/2019 F 201Array

    18/22

    Complile Time ArraysConsider the follo ing declaration)7#A@, !"#$'!&$-10 )) A'he abo e statement causes the compiler to allocate a blockof memory large enough to hold 10 real alues. 'he siGe ofthis block cannot be altered except by recompiling the array

    ith a different siGe. A compile time H fixed siGe array suffer from t o problems)

    !f the siGe of the array exceeds the number of alues to be stored

    in it, then memory is asted by the unused elements.!f the siGe of the array is smaller than the number of alues to bestored in it, then there is a problem of array o erflo .

  • 8/18/2019 F 201Array

    19/22

    Run Time Arrays7un time or allocatable arrays are those in hich the memory isallocated during execution instead of during compilation.

    A@@&CA'AE@# attribute is re(uired in the declaration of runtime arrays)

    type, !"#$%!&$ -) , A@@&CA'AE@# )) array/name'he actual bounds of an allocatable array are specified in an

    A@@&CA'# statement) A@@&CA'# -list, %'A' ? status/ ariable

    here list is a list of array specifications of the formarray/name -l)uand %'A' clause -optional is assigned to an integer ariablethat ill be set to Gero if allocation is successful or some systemerror alue if there is insufficient memory or if the array isalready been allocated.

  • 8/18/2019 F 201Array

    20/22

    E ampleREAL, DIMENSION(:), ALLOCATABLE :: A, BPRINT , !Enter t"e s#$e o% t"e arra& A and B'READ , N

    ALLOCATE (A(N), B( :N *), STAT + A ocateStatus)I- (A ocateStatus . + ) t"en

    PRINT , !Not enou " 0e0or&'E se

    ot"er state0ents11

  • 8/18/2019 F 201Array

    21/22

    !assin" Arrays to Subpro"rams

  • 8/18/2019 F 201Array

    22/22

    Intrinsi# Array Subpro"rams

    A ocated(A) 2 Returns true #% 0e0or& "as 3eena ocated to t"e a ocata3 e arra& A and %a seot"er4#se5

    Dot6Product(A, B) 2 Returns t"e dot 7roduct o% arra&sA and 85Ma9va (A) 2 Returns t"e 0a9#0u0 va ue #n arra& A5M#nva (A) 2 Returns t"e 0#n#0u0 va ue #n arra& A5Product(A) 2 Returns t"e 7roduct o% e e0ents #n A5S#$e(A) 2 Returns t"e nu03er o% e e0ents #n A5Su0(a) 2 Returns t"e su0 o% t"e e e0ents #n A5