An introduction to arrays WEEK 7 Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values,

Slides:



Advertisements
Similar presentations
Introduction to arrays
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Need for Arrays Exercise Read the IDs and the grades for all ICS 101 students. Compute and print the average of the students. Print the grades and IDs.
Fortran: Array Features Session Five ICoCSIS. Outline 1.Zero-sized Array 2.Assumed-shaped Array 3.Automatic Objects 4.Allocation of Data 5.Elemental Operations.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Arrays Array Terminology Declarations Visualisation of Arrays Array Conformance Array Element Ordering Array Syntax Whole Array Expressions Array Sections.
Chapter 10 Additional Features of Arrays Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Summary of previous weeks Introduction to Scientific & Engineering Computing.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 10 Modules and programming with subroutines.
Chapter 8 ARRAYS. 2 Array subscript expressions  Each subscript in an array element designator is an expression of integer type. It need not be a constant.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Structured Data Types. Date Types We have seen various data types –Integer, Real, Character, Logical All these types define data values of different kinds.
5  Systems of Linear Equations: ✦ An Introduction ✦ Unique Solutions ✦ Underdetermined and Overdetermined Systems  Matrices  Multiplication of Matrices.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Fortran: Specification Statements Session Six ICoCSIS.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Fortran: Program Units and Procedures Session Four ICoCSIS.
IE 212: Computational Methods for Industrial Engineering
Multi-Dimensional Arrays
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Chapter 8 Arrays and Strings
Week 8 Improving on building blocks Recursive procedures.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Module and Data Sharing. Programming in the Large Software, in general, is large having multiple units Multiple units designed and developed independently.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables.
Arrays Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such as vectors and matrices.
An introduction to arrays. Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2005 Pearson Education, Inc. All rights reserved Arrays.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Objectives You should be able to describe: One-Dimensional Arrays
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
5 Systems of Linear Equations and Matrices
Programming For Nuclear Engineers Lecture 6 Arrays
Array processing and Matrix manipulation
JavaScript: Functions.
Subroutine Comp 208 Yi Lin.
Midterm Review Programming in Fortran
CIS16 Application Development and Programming using Visual Basic.net
7 Arrays.
Array processing and Matrix manipulation
Presentation transcript:

An introduction to arrays WEEK 7

Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such as vectors and matrices. There is a common requirement in many applications to repeat the same sequence of operations on successive sets of data. In order to handle both of these requirements, F provides extensive facilities for grouping a set of items of the same type into an array which can be operated on  either as an object in its own right  or by reference to each of its individual elements.

The array concept A

n A set with n (=6) object, named A n In mathematical terms, we can call A, a vector n And we refer to its elements as A 1, A 2, A 3, …

The array concept n In F, we call such an ordered set of related variables an array n Individual items within an array array elements n A(1), A(2), A(3), …, A(n)

Array Concept In all the programs we have used one name to refer to one location in the computer’s memory. It is possible to refer a complete set of data obtained for example from repeating statements, etc. To do this is to define a group called “array” with locations in the memory so that its all elements are identified by an index or subscript of integer type. An array element is designated by the name of the array along with a parenthesized list of subscript expressions.

The array concept n Subscripts can be: x(10) y(i+4) z(3*i+max(i, j, k)) x(int(y(i)*z(j)+x(k))) x(1)=x(2)+x(4)+1 print*,x(5)

The array concept A A 20 A 31

Array declarations type, dimension(subscript bounds) :: list_of_array_names type, dimension(n:m) :: variable_name_list real, dimension(0:4) :: gravity, pressure integer, dimension(1:100) :: scores logical, dimension(-1, 3) :: table

Examples for Shape Arrays real, dimension ( 0 : 49 ) : : z ! the array “z” has 50 elements real, dimension ( 11 : 60 ) : : a, b, c ! a,b,c has 50 elements real, dimension ( -20 : -1 ) : : x ! the array “x” has 20 elements real, dimension ( -9 : 10 ) : : y ! the array “y” has 20 elements

