Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 8 ARRAYS  An array is a group of storage locations (elements) that have the same name.  The element of an array is distinguished by using the.

Similar presentations


Presentation on theme: "CHAPTER 8 ARRAYS  An array is a group of storage locations (elements) that have the same name.  The element of an array is distinguished by using the."— Presentation transcript:

1 CHAPTER 8 ARRAYS  An array is a group of storage locations (elements) that have the same name.  The element of an array is distinguished by using the common name followed by a subscript or an index in parentheses.  Subscript is represented by consecutive integer, usually begins with the number 1.

2 EXAMPLES !Specification statement Real, Dimension (50) :: A Real:: A(50) !Assignment statement A (4) = 177.8 !Output statement Print *, A (10) !Read statement Read (10, *) A (10) Read (10, *) A !DO loop DO I=1, 50 Read (10, *) A (I) End DO

3 Array Declaration 1.Compile Time Array Type, DIMENSION (size):: list of array names type, DIMENSION (1:u) :: list of array names type :: list of array specifiers Examples: Integer, Dimension(40)::A, B Real, Dimension(1:c):x Character:: Names(5) two problems: If size exceeds stored values  wasted memory If size is smaller than the stored values  overflow.

4 2. Run Time Arrays (Allocatable array) memory allocated during execution NOT compilation type, DIMENSION (:) ALLOCATABLE :: list of arrays REAL,DIMENSION ( : ), ALLOCATABLE : : A, B Allocation statement: ALLOCATE ( list, STAT=status-variable) Example: REAL, DIMENSION ( : ), ALLOCATABLE :: A, B PRINT *, "Enter size of arrays A and B:" READ *, N ALLOCATE (A(N), B(0:N+1), STAT= AllocateStatus) IF (AllocateStatus/=0) STOP ! “***Not enough memory***"

5 Storing Data In 1-D Array Examples: J(1)=0 J(5)=NUM*COUNT DIST(2)=46.2+Sin(x) J(2*I)=3 R(J)=R(J-1) B=TR(2*I)+TR(2*I+1)

6 Input And Output 1.Array as a list enclosed between (/ and /). Example: INTEGER, DIMENSION (8) :: A A= (/11, 22, 33, 44, 55, 66, 77, 88/) 2.read statement  to store data into an array. Example: If the array A contains 8 elements, then: REAL, DIMENSION (8) :: A READ *, A for only the first three elements REAL, DIMENSION (8) : : A READ *, A(1), A(2), A(3)

7 3.Array values read using an implied DO Loop. Example: REAL, DIMENSION (20) : : R READ *, (R(I), I=1, 10) Example: REAL, DIMENSION (20) : : R DO I=1, 10 READ *, R(I) END DO

8

9

10

11 Two-Dimensional Array (2-D Array) two dimensional array with 4 rows and 4 columns A= A(2,3) = A(1,3) = A(4,3) = -5 -33 0

12 Array Declaration 1. Fixed Dimension 2-D Arrays: type, DIMENSION (Rn, Cn) :: array name Rn= Raws number, Cn = Columns number Examples: REAL, DIMENSION (2,3) : : Course 2.Allocatable 2-D arrays type, Dimension (:, :), Allocatable : : list Allocate (A (1:3, 1:4), status = Allocate Status)

13 INTEGER, DIMENSION(10) :: aa INTEGER, DIMENSION(2,5) :: bb aa=(/1,2,3,4,5,6,7,8,9,10/) bb=RESHAPE(aa,(/2,5/)) bb=RESHAPE(aa,(/2,5/),ORDER=(/2,1/))

14 Examples 1. Write a program that reads 1D array, A, with N number of element then does the following operations: I) B=A**2 II) C=sqrt(B) III)D=A+ B IV) E=Sum(A)

15 Example: Write a FORTRAN program that does the following: a.Reads from a data file 'Exam.dat', number of students (N), the names of students and their IDs into a character 2-D array number of students (N), and their test scores for four tests into a real 2-D array Test in row order. (Each input data line will have the name of the student, his ID number, and his four test scores). b.Calculates the average score for the scores for each student and store the result in a real 1-D array Ave. c.Find the maximum average for the students. d.Writes the results into output data file called Output.dat where each line should contains the name of the student, his ID number, and his four test scores, and the average value. Also, let the program write the name and the ID for the student who got the maximum average, and his average value.

16 Solution: PROGRAM Students_Exam CHARACTER (20), dimension (50, 2) : : NAME REAL : : Test (50, 4), Ave (50) OPEN (UNIT =5, FILE = 'Exam.dat', STATUS = 'OLD') OPEN (UNIT =6, FILE='Output.dat', STATUS='New') READ (5, *) Num_Students DO I=1, Num_students READ (5, *) (Name (I, J), J=1,2), (Test (I, K), K=1,4) END DO DO I=1, Num_students Sum=0 DO J=1,4 Sum = Sum +Test (I, J) END DO Ave (I) = Sum / 4.0 END DO AveMax=Ave(1) M=1 DO I=2, Num_Students IF (AveMax < Ave(I) ) THEN AveMax=Ave(I) M=I END IF END DO DO I=1, Num_students WRITE (6,2) (Name (I,J), J=1,2), (Test (I, K), K=1, 4), Ave (I) 2 FORMAT (1X, 2A, 2X, 5F6.2) END DO WRITE (6,5) (NAME (M,I), I=1,2), Ave (M) 5 FORMAT (1X, 2A, F6.2) CLOSE (UNIT=5) CLOSE (UNIT=6) STOP END PROGRAM Students_Exam

17


Download ppt "CHAPTER 8 ARRAYS  An array is a group of storage locations (elements) that have the same name.  The element of an array is distinguished by using the."

Similar presentations


Ads by Google