170 likes | 385 Views
FORTRAN 77 Programming. Lecture 3 : January 2001 Dr. Andrew Paul Myers. Array Variables. An array is a simple structure, capable of storing many variables of the same type in a single data structure. Declare arrays with other variables. Arrays can be of any data type. Declaration :
E N D
FORTRAN 77 Programming. Lecture 3 : January 2001 Dr. Andrew Paul Myers
Array Variables. • An array is a simple structure, capable of storing many variables of the same type in a single data structure. • Declare arrays with other variables. • Arrays can be of any data type. • Declaration : REAL array(100) INTEGER loop(20),data(500) CHARACTER names(10)*20
More on Arrays. • Each array member is addressable by an array subscript. • e.g for REAL x(10), subscripts range from 1 to 10. i.e. array(1), … array(10) • The subscript may be a constant, variable or expression : array(1)=SIN(x) array(loop)=4.0*array(loop-1)
And There’s More… Consider : • REAL array(10) • As mentioned before, array subscripts range from 1 to 10. • We can over ride this default: REAL array(-10:10) INTEGER data(0:200)
Still more… • Arrays can be multi-dimensional REAL array(5,2) i.e. array(1,1), array(1,2), array(2,1)… • Taking things to an extreme!!! REAL array(5,0:12,20,-10:100)
Iterations. • Loops allow blocks of command to be repeated. • Simplest is the DO loop. DO <index> = <start>,<stop> [,<step>] <statement(s)> END DO
Example DO Loop. INTEGER loop REAL array(100) DO loop=1,100,1 array(loop)=0.0 END DO
DO WHILE Loop. • Another type of loop : DO WHILE <condition> <statement(s)> END DO Not in all versions of FORTRAN 77.
Example. INTEGER test test=15 DO WHILE ( test .GT. 12 ) test=test-1 END DO Useful for repeating a program?
The Horrible Past… • DO loop with numeric label. DO 1000 loop=0,10,2 <statement(s)> 1000 CONTINUE • Implied DO loop! READ*,(value(loop),loop=1,10,1)
The DATA statement. • Another method of assigning values to variables and arrays. REAL a,b,c INTEGER d(5),lots(100) CHARACTER single(5)*1 DATA a /1.5/ DATA b,c /2.5,5.6/ DATA d /1,2,3,4,5/ DATA lots /100*0.0/ DATA single /5*’Y’/
Advanced Data Types. • Structures. Not in all versions of F77. • Structures can hold many variables of different types. • Structures are declared with all other variables at the beginning of a program. • With structures you can build powerful, custom data types.
Declaring A Structure. STRUCTURE / book_type / CHARACTER title*80 CHARACTER author*40 INTEGER stock_level REAL price END STRUCTURE
The Next Step… • Now we must declare a record of the type defined in the structure. RECORD / book_type / science RECORD / book_type / rubbish(10)
Value Assignments. • Each part of the record is identified thus : science.title = ‘2001’ science.author = ‘A.C.Clarke’ science.stock = 25 science.price = 9.99
For the More Brave… • Remember? RECORD / book_type / rubbish(10) DO loop=1,10 READ’(A80)’,rubbish.title(loop) READ*,rubbish.price(loop) END DO