Arrays

           

References:     Section 7 in the FORTRAN95 Manual and the Fortran Lecture4

 

            A variable:  a named position of memory.

            An array: a collection of variables of the same type that are referenced by a common name. A specific element of an array can be accessed by using a subscript, which is a number indicating the position of the element in the array.

            An array must be declared at the beginning of a program before being used. The declaration defines the type and the size of the array. For example:

                        real x(100)                              ! x is a real type array and it has 100 elements, x(1), x(2)…..x(100)

                        integer  I(0:10)                       ! I is an integer type array and it has 11 elements, I(0), I(1)…..I(10)

                        dimension x(100), I(0:10)      ! x: real type, I: integer type

            When dimension is used to declare an array without a type-declaration statement, array type is determined implicitly; i.e. all are real type, except the first letter of the name is I, J, K, L, M or N.

 

            To get access to every subscript of an array, the easiest way is by using a DO loop, when the number of elements in the array is known. For example, if we know there are 10 numbers need to be put in x(1) to x(10):

   DO I = 1, 10

   READ(*,*) x(i)

   END DO

if we need to print those 10 numbers on screen:

   DO I = 1, 10

   WRITE(*,*) x(i)

   END DO

if we need to get the average of those 10 numbers, :

   sum = 0

   DO I = 1, 10

   sum = sum + x(i)

   END DO

   average = sum / 10

           

            More information on array declaration and initialization can be found in Section 7 in the FORTRAN95 Manual.

 

 

Exercises

 

1)  (optional) Copy the Prog16 (from Fortran Lecture4) into U: drive, save it as prog16.f95 and execute it.

 

PROGRAM PROG16

DIMENSION X(100)

!-----------------------------

! read in a long list of numbers and calculate the average

!------------------------

WRITE (*,*) ‘ Please type in a list of real numbers. To end input type a letter’

N=0

SUM=0.0

AVERAGE=0.0

I=1 ! ARRAY SUBSCRIPT

101 READ (*,1000, ERR=100) X(I) ! trap format errors ourselves

N=N+1

SUM=SUM+X(I)

I=I+1

IF (I.GT.100) GO TO 102

GOTO 101 ! loop until format error

!-----------------------------

! ran out of array memory

!-----------------------------

102 WRITE (*,*) ‘ONLY ROOM FOR 100 NUMBERS’

!-----------------------------

! user ended input

!with non numeric character

!------------------------------

100 IF ( N.GT.0 )AVERAGE=SUM/N

WRITE (*,*) ‘Sum of ‘, N ,’ numbers is ‘,SUM, ‘average is ‘,AVERAGE

!-------------------------------

! we can go back and recheck the data

!-------------------------------

NABOVE=0

IF (N.LE.0) GO TO 106

DO 105 II=1,N

IF(X(II).GT.AVERAGE) NABOVE=NABOVE+1

105 CONTINUE

106 Write (*,*) ‘number above average-=’,NABOVE

! -------------------------------

!sort into numerical order

!-------------------------------


 

!--------------------

! nested do loops compare all the numbers in our list with each other

! outer loop starts with II =1, then inner loop starts with JJ=II

!---------

!

IF (N.EQ.0) STOP

DO 110 II=1,N

DO 110 JJ=II,N

IF (X(II).GE.X(JJ)) GO TO 110

TEMP=X(II)

X(II)=X(JJ)

X(JJ)=TEMP

110 CONTINUE

 

!------------------

!print out answers

!------------------

DO 111 JJ=1,N

WRITE (*,1002) X(JJ)

111 CONTINUE

STOP

1000 FORMAT ( F12.3)

1002 FORMAT (1X,F12.3)

END


 

2) What type is array x? How many elements are declared? What is the subscript range?

3) If IMPLICIT NONE is added in the program, all variables must to be declared before being used. Give all the declaration statements needed in the program (correct types must be used).

4) READ (*,1000, ERR=100) X(I)  ! In this statement, 1000 is a label. Which statement is labelled by 1000 in the program?  ERR=100 is an option in this read statement. What is the function of this option?

5) Write the part of the program, which is for counting the number of data above the average.

6) If input data are: 1, 2, 3, 4, 5, 6; what are the values in AVERAGE and NABOVE at the end of the program?

7) Which numerical order is the sorted data, ascending or descending? How to change from one to the other?

8) (optional) Draw a flowchart for this program.