Array declarations n Up to 7 dimensions n Number of permissible subscripts: rank n Number of elements in a particular dimension:extent n Total number of elements of an array: size n Shape of an array is determined by its rank and the extent of each dimension

Array constants and initial values Since an array consists of a number of array elements, it is necessary to provide values for each of these elements by means of an “ array constructor “. In its simplest form, an array constructor consists of a list of values enclosed between special delimiters :  the first of which consists of the 2 characters : ( /  the second of the 2 characters : / ) (/ value_1, value_2, ……………………., value_n /)

Examples integer, dimension ( 10 ) : : arr arr = (/ 3, 5, 7, 3, 27, 8, 12, 31, 4, 22 /) arr ( 1 ) = 3 arr ( 5 ) = 27 ! the value 27 is storen in the 5 th location of the array “arr” arr ( 7 ) = 12 arr ( 10 ) = 22

Array constructors n (/ value_1, value_2, … /) arr = (/ 123, 234, 567, 987 /) n Regular patterns are common: implied do (/ value_list, implied_do_control /) n arr = (/ (i, i = 1, 10) /) n arr = (/ -1, (0, i = 1, 48), 1 /)

Initialization n You can declare and initialize an array at the same time: integer,dimension(50)::my_array=(/(0,i=1,50)/)

Input and output n Array elements treated as scalar variables n Array name may appear: whole array n Subarrays can be referred too EXAMPLE : integer, dimension ( 5 ) : : value read *, value read *, value(3)

real, dimension(5) :: p, q integer, dimension(4) :: r print *, p, q(3), r read *, p(3), r(1), q print *, p, q (3), q (4), r print *, q (2) ! displays the value in the 2 nd location of the array “q” print *, p ! displays all the values storen in the array “p” Examples

Using arrays and array elements... The use of array variables within a loop, therefore, greatly increases the power and flexibility of a program. F enables an array to be treated as a single object in its own right, in much the same way as a scalar object. Assignment of an array constant to an array variable will be performed as is seen below : array_name = (/ list_of_values /) array_name = (/ value_1, value_2, ….., value_n /) An array element can be used anywhere that a scalar variable can be used a(2) = t(5) - t(3)*q(2) a(i) = t(j) - f(k)

Using arrays and array elements... n An array can be treated as a single object –Two arrays are conformable if they have the same shape –A scalar, including a constant, is conformable with any array –All intrinsic operations are defined between two conformable objects

Using arrays and array elements.... real, dimension(20) :: a, b, c. a = b*c do i = 1, 20 a(i) = b(i)*c(i) end do Arrays having the same number of elements may be applied to arrays and simple expressions. In this case, operation applied toan array are carried out elementwise.

Example integer, dimension ( 4 ) : : a, b integer, dimension ( 0 : 3 ) : : c integer, dimension ( 6 : 9 ) : : d a = (/ 1, 2, 3, 4 /) b = (/ 5, 6, 7, 8 /) c = (/ -1, 3, -5, 7 /) c(0) c(1) c(2) c(3) a = a + b ! will result a = (/ 6, 8, 10, 12 /) d = 2 * abs ( c ) + 1 ! will result d = (/ 3, 7, 11, 15 /) d(6) d(7) d(8) d(9)

Intrinsic procedures with arrays n Elemental intrinsic procedures with arrays array_1 = sin(array_2) arr_max = max(100.0, a, b, c, d, e) n Some special intrinsic functions: maxval(arr) maxloc(arr) minval(arr) minloc(arr) size(arr) sum(arr)

Sub-arrays n Array sections can be extracted from a parent array in a rectangular grid usinf subscript triplet notation or using vector subscript notation n Subscript triplet: subscript_1 : subscript_2 : stride Similar to the do – loops, a subscript triplet defines an ordered set of subscripts beginning with subscript_1, ending with subscript_2 and considering a seperation of stride between the consecutive subscripts. The value of stride must not be “zero”.

