Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# 14 arrays

87

Published on

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
87
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Basic Scientific Programming Arrays
• 2. Arrays Declaration   type, dimension (l:u):: array_name or type:: array_name l,u: are integer constants. Ex: integer, dimension(40)::grades integer:: grades(40) integer:: grades(0:39), avg(1:40) integer:: final(21:60)
• 3. Ex:   To read 6 number from the keyboard. Real, dimension(6):: example integer:: j do j =1,6 print*, ‘Enter a number’ read*, example(j) end do Or print*, ‘please enter 6 numbers’ read*, example
• 4. Compile Time & Run Time Arrays     The memory for grades is allocated at compile time. This means that the size of such compile-time arrays is fixed before execution begins. If the data to be stored in the array is too large, it cannot be stored in the array and processed correctly. To deal with this problem, we use run-time arrays for which memory is allocated during execution.
• 5. Allocatable Arrays  Integer :: N print*, ‘ how many elements?’ read*, N Real:: Grades(N) THIS IS WRONG !!
• 6. Declaration of Allocatable Arrays  Type, dimension, Allocatable:: Array_name the actual bounds of an allocatable array can be specified in an Allocate statement. Form Allocate(array_name) Allocate(array_name,Stat= StatusVariable)
• 7. Ex:    Print*, ‘Enter size of arrays A and B:’ read*, N Allocate(A(N),B(0:N+1), stat=allostatus) if(allostatus /= 0) stop “not enough memory” If memory is available, A can store N values and be can store N+2 values Once memory has been allocated, these arrays may be used in the same ways as compile-time arrays.
• 8. Deallocate  Memory that is not needed anymore should be cleared so that we can use it for allocating other arrays. Form deallocate(grades) or deallocate(grades,stat=StatusVar) Please read the example in page 530.
• 9. Array Constants  An array constant may be constructed as a list of values enclosed between (/ and /) (/ Value1, Value2, … , Value n /) Ex: integer:: A(5) A= (/ 2,4,6,8,10/) A= ( /(2*I, I = 1,5)/ )
• 10. Array Expressions   Operators and functions normally applied to simple expressions may also be applied to arrays having the same number of elements. Operations applied to an array are carried out element wise.
• 11.   Integer,dimension(4):: A,B,C … A= B + C Do I = 1,4 A(I)= B(I)+ C(I) end do
• 12.   A=2*C or Do I = 1,4 A(I)= 2 * C(I) End do C = 0 assigns zeros to all elements of C
• 13. Array Sections and Subarrays  In some cases, it is desired to have access to only a part of the array. We can either use Do loops or subarrays. Form array_name(lower:upper:stride) integer:: A(10) A=(/11,22,33,44,55,66,77,88,99,110/) print*, A(2:10:2) prints A(2), A(4), A(6), A(8), A(10)
• 14. The WHERE Construct  May be used to assign values to arrays depending on the value of a logical array expression. Form where (logical_array_expr) array_var1= array_expr1 Elsewhere array_var2= array_expr2 End where
• 15. Ex:  Integer, dimension(5):: A=(/0,2,5,0,10/) Real:: B(5) Where (A>0) B= 1.0/ Real(A) elsewhere B= -1.0 end where B will store –1.0, 0.5, 0.2, -1.0, 0.1
• 16.  Same as Do I=1,5 if (A(I) > 0) then B(I) = 1.0/ Real(A(I)) else B(I) = -1.0 end if end do
• 17. Intrinsic Array-Processing Subprograms  Fortran 90 provides several intrinsic functions whose arguments are arrays, some of the useful ones are: Dot_product(A,B) Maxval(A) Maxloc(A) Minval(A) Minloc(A) refer to page 548 and Appendix D.
• 18. Programmer-defined Array-Processing Subprograms  The actual array argument must be declared in the calling program unit, and the corresponding formal argument must be declared in the subprogram. … Actual Array Arg. … Formal Array Arg.
• 19. Array Valued Functions  A function may return an array as the return argument.
• 20. 2-Dimensional Arrays     2-D arrays have two indices to indicate rows and columns.(matrix) The first index corresponds to rows, the second to columns. Real:: Matrix(5,3) declares a matrix with 5 rows and 3 columns. Integer, dimension(22:28,1:24):: hours declares a matrix of 7 rows and 24 columns
• 21.  2D arrays follow all the rules 1D arrays have. (1,1) (1,2) (1,3) (2,1) (2,2) (2,3) (3,1) (3,2) (3,3) (4,1) (4,2) (4,3) (5,1) (5,2) (5,3)
• 22. Do I=1,5 Do J = 1,5 matrix(I,J) = I * J end do 1 2 3 End do 2 3 4 5 4 4 6 8 6 9 12 8 12 16 10 15 20 5 10 15 20 25