MULTI-DIMENSIONAL ARRAY




Multi-dimensional Arrays

Lecture written by: Dr. Larry Genalo

Authored for presentation by: Dr. Larry Genalo

Revised by: John Even

HTML documentation by: Julie Sandberg

Date last updated: 6/21/95


Introduction

Subscripted Variable Name: Subscripted Array:
Note: In general they are multidimensional


Multi-Dimensional Arrays






Array Name-------------------> X

Array Indices----------------> I,J

Standard Identification------> X(I,J)

Number of Entries------------> N*M






Subscripted Variable Rules

  1. Subscripted variables can be REAL, INTEGER, or CHARACTER. Don't mix modes!
  2. The maximum number of subscripts is dictated by local convention, but 7 is very common (e.g. Lahey).
  3. Arithmetic expressions may be used for subscripts.
  4. Subscripts must be integer valued.
  5. A statement must be included in your program which sets aside storage space (dimensions) for your arrays.

DIMENSION Statement

The computer stores arrays in a continuous, orderly fashion. The DIMENSION statement provides the following information to the computer so that it can set aside appropriate space for the subscripted arrays in your program:

Some Examples

DIMENSION A(5,3), B(4,15,2), K(2,4)

DIMENSION X(0:8,-2:4), Y(-3:7,10)

REAL LOAD (100,30)

INTEGER RATE (0:30,-3:55)

NOTE: The DIMENSION statement, like
all declaration statements, must appear
before any executable statements


Reading Arrays with 2 EXPLICIT DO LOOPS

	DIMENSION X(5,6)
	OPEN (55,FILE='IN.DAT',STATUS='OLD')
	DO 15 N=1,5
	   DO 10 M=1,6
	      READ(55,20) X(N,M)
     10    CONTINUE
     15 CONTINUE
     20 FORMAT(F10.4)
	CLOSE(55)
NOTE: One X value per line in the input file


Reading Arrays with 1 IMPLICIT DO LOOP
and 1 EXPLICIT DO LOOP

	DIMENSION X(5,6)
	OPEN(55,FILE='IN.DAT',STATUS='OLD')
	DO 10 N=1,5
	   READ(55,30) (X(N,M),M=1,6)
     30    FORMAT(6F10.4)
     10 CONTINUE
	CLOSE(55)
NOTE: Six X values on each line in the input file


Reading Arrays with 2 IMPLICIT DO LOOPS

	DIMENSION X(5,6)
	OPEN (55,FILE='IN.DAT',STATUS='OLD')
	READ(55,30) ((X(N,M),M=1,6),N=1,5)
     30 FORMAT(6F10.4)
	CLOSE(55) \
       		   \           Repetition number
                    \_________ determines the number
			       of entries per line
NOTE: Six X values on each line in the input file


Writing Arrays with 2 EXPLICIT DO LOOPS

	DIMENSION X(5,6)
	OPEN (66,FILE='OUT.DAT',STATUS='NEW')
	DO 45 N=1,5
	   DO 40 M=1,6
	      WRITE(66,50) X(N,M)
     40    CONTINUE
     45 CONTINUE
     50 FORMAT(F10.4)
        CLOSE(66)
NOTE: Produces a column of output


Writing Arrays with 1 IMPLICIT DO LOOP
and 1 EXPLICIT DO LOOP

	DIMENSION X(5,6)
	OPEN (66,FILE='OUT.DAT',STATUS='NEW')
	DO 10 N=1,5
	   WRITE(66,60) (X(N,M),M=1,6)
     60    FORMAT(6F10.4)
     10 CONTINUE  \ 
 	CLOSE(66)  \           What happens if the
                    \__________repetition number is 
			       left off?
NOTE: Six X values on each line in the output file


Writing Arrays with 2 IMPLICIT DO LOOPS

	DIMENSION X(5,6)
	OPEN (66,FILE='OUT.DAT',STATUS='NEW')
	WRITE(66,60) ((X(N,M),M=1,6),N=1,5)
     60 FORMAT(6F10.4)
	CLOSE(66) \
                   \
                    \             What happens if the 
                     \____________repetition number is
  				  left off?
NOTE: Six X values on each line in the output file


Example Part 1

Find the average for each row and
the overall average for the entire matrix


Example Part 2


Example Part 3

You can also read the data with 1 implicit
and 1 explicit DO LOOP:

	REAL X(10,10),ROWAVG(10)
	OPEN(5,FILE='IN.DAT')
	READ(5,*)N,M
	DO 10 I=1,N
	    READ(5,20)(X(I,J),J=1,M)
     10 CONTINUE
     20 FORMAT(10F8.3)
	CLOSE(5)

The rest of the program is the same as Example Part 2.