2. Introduction
So far we have covered the predefined
Fortran data types; Integer, Real, Character,
and Logical.
These data types are called simple types
because a data value of one of these types
consists of a single value.
It might be necessary for some application to
process a collection of values, e.g. a list of
scores.
To do that we use Arrays.
3. Ex:
Suppose we want to calculate the
average of the grades of 40 students in
a class, then display all grades greater
than the average. The grades are
stored in a file.
6.
If (grade1>avg) print*, grade1
If (grade2>avg) print*, grade2
.
.
If (grade40>avg) print*, grade40
End Program Avg
Is this a practical approach??
What if we have 15432 students?
7.
Another way is to rewind the file
Program Avg2
implicit none
integer:: openstatus, Inputstatus
integer:: No_of_grades, I
real:: grade, sum, avg
.
.
Open(unit=10,file=“grade.avg”,status=“old” , &
IOSTAT= openstatus)
if (openstatus>0) stop “ **error**”
8.
No_of_grades = 0
Do
Read(10,*,IOSTATUS= inputstatus) grade
if (inputstatus>0) stop “**Input Error**”
if (inputstatus<0) stop “not enough data”
No_of_grades = No_of_grades + 1
End do
9.
Rewind (unit=10)
sum = 0.0
Do I = 1, No_of_grades
Read(10,*) grade
sum= sum + grade
End do
Avg = sum/ No_of_grades
10.
Rewind (unit=10)
Do I = 1, No_of_grades
Read(10,*) grade
if (grade>avg) print*, grade
End do
End program Avg2
this approach is slow because files are stored
in the hard drive which is slower than the
memory.
11. Arrays
We need a data structure to store and
organize the entire collection of grades.
This structure should be stored in the main
memory to reduce retrieval time.
Instructions to access this structure should be
simple.
Direct access to data it should take the
same time to access grade40 as it takes to
access grade1
12. Arrays Declaration
Real, Dimension(40):: grades
instructs the compiler to establish an
array with the name grades consisting
of 40 memory locations in which values
of type real can be stored.
13.
grades(1) refers to the first element of the
array.
Grades(40) refers to the 40th element of the
array.
Each subscripted variable grades(1),
grades(2), …, grades(40) maps an individual
memory location and can be used the same
way as a simple variable.
15.
Subscripts attached to an array may be an
integer variable or expression
Read*, grades(n)
where n holds an integer value.
To read the 40 grades,
do I =1, 40
read(10,*) grades(I)
end do
16. Note
Each value in the input file must be on a
separate line, because each execution of the
read inquires a new line.
Equivalent to :
Read(10,*) grades(1)
Read(10,*) grades(2)
.
.
Read(10,*) grades(40)
17.
An alternative method of reading or
displaying an array is to use input or output
statements containing the array name.
Read(10,*) grades
Read(10,*) grades(1),grades(2),…, grades(40)
here all values need not be read from
separate lines.
18. Implied Do Loops
Using an implied do loop provides
another way for Input/Output.
Form
(list of vars, control_var = init_value,limit,step)
It has exactly the same effect as the do
loop.
19. Ex:
Read(10,*) ( grades(I), I = 1, 40)
Read(10,*) grades(1), grades(2), …
or
print (10,*) (grades(I), I = 1,40)
print (10,*) grades(1), grades(2), …