Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 8 Introduction to Arrays Part II Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul."— Presentation transcript:

1 Chapter 8 Introduction to Arrays Part II 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: takinaci@itu.edu.tr

2 Introduction to Arrays – Part II Input and Output of Array Elements We previously stated that an array element is a variable just like any other variable, and that an array element may be used in any place where an ordinary variable of the same type may be used. Therefore, READ and WRITE statements containing array elements are just like READ and WRITE statements for any other variables. To write out specific elements from an array, just name them in the argument list of the WRITE statement. For example, the following code writes out the first five elements of the real array a. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 2

3 Introduction to Arrays – Part II The Implied DO Loop The implied DO loop is also permitted in I/O statements. It allows an argument list to be written many times as a function of an index variable. Every argument in the argument list is written once for each value of the index variable in the implied DO loop. With an implied DO loop, the previous statement becomes: The argument list in this case contains only one item: a(i). This list is repeated once for each value of the index variable i. Since i takes on the values from 1 to 5, the array elements a(1), a(2), a(3), a (4), and a(5) will be written. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 3

4 Introduction to Arrays – Part II The general form of a WRITE or READ statement with an implied DO loop is: where arg1, arg2, etc., are the values to be written or read. The variable index is the DO loop index, and istart, iend, and incr are respectively the starting value, ending value, and increment of the loop index variable. The index and all of the loop control parameters should be of type INTEGER. For a WRITE statement containing an implied DO loop, each argument in the argument list is written once each time the loop is executed. Therefore, a statement like © 2010, Dr. ALİ CAN TAKİNACI Slide No: 4

5 Introduction to Arrays – Part II © 2010, Dr. ALİ CAN TAKİNACI Slide No: 5

6 Introduction to Arrays – Part II Nested Implied DO Loops Like ordinary DO loops, implied DO loops may be nested. If they are nested, the inner loop will execute completely for each step in the outer loop. As a simple example, consider the following statements There are two implicit DO loops in this WRITE statement. The index variable of the inner loop is j, and the index variable of the outer loop is i. When the WRITE statement is executed, variable j will take on values 1, 2, and 3 while i is 1, and then 1, 2, and 3 while i is 2. The output from this statement will be Nested implied DO loops are important in working with arrays having two or more dimensions, as we will see later in Chapter 8. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 6

7 Introduction to Arrays – Part II Input and Output of Whole Arrays and Array Sections Entire arrays or array sections may also be read or written with READ and WRITE statements. If an array name is mentioned without subscripts in a Fortran I/O statement, then the compiler assumes that every element in the array is to be read in or written out. If an array section is mentioned in a Fortran I/O statement, then the compiler assumes that the entire section is to be read in or written out. Figure 6-11 shows a simple example of using an array and two array sections in I/O statements. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 7

8 Introduction to Arrays – Part II © 2010, Dr. ALİ CAN TAKİNACI Slide No: 8

9 Introduction to Arrays – Part II Summary of Good Programming Practice The following guidelines should be adhered to when working with arrays. 1. Before writing a program that uses arrays, you should decide whether an array is really needed to solve the problem or not. If arrays are not needed, don't use them! 2. All array sizes should be declared by using named constants. If the sizes are declared by using named constants, and if those same named constants are used in any size tests within the program, then it will be very easy to modify the maximum capacity of the program at a later time. 3. All arrays should be initialized before use. The results of using an uninitialized array are unpredictable and vary from processor to processor. 4. The most common problem in programming with arrays is attempting to read from or write to locations outside the bounds of the array. To detect these problems, the bounds checking option of your compiler should always be turned on during program testing and debugging. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 9


Download ppt "Chapter 8 Introduction to Arrays Part II Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul."

Similar presentations


Ads by Google