Sub-arrays n Subscript triplet: subscript_1 : subscript_2 : stride n Simpler forms: subscript_1 : subscript_2 subscript_1 : subscript_1 : : stride : subscript_2 : subscript_2 : stride : : stride :

Example real, dimension ( 10 ) : : arr arr ( 1 : 10 ) ! rank-one array containing all the elements of arr. arr ( : ) ! rank-one array containing all the elements of arr. arr ( 3 : 5 ) ! rank-one array containing the elements arr (3), arr(4), arr(5). arr ( : 9 ) ! rank-one array containing the elements arr (1), arr(2),…., arr(9). arr ( : : 4 ) ! rank-one array containing the elements arr (1), arr(5),arr(9).

Example integer, dimension ( 10 ) : : a integer, dimension ( 5 ) : : b, i integer : : j a = (/ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 /) i = (/ 6, 5, 3, 9, 1 /) b = a ( i ) ! will result b = ( 66, 55, 33, 99, 11 /) a ( 2 : 10 : 2 ) ! will result a = ( 22, 44, 66, 88, 110 /) a ( 1 : 10 : 2 ) = (/ j ** 2, ( j = 1, 5 ) /) ! will result a = ( 1, 4, 9, 16, 25 /) a(1) a(3) a(5) a(7) a(9)

Type of printing sub-arrays work = ( / 3, 7, 2 /) print *, work (1), work (2), work (3) ! or print *, work ( 1 : 3 ) ! or print *, work ( : : 1 ) ! or integer : : i do i = 1, 3, 1 print *, work (i) end do ! or another example print *, sum ( work (1: 3) ) ! or another example print *, sum ( work (1: 3 : 2) )

Type of INPUT of sub-arrays integer, dimension ( 10 ) : : a integer, dimension ( 3 ) : : b. b = a ( 4 : 6 ) ! b (1 ) = a (4) ! b (2 ) = a (5) ! b (3 ) = a (6). a ( 1 : 3 ) = 0! a(1) = a(2) = a(3) = 0 a ( 1 : : 2 ) = 1! a(1) = a(3) =…….= a(9) = 1 a ( 1 : : 2 ) = a ( 2 : : 2 ) + 1 ! a(1) = a(2) +1 ! a(3) = a(4) +1 ! a(5) = a(6) +1 ! etc.

PROBLEM : Write a function that takes 2 real arrays as its arguments returns an array in which each element is the maximum of the 2 corresponding elements in the input array function max_array ( array_1, array_2 ) result (maximum_array) ! this function returns the maximum of 2 arrays on an element-by- element basis ! dummy arguments real, dimension ( : ), intent (in) : : array_1, array_2 ! result variable real, dimension ( size ( array_1 ) ) : : maximum_array ! use the intrinsic function “max” to compare elements maximum_array = max ( array_1, array_2) ! or use a do-loop instead of the intrinsic function max to compare elements ! do i = 1, size (array_2) ! maximum_array ( i ) = max ( array_1 ( i ), array_2 ( i ) ) ! end do end function max_array

program normal_probability integer, parameter :: ndim = 100 real, parameter :: pi = , two_pi = 2.0*pi real, dimension(ndim) :: phi_x, x real :: del_x integer :: i, n, step step = 5 !use do construct i = 1 x(i) = -3.0 del_x = 0.2 do if (x(i) > del_x) then exit end if phi_x(i) = exp(-x(i)**2/2.0)/sqrt(two_pi) i = i + 1 x(i) = x(i-1) + del_x end do n = i-1 do i = 1, n write(unit=*,fmt="(1x,i3,2x,f8.5,2x,f8.5)") i,x(i),phi_x(i) end do do i = 1, n, step print "(5f8.5)", phi_x(i:i+step-1) end do end program normal_probability

