Download presentation
Presentation is loading. Please wait.
1
CHAPTER 8 ARRAYS FORTRAN 90 provides arrays for storing, structuring and processing a collection of data values that are related in some way such as a list of test scores.
2
One Dimensional Array Declartion Real, Dimension (50) :: Failure_Time OR Real :: Failure_Time (50) OR DIMENSION :: Failure_Time (50) REAL:: Failure_Time
3
Method or Reading Array Read (10, *) Failure_Time (10) DO I=1, 50 Read (10, *) Failure_Time (I) End DO Data 1.5 3.9 4.0
4
Method or Reading Array Read (10, *) Failure_Time (1) Read (10, *) Failure_Time (2) - - - Read (10, *) Failure_Time (50)
5
Failure_Time (4) = 177.8 !Output statement Print *, Failure_Time (10) !Read statement Read (10, *) Failure_Time (10)
6
Input or output containing the array name Read (10, *) Failure_Time !Is equivalent to Read (10, *) Failure_Time (1), Failure_Time (2),..., Failure_Time (50) Data 1.5 3.9 4.0 print (10, *) Failure_Time
7
Array Declaration General form: Type, DIMENSION (size):: list of array names where array sizes must be specified with integer constants, not variable. Examples: INTEGER, DIMENSION (5) : : J, Dist REAL, DIMENSION (20) : : Calc, Dom1, Kito CHARACTER (20), DIMENSION (100) : : names, IDs
8
Allocatable Arrays (or run time arrays) type, DIMENSION (:) ALLOCATABLE :: list of array names Example: REAL, DIMENSION ( : ), ALLOCATABLE : : A, B Example: REAL, DIMENSION ( : ), ALLOCATABLE :: A, B READ *, N ALLOCATE (A(N), B(0:N+1), STAT= AllocateStatus) IF (AllocateStatus/=0) STOP "****Not enough memory***"
9
Storing Data In 1-D Array Examples: J(1)=O J(5)=NUM*COUNT DIST(2)=46.2+Sin(x) Subscripts can also be integer expression: Examples: J(2*I)=3 R(J)=R(J-1) B=TR(2*I)+TR(2*I+1)
10
Initializing Of 1-D Array Example: INTEGER, DIMENSION ( 5 ) : : J DO I=1,5 J(I)=10 END DO Input And Output 1.An array constant may be constructed as a list of values enclosed between (/ and /). Example: INTEGER, DIMENSION (8) :: A A= (/11, 22, 33, 44, 55, 66, 77, 88/)
11
REAL, DIMENSION (8) :: A READ *, A will read values for all 8 elements. And if we have REAL, DIMENSION (8) : : A READ *, A(1), A(2), A(3) Array values may also be read using an implied DO Loop. Example: If we wish to read the first 10 elements of the array R, we can use the following implied DO-Loop in the read statement. REAL, DIMENSION (20) : : R READ *, (R(I), I=1, 10)
12
اكتب برنامجا يقوم بقراءة عناصر مصفوفة احادية حقيقية A بها عدد عناصر اقل من 100 عنصر ، ثم يقوم البرنامج بعمل التالي 1- حساب المتوسط الحسابي لعناصر المصفوفة وطباعته 2- حساب القيمة العظمي في المصفوفة وطباعتها Real :: A(100),Mean,Max Read*,N READ(5,*) (A(I),I=1,N) SUM=0.0 DO J= 1,N SUM=SUM+A(J) END DO MEAN=SUM/N PRINT *, “MEAN=“, MEAN
13
MEAM = A(1) DO I= 1,N IF ( A(I). GT. MAX) MAX= A(I) END DO MEAM = A(1) DO I= 1,N IF ( A(I). LT. MIN) MIN= A(I) END DO
14
اكتب برنامجا يقوم بقراءة عناصر مصفوفة احادية حقيقية A بها عدد عناصر اقل من 100 عنصر ، ثم يقوم البرنامج بعمل التالي ترتيب عناصر المصفوفة ترتيبا تصاعديا ، وطباعة المصفوفة الجديدة Real :: A(100),TEMP Read*,N DO I= 1,N READ(5,*) A(I) DO I = 1,N-1 DO J= I+1,N IF (A(I).GT. A(J) )THEN TEMP= A(I) A(I) = A(J) A(J) = TEMP END IF END DO PRINT *, A END
15
اكتب برنامجا يقوم بقراءة عناصر مصفوفتين احاديتين بهما قياسات درجات الحرارة في مدينة الرياض طوال الليل والنهار لكل ربع ساعة كقيمة صحيحة وخزنة في ملف علي الوحدة الخامسة، ثم يقوم البرنامج بعمل التالي بحساب وطباعة المتوسط الحسابي لدرجات الحرارة في مدينة الرياض. INTEGER :: A(100), B(100), Mean,N1,N2 Read*,N1,N2 READ(5,*) (A(I),I=1,N1) SUM1=0.0 DO J= 1,N1 SUM1=SUM1+A(J) END DO SUM2=0.0 DO J= 1,N1 SUM2=SUM2+A(J) END DO MEAN=(SUM1+SUM2)/(N1+N2) PRINT *, “MEAN=“, MEAN END
16
Integer, parameter : : lower limit_1 =1, upper limit_1=3, lower limit_2 = 2, upper limit_2 =5 Integer, Dimension (lower limit_1 : Upper limit_1) : : Gamma Real, Dimension (lower limit_2, upper limit_2) : : Delta
17
Dimensional Array -Two Example Array Declaration Examples: REAL, DIMENSION (2,3) : : Course type, DIMENSION (number of rows, number of columns) :: array name(s) REAL, DIMENSION (-1:1, 1:3) : : Course
18
طرق قراءة المصفوفة READ(( A(I,J),J= 1,3),I= 1,3) DATA 0.0 1.1 2.2 -10.0 7.7 3.3 3.5 5.3 0.0 READ(( A(I,J),I= 1,3),J= 1,3) DATA 0.0 - 10.0 3.5 1.1 7.7 5.3 2.2 3.3 0.0 DO I = 1,3 READ ( A(I,J), J= 1,3) END DO DATA
19
then A(0,2)=7.7 A(-1, 3)=2.2 Initialization REAL, DIMENSION (5,4):: AREA - - - DO I=1,5 AREA (I,1)=1 AREA (I,2)=1 AREA (I,3)=2 AREA (I,4)=2 END DO
20
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.
21
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
22
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) 2FORMAT (1X, 2A, 2X, 5F6.2) END DO WRITE (6,5) (NAME (M,I), I=1,2), Ave (M) 5FORMAT (1X, 2A, F6.2) CLOSE (UNIT=5) CLOSE (UNIT=6) STOP END PROGRAM Students_Exam
23
INTEGER :: A(5,5) DO I= 1,5 DO J= 1,5 IF (I. EQ. J) THEN A(I,J) = 1 ELSE A(I,J)=0 END IF END DO
24
EXAMPLES INTEGER, DIMENSION (3,3) :: A,B INTEGER :: NU (6), NM (9) CHARACTER(*), DIMENSION (2,2): : NAMES 0PEN (UNIT= 3, FILE = DATA,STATUS= “OLD”) 1- READ (3, *) NU,NM,A 2- DO I= 1,3 READ (3, *)( A(I,J), J=1,3), *)( B(I,J), J=1,3)
25
987654321 181716151413121110 272625242322212019 363534333231302928 454443424140393837 SALEHSMIRSAUDSAAD
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.