Date: Wed, 11 Dec 1996 22:34:08 GMT
Server: NCSA/1.5
Content-type: text/html
Last-modified: Fri, 18 Oct 1996 17:50:49 GMT
Content-length: 7663
CS 302 Section 70 Lecture Notes - Week 7
Lecture Notes - Week 7
- Topic:
- Multi-dimensional arrays. Multi-dimensional array arguments. Parallel arrays.
- Text:
- 8.1 - 8.3
- Notes:
-
Two-Dimensional Arrays
One-Dimensional Arrays
- 1-D arrays have one subscript. The subscript specifies which value in the array you want.
INTEGER SCORES(10)
...
PRINT *, SCORES(3)
- View the array as a single row of boxes.
SCORES()
1 2 3 4 5 6 7 8 9 10
+---+---+---+---+---+---+---+---+---+---+
|-15|83 |92 |132|73 | 9 |27 |-54|82 |294|
+---+---+---+---+---+---+---+---+---+---+
- Only one subscript is needed to uniquely specify any particular position in the array.
Two-Dimensional Arrays
- 2-D arrays have two subscripts.
arrayname(sizeX,sizeY)
CHARACTER TICTAC(3,3)
- The size of the array (i.e. number of values it can store) equals sizeX*sizeY
TICTAC(1,1), TICTAC(1,2), TICTAC(1,3), TICTAC(2,1), TICTAC(2,2), TICTAC(2,3), TICTAC(3,1), TICTAC(3,2), TICTAC(3,3),
3*3 = 9 different values!
- To access a particular value specify both its subscripts; i.e. the column (X coordinate) and row (Y coordinate).
arrayname(column, row)
PRINT *, TICTAC(3,2)
READ *, TICTAC(X,Y)
- Important: The order of the subscripts is very important.
TICTAC(3,2) .NE. TICTAC(2,3)
- View a 2-D array as a grid.
TICTAC(X,Y)
+---+---+---+
3 | O | X | X |
+---+---+---+
Y = 2 | O | X | O |
+---+---+---+
1 | X | O | O |
+---+---+---+
X = 1 2 3
- Two subscripts are needed to uniquely specify a position in the grid; i.e. the column and row.
- As with 1-D arrays, subscripts can be literals, variables or expressions, or any combination thereof.
TICTAC(COL, 2)
TICTAC(1,NINT(-COS(180)*2))
- 2-D arrays do not have to be square. The number of rows and columns can differ.
INTEGER GRADES(10,4)
...
PRINT *, 'Grades for student',STU
DO GRA = 1,4
PRINT *, GRADES(STU,GRA)
END DO
2-D Arrays and DO Loops
- 1-D arrays are processed with a single DO loops whose counter is used as the array subscript.
DO COUNT = 1, MAXSIZ
PRINT *, SCORES(COUNT)
END DO
- 2-D arrays are processed with two DO loops. Both counters are used as array subscripts. Important: DO loops must be nested.
DO Y = 3, 1, -1
DO X 1, 3
PRINT *, TICTAC(X,Y)
END DO
END DO
- Use an implied DO loop for the inner loop so that all the values for each row appear on the same line.
C Print rows (Y-axis)
DO 100 Y = 3, 1, -1
C Print columns of each row (X-axis)
PRINT *, (TICTAC(X,Y), X = 1, 3)
END DO
- Important: outer loop for rows Y, inner loop for columns X.
2-D Array Arguments
- To pass 1-D arrays as arguments to a user-defined function/subroutine, pass the array name and the array size.
PRINT *, GETMAX(SCORES, MAXSIZ)
...
INTEGER FUNCTION GETMAX (LIST, SIZE)
- To pass 2-D arrays as arguments, pass the array name and both the array sizes sizeX and sizeY.
SHOW(TICTAC, 3, 3)
...
SUBROUTINE SHOW(BOARD, SIZEX, SIZEY)
C Prints the board on the screen
C Declare arguments
INTEGER SIZEX, SIZEY
CHARACTER BOARD(SIZEX, SIZEY)
C Print rows (Y-axis)
DO Y = SIZEY, 1, -1
Print columns of each row (X-axis)
PRINT *, (BOARD(X,Y), X = 1, SIZEX)
END DO
RETURN
END
DATA Statement
- The DATA statement provides a convenient way to initialize an array.
DATA arrayname / value1, value2, ... /
INTEGER MAXSIZ
PARAMETER (MAXSIZ=8)
INTEGER SCORES(MAXSIZ)
DATA SCORES /83,94,75,39,97,86,91,73/
- DATA statement should appear immediately after the array declaration.
- Can initialize an array to all the same values, e.g. zero.
DATA arrayname / arraysize * value/
DATA SCORES /MAXSIZ * 0/
Column-Major Order
- Internally, 2-D arrays are stored as a list of values (e.g. like a long 1-D array) in column-major order where each "column" is stored next to each other. In this case, the "column" is considered to be the first subscript; i.e. TICTAC(column, row)
CHARACTER TICTAC(3,3)
TICTAC(1,1), TICTAC(2,1), TICTAC(3,1), TICTAC(1,2), ...
- If PRINT, READ or WRITEthe whole array, or initialize it with a DATA statement, then array values will be listed in column-major order.
TICTAC(X,Y)
+---+---+---+
3 | O | X | X |
+---+---+---+
Y= 2 | O | X | O |
+---+---+---+
1 | X | O | O |
+---+---+---+
X = 1 2 3
For example,
PRINT *, TICTAC
prints
X O O O X O O X X
Parallel Arrays
- Often have several arrays of the same size storing related values. For example, a student's name, grades, year and GPA.
INTEGER NUMSTU
PARAMETER (NUMSTU = 50)
CHARACTER *20 NAME(NUMSTU)
INTEGER GRADES(NUMSTU,4)
CHARACTER *2 YEAR(NUMSTU)
REAL GPA(NUMSTU)
- Called parallel arrays because all the values for a particular student have the same subscript.
e.g. For student #7:
- Name = NAME(7)
- Grades = GRADES(7,X) [X=1..4]
- Year = YEAR(7)
- GPA = GPA(7)
C Display every student's data
DO S = 1, NUMSTU
PRINT *, 'Name:', NAME(S)
PRINT *, 'Grades:',(GRADES(S,X),X=1,4)
PRINT *, 'Year:', YEAR(S)
PRINT *, 'GPA:', GPA(S)
END DO
Copyright © 1996 Modified fromGareth S. Bestor (bestor@cs.wisc.edu). Last modified October 18, 1996.