program vector_manipulation real, dimension(3) :: a, b, c real :: dot_pro a = (/1., -1., 0./) b =(/2., 3., 5./) dot_pro = dots_product(a,b) call vector_product(a,b,c) print *, "the dot product of two vectors is:",dot_pro print *, "the vector product of two vectors is:",c end program vector_manipulation !***************************************************** real function dots_product(a,b) result(result) real, dimension(3),intent(in):: a, b result = a(1)*b(1) + a(2)*b(2) + a(3)*b(3) end function dots_product !****************************************************** subroutine vector_product(a,b,c) real, dimension(3),intent(in):: a, b real, dimension(3),intent(out) :: c c(1) = a(2)*b(3) - a(3)*b(2) c(2) = a(3)*b(1) - a(1)*b(3) c(3) = a(1)*b(2) - a(2)*b(1) end subroutine vector_product

If we wish to use an array as an argument to a procedure, it is needed to know the size or shape of the actual arguments used. On the other hand, as mentioned before a procedure does not need to know the details of the calling program unit the program unit called, in turn, also does not need to know anything about the procedure except the information about its arguments which form part of the procedure’s interface.

In fact, if an array is a procedure dummy argument, it must be declared in the procedure as an assumed-shape array. An assumed-shape array is a dummy argument array wose shape, as its mame implies, is not known but which assumes the shape as that of any actual argument that becomes associated with it. The array specification for an “assumed-shape array” can take 2 forms as can be seen below, where the 2 nd form is equivalent to the 1 st with a “lower bound = 1” and on the other hand, the upper bound in both cases will only be established on entry to the procedure and will be whatever value is necessary to ensure that the extent of dummy array is the same as that of the actual array argument : * ( lower bound : )

Arrays and procedures n Explicit-shape array: no freedom! n Assumed-shape array –If an array is a procedure dummy argument, it must be declared in the procedure as an assumed- shape array!

Arrays and procedures Calling programProcedure Assumed-shape Explicit-shape

Arrays and procedures

Example main program unit: real, dimension ( b : 40 ) : : a subprogram: real, dimension ( d : ) : : x x ( d )  a ( b ) x ( d + 1 )  a ( b + 1 ) x ( d + 2 )  a ( b + 2 ) x ( d + 3 )  a ( b + 3 ).

Example main program unit: real, dimension ( 10 : 40 ) : : a, b. call array_example_l (a,b) subroutine array_example_1 ( dummy_array_1, dummy_array_2) real, intent (inout), dimension : : dummy_array_1, dummy_array_2. subprogram:

Example main program unit real : : p (-5 : 5 ), q ( 100 ). call array_example_2 ( p, q ). subprogram subroutine array_example_2 ( dummy_array_1, dummy_array_2) real, intent (inout), dimension : : dummy_array_1, dummy_array_2.

Example integer, dimension ( 4 ) : : section = (/ 5, 1, 2, 3 /) real, dimension ( 9 ) : : a call array_example_3 ( a ( 4 : 8 : 2 ), a ( section ) ) dummy_array_1 = (/ a (4), a (6), a (8) /) dummy_array_2 = (/ a (5), a (1), a (2), a (3) /) dummy_argument_2 (4) dummy_argument_2 (3) dummy_argument_2 (2) dummy_argument_2 (1)

Example PROBLEM : Write a subroutine that will sort a set of names into alphabetic order. İnitial order After 1 st exc After 2 nd exc After 3 rd exc After 4 th exc After 5 th exc After 6 th exc After 7 th exc

Array-valued functions As is well known, arrays can be passed to procedures as arguments and a subroutine can return information by means of an array. It is convenient for a function to return its result in the form of an array of values rather than as a single scalar value  array-valued function, as can be seen below : function name ( ……………………..) result (arr) real, dimension ( dim ) : : arr. end function name

Array-valued functions n A function can return an array n Such a function is called an array-valued function function name(…..) result(arr) real, dimension(dim) :: arr... end function name Explicit-shape

