Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty...

17
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: [email protected]
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    224
  • download

    1

Transcript of Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty...

Page 1: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Chapter 7 Introduction to Arrays

Part I

Dr. Ali Can Takinacıİstanbul Technical University

Faculty of Naval Architecture and Ocean Engineeringİstanbul - Turkey

Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: [email protected]

Page 2: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

Introduction to Arrays

•An array is a group of variables or constants, all of the same type, that are referred to by a single name. •The values in the group occupy consecutive locations in the computer's memory.• An individual value within the array is called an array element; it is identified by the name of the array together with a subscript pointing to the particular location within the array. •For example, the first variable shown in Figure .•The subscript of an array is of type INTEGER.• Either constants or variables may be used for array

subscripts.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 2

Page 3: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

• As we shall see, arrays can be extremely powerful tools. • They permit us to apply the same algorithm over and over

again to many different data items with a simple DO loop.• For example, suppose that we need to take the square root

of 100 different real numbers. • If the numbers are stored as elements of an array a

consisting of 100 real values, then the code

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 3

Page 4: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

Declaring Arrays

•Before an array can be used, its type and the number of elements it contains must be declared to the compiler in a type declaration statement, so that the compiler will know what sort of data is to be stored in the array, and how much memory is required to hold it. •For example, a real array voltage containing 16 elements could be declared as follows.

•Similarly, an array of fifty 20-character-long variables could be declared as above.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 4

Page 5: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

• Array constants may also be defined. • An array constant is an array consisting entirely of

constants. • It is defined by placing the constant values between • special delimiters, called array constructors. • The starting delimiter of a Fortran 95 array constructor is (/,

and the ending delimiter of an array constructor is /). • For example, the expression shown below defines an array

constant containing five integer elements:

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 5

Page 6: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

USING ARRAY ELEMENTS IN FORTRAN STATEMENTS Array Elements Are Just Ordinary Variables

•Each element of an array is a variable just like any other variable, and an array element may be used in any place where an ordinary variable of the same type may be used. •Array elements may be included in arithmetic and logical expressions, and the results of an expression may be assigned to an array element. •For example, assume that arrays index and temp are declared as:

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 6

Page 7: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

Initialization of Array Elements

•Just as with ordinary variables, the values in an array must be initialized before use. •If an array is not initialized, the contents of the array elements are undefined. In the following Fortran statements, array j is an example of an uninitialized array.

•The array j has been declared by the type declaration statement, but no values have been placed into it yet. •Since the contents of an uninitialized array are unknown and can vary from computer to computer, the elements of the array should never be used until they are initialized to known values.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 7

Page 8: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

The elements in an array may be initialized by one of three techniques:

1. Arrays may be initialized by using assignment statements. 2. Arrays may be initialized in type declaration statements at

compilation time. 3. Arrays may be initialized by using READ statements.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 8

Page 9: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

1. Initializing arrays with assignment statements

•Initial values may be assigned to the array by using assignment statements, either element by element in a DO loop or all at once with an array constructor. •For example, the following DO loop will initialize the elements of array array 1 to 0.0, 2.0, 3.0, etc. one element at a time:

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 9

Page 10: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

A program to calculate the squares of the integers from 1 to 10, using assignment statements to initialize the values in array number.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 10

Page 11: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

2. Initializing arrays in type declaration statements

•Initial values may be loaded into an array at compilation time by declaring their values in a type declaration statement. •To initialize an array in a type declaration statement, we use an array constructor to declare its initial values in that statement. •For example, the following statement declares a five-element

•The five-element array constant (/ 1, 2, 3, 4, 5 I) was used to initialize the five-element array array2. •In general, the number of elements in the constant must match the number of elements in the array being initialized. •Either too few or too many elements will result in a compiler error. •This method works well to initialize small arrays, but what do we do if the array has 100 (or even 1000) elements?

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 11

Page 12: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

• Writing out the initial values for a 100-element array would be very tedious and repetitive.

• To initialize larger arrays, we can use an implied DO loop. • An implied DO loop has the general form

• Initializing arrays with READ statements • Arrays may also be initialized with READ statements. • The use of arrays in I/O statements will be described in

detail in next week.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 12

Page 13: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

A program to calculate the square roots of the integers from 1 to 10, using a type declaration statement to initialize the values in array value.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 13

Page 14: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

USING WHOLE ARRAYS AND ARRAY SUBSETS IN FORTRAN STATEMENTS

•Under certain circumstances, whole arrays may be used in arithmetic calculations as though they were ordinary variables. •If two arrays are the same shape, then they can be used in ordinary arithmetic operations, and the operation will be applied on an element-by-element basis (see Figure).

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 14

Page 15: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I© 2010, Dr. ALİ CAN TAKİNACI Slide No: 15

Page 16: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

• Two arrays can be used as operands in an intrinsic operation (addition etc.) if and only if they have the same shape.

• This means that they must have the same number of dimensions (the same rank), and the same number of elements in each dimension (the same extent).

• Two arrays of the same shape are said to be conformable.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 16

Page 17: Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Introduction to Arrays - Part I

Array Subsets

•A subset of an array is called an array section. •It is specified by replacing an array subscript with a subscript triplet or vector subscript. •A subscript triplet has the general form

•where subscript_1 is the first subscript to be included in the array subset, • subscript_2 is the last subscript to be included in the array subset, and stride is the subscript increment through the data set.

•Then the array subset array (1:10:2) would be an array containing only elements array(1), array(3), array(5), array(7), and array(9).

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 17