Date: Mon, 11 Nov 1996 17:57:58 GMT
Server: NCSA/1.5
Content-type: text/html
Last-modified: Sat, 04 May 1996 04:22:28 GMT
Content-length: 7609
CS 110 Section 2 Lecture Notes - Week 7
Lecture Notes - Week 7
- Topic:
- One-dimensional arrays. Passing arrays as arguments.
- Text:
- 7.1 - 7.4, 7.6, 7.9
- Notes:
-
Arrays
Simple Variables
- So far we have used simple variables to store values.
INTEGER SCORE
REAL PRICE
LOGICAL ANSWER
- Simple variables can only store a single value; i.e one integer, one real number or one logical value.
- To store another value we must declare a new variable with a different name.
INTEGER SCORE1, SCORE2, SCORE3
Array Variables
- Arrays store several related values under a single variable name.
- All values stored in an array are of the same type, called the base type.
- Easier to store several values in an array than declaring separate variables for each.
Declaring Array Variables
- Specify the name of the array, the base type, and the number of values to store - called the size of the array.
basetype arrayname(arraysize)
INTEGER SCORES(3)
REAL PRICES(1000)
LOGICAL ANSWRS(10)
- Array variables are declared together with simple variables.
PROGRAM FOO
REAL COST
INTEGER SCORES(3), SUM
- Commonly use a constant (i.e. Fortran PARAMETER statement) to declare the size of an array.
INTEGER MAXSIZ
PARAMETER (MAXSIZ=3)
REAL SCORES(MAXSIZ)
- SCORES array variable stores three INTEGER's under a single variable name.
Referencing Arrays
- To access a particular value in an array you specify the array name and the subscript of the value (i.e. its position in the array).
arrayname(subscript)
- The first value in an array has subscript 1, the last has subscript arraysize.
SCORES(1) - first element
SCORES(2) - second element
SCORES(3) - last element
- Array variables can be used anywhere you would use a normal simple variable.
PRINT *, 'Your score is ', SCORES(1)
BAR = SCORES(1)*10 + COS(SCORES(2))
READ(1, 100, END=150) SCORES(3)
- Store values in an array using the assignment statement, just like simple variables
arrayname(subscript) = value
SCORES(1) = 3
SCORES(2) = SCORES(1)*6
Array Subscripts
- Array subscripts can be literals, variables or expressions. The value of the expression determines which element is accessed.
INTEGER N, SCORES(3)
N = 2
SCORES(N) = SCORES(N*4-5)-SCORES(1)
- Value of array subscript must be in the range 1 ... arraysize.
- Array subscript is an INTEGER expression enclosed by parentheses ().
Right:
SCORES(N), SCORES(N*4-5)
Wrong:
SCORES(-13), SCORES(5), SCORES[2], SCORES(2.3)
- Important: The type of the array variable, the base type, and the subscript are very different.
REAL PRICES(1000)
PRICES(N) = ...
Variable Type
--------------------------------------------------
PRICES ARRAY (all of the prices together)
PRICES(N) REAL
N INTEGER
Arrays and DO Loops
- Arrays are commonly processed using a DO loop.
INTEGER MAXSIZ, TOTAL, COUNT
PARAMETER (MAXSIZ=100)
REAL SCORES(MAXSIZ)
C Read in all the scores
DO COUNT=1, MAXSIZ
READ *, SCORES(COUNT)
END DO
C Add up all the scores
TOTAL = 0
DO COUNT=1, MAXSIZ
TOTAL = TOTAL + SCORES(COUNT)
END DO
C Print out all the scores
DO COUNT=1, MAXSIZ
PRINT *, SCORES(COUNT)
END DO
STOP
END
Try doing this without arrays!
- The DO loop counter COUNT is used as the array subscript to sequentially access the values one at a time.
Reading and Writing Arrays
DO Loop
- An array can be "filled up" by reading in the values in a DO loop.
DO I=1, 100
READ *, SCORES(I)
END DO
- Values must be typed in on different lines because the READ statement is re-executed each time.
- The values in an array can be printed on different lines using a DO loop.
DO I=1, 100
PRINT *, SCORES(I)
END DO
Implied DO Loop
- Can read in any part of an array from the same line using an implied DO loop.
READ *, (SCORES(I), I=50, 100)
- Equivalent to a DO loop inside the READ statement, so everything is read off the same line.
- Can print in any part of an array on the same line using an implied DO loop.
PRINT *, (SCORES(I), I=50, 100)
Array Arguments
- Arrays can be passed into and out of user-defined functions and subroutines as array arguments.
- As with passing simple variables as arguments, array variables must first be declared in the main program.
- Important: Pass both the array name and the array size as arguments. Must re-declare array inside the function or subroutine.
PROGRAM TEST
INTEGER MAXSIZ
PARAMETER (MAXSIZ=10)
INTEGER SCORES(MAXSIZ)
...
PRINT *, GETMAX(SCORES, MAXSIZ)
STOP
END
INTEGER FUNCTION GETMAX (LIST, SIZE)
C Finds the highest value in the list
C Declare arguments
INTEGER SIZE, LIST(SIZE)
...
RETURN
END
Input Array Arguments
- As with simple variable arguments, input array arguments should not be re-assigned.
Right:
MAX = LIST(1)
IF (MAX .GT. LIST(N)) ...
Wrong:
LIST(N) = ...
- Whole arrays can be passed in as input arguments
PRINT *, GETMAX(SCORES, MAXSIZ)
or individual values can be passed as input arguments, just like simple variables.
PRINT *, FACT(SCORES(3))