Example Consider a trivial subroutine that simply adds 2 arrays together, returning the result through a third dummy argument array. subroutine trivial_fun ( x, y ) result (sumxy) real, dimension ( : ), intent (in) : : x, y real, dimension ( size (x) ) : : sumxy end subroutine trivial_fun

Example Write a function that takes 2 real arrays as its arguments, returns an array in which each element is the maximum of the 2 corresponding elements in the input array function max_array ( array_1, array_2 ) result (maximum_array) ! this function returns the maximum of 2 arrays on an element-by-element basis dummy arguments real, dimension ( : ), intent (in) : : array_1, array_2 ! result variable real, dimension ( size ( array_1 ) ) : : maximum_array ! use the intrinsic function “max” to compare elements maximum_array = max ( array_1, array_2) ! or use a do-loop instead of the intrinsic function max to compare elements do i = 1, size (array_2) maximum_array ( i ) = max ( array_1 ( i ), array_2 ( i ) ) end do end function max_array

Matrices and 2-d arrays n In mathematics, a matrix is a two- dimensional rectangular array of elements (that obeys to certain rules!) A= Row numberColumn number

F extends the concept of a one-dimensional array in a natural manner, by means of the “ dimension” attribute. Thus, to define a two-dimensional array that could hold the elements of the matrix A, we would write : real, dimension ( 3, 4 ) : : A numbers of rows numbers of columns

Matrices and 2-d arrays n Similarly, a vector is (given a basis) a one-dimensional array of elements (that obeys to certain rules!)

2-d arrays dimension attribute: real, dimension(3, 4) :: a logical, dimension(10,4) :: b, c, d n You can refer to a particular element of a 2-d array by specifing the row and column numbers: a(2,3) b(9,3)

Intrinsic functions n matmul(array, array) dot_product(array, array) transpose(array) maxval(array[,dim,mask]) maxloc(array[,dim]) minval(array[,dim,mask]) minloc(array[,dim]) product(array[,dim,mask]) sum(array[,dim,mask])

program vectors_and_matrices integer, dimension(2,3) :: matrix_a integer, dimension(3,2) :: matrix_b integer, dimension(2,2) :: matrix_ab integer, dimension(2) :: vector_c integer, dimension(3) :: vector_bc ! Set initial value for vector_c vector_c = (/1, 2/) ! Set initial value for matrix_a matrix_a(1,1) = 1! matrix_a is the matrix: matrix_a(1,2) = 2 matrix_a(1,3) = 3 ! [1 2 3] matrix_a(2,1) = 2 ! [2 3 4] matrix_a(2,2) = 3 matrix_a(2,3) = 4 ! Set matrix_b as the transpose of matrix_a matrix_b = transpose(matrix_a) ! Calculate matrix products matrix_ab = matmul(matrix_a, matrix_b) vector_bc = matmul(matrix_b, vector_c) end program vectors_and_matrices

Some basic array concepts... n Rank number of its dimensions Examples: real, dimension(8) :: a integer, dimension(3, 100, 5) :: b logical, dimension(2,5,3,100,5) :: c

Some basic array concepts... n Extent number of elements in each dimension Examples: real, dimension(8) :: a real, dimension(-4:3) :: b integer, dimension(3,-50:50,40:44) :: c integer, dimension(0:2, 101, -1:3) :: d

Some basic array concepts... n Size total number of elements Examples: real, dimension(8) :: a integer, dimension(3, 100, 5) :: b logical, dimension(2,5,4,10,5) :: c

Some basic array concepts... n Shape –Rank –Extent in each dimension n Shape of an array is representable as a rank- one integer array whose elements are the extents Example logical, dimension(2,-3:3,4,0:9,5) :: c shape_c = (/ 2,7,4,10,5 /)

Some basic array concepts... n Array element order first index of the element specification is varying most rapidly, … Example integer, dimension(3,4) :: a a(1,1), a(2,1), a(3,1) a(1,2), a(2,2), a(3,2)...

Array constructors for rank-n arrays n An array constructor always creates a rank-one array of values n Solution? reshape function reshape((/ (i, i=1,6) /), (/ 2, 3 /))

Example : The implied-do loop given below provides the following matrix integer : : i, j real, save, dimension ( 2,2 ) : : a = & reshape ( ( / (10*i + j, i = 1,2 ), j = 1, 2) / ), (/ 2, 2 /) )

Array I/O n As a list of individual array elements: a(1,1), a(4,3), … As the complete array: real, dimension(2,4) :: a print *, a a(1,1), a(2,1), a(1,2), a(2,2),... n As an array section Could be handled in three different ways

The four classes of arrays n Explicit-shape arrays n Assumed-shape arrays n Allocatable (deferred-shape) arrays n Automatic arrays

Explicit-shape arrays Arrays whose index bounds for each dimension are specified when the array is declared in a type declaration statement real, dimension(35, 0:26, 3) :: a

Assumed-shape arrays n Only assumed shape arrays can be procedure dummy arguments n Extents of such arrays is defined implicitly when an actual array is associated with an assumed-shape dummy argument n subroutine example(a, b) integer, dimension(:,:), intent(in) :: a integer, dimension(:), intent(out) :: b

Automatic arrays n A special type of explicit-shape array n It can only be declared in a procedure n It is not a dummy argument n At least one index bound is not constant n The space for the elements of an automatic array is created dynamically when the procedure is entered and is removed upon exit from the procedure

Automatic arrays subroutine abc(x) ! Dummy arguments real, dimension(:), intent(inout):: x ! Assumed shape ! Local variables real, dimension(size(x)) :: e !Automatic real, dimension(size(x), size(x)) :: f !Automatic real, dimension(10) :: !Explicit shape. end subroutine abc

Allocatable arrays n Allocation and deallocation of space for their elements us completely under user control n Flexible: can be defined in main programs, procedures and modules

Allocatable arrays n Steps: –Array is specified in a type declaration statement –Space is diynamically allocated for its elements in a separate allocation statement –The arrays is used (like any other array!) –Space for the elements is deallocated by a deallocation statement

Allocatable arrays Declaration type, allocatable, dimension(:,:,:) :: allocatable array real, allocatable, dimension(:,:,:) :: my_array type(person), allocatable, dimension(:) :: personel_list integer, allocatable, dimension(:,:) :: big_table

Allocatable arrays Allocation allocate(list_of_array_specs, [stat=status_variable]) allocate(my_array(1:10,-2:3,5:15), personel_list(1:10)) allocate(big_table(0:50000,1:100000), stat=check) if (check /= 0) then print *, "No space for big_table" stop end if

Allocatable arrays Deallocation deallocate(list_of_currently_allocated_arrays, [stat=status variable]) deallocate(my_array, personel_list, big_table)

Whole-array operations n Whole array operations can be used with conformable arrays of any rank –Two arrays are conformable if they have the same shape –A scalar, including a constant, is conformable with any array –All intrinsic operations are defined between conformable arrays

Whole-array operations n Relational operators follow the same rules: n Example:

Masked array assignment where (mask_expression) array_assignment_statements end where n where (my_array > 0) my_array = exp(my_array) end where

Masked array assignment where (mask_expression) array_assignment_statements elsewhere array_assignment_statements end where n where (my_array > 0) my_array = exp(my_array) elsewhere my_array = 0.0 end where

Sub-arrays n Array sections can be extracted from a parent array of any rank Using subscript triplet notation: first_index, last_index, stride n Resulting array section is itself an array

Sub-arrays n integer, dimension(-2:2, 0:4) :: tablo tablo(-1:2, 1:3) Rank-2

n integer, dimension(-2:2, 0:4) :: tablo tablo(2, 1:3) Sub-arrays Rank-1