Title: ICS 101: Computer Programming in FORTRAN
1ICS 101 Computer Programming in FORTRAN
- Lecture 8 One-Dimensional Arrays
2Objectives
- Definition declaration
- Initialization
- Array processing
- Arrays subprograms
3Definition
- In many programming problems, several operations
need to be applied on a collection of data. For
examples, scores of 50 students. - Having a variable for each single data is not
efficient. - How can we
- Read the scores.
- Find the average scores.
- Display the scores higher than the average.
- Doing these operations on the variables is
cumbersome for the programmer.
REAL S1, S2, ..., S50
4Definition
- FORTRAN provides a data structure (called array)
that can hold these data together under one
variable. - An array is a collection of related data of the
same type. - An array consists of elements (50 in this
example) that can be processed as a normal
variable. - Whenever an array is declared, the computer will
allocate consecutive memory locations of the
array size.
SCORES
5Declaration
- There are two ways to declare an array in
FORTRAN. - Where
- TYPE the data type of the elements. INTEGER,
REAL, LOGICALetc. - name is the name of the array (rules of naming
variables apply here) - no of elements is the size of the array. It can
be constant or expression. This value should not
be REAL. - Example
- To declare more than one array
TYPE name (no of elements)
INTEGER SCORES(50)
INTEGER LIST1(10), LIST2(20), LIST3(30)
6Declaration
- The other way is as follows
- Example
- If TYPE is omitted, then the array type follow
the implicit declaration. - What is the type of ALIST?
DIMENSION name (no of elements) TYPE name
Optional
DIMENSION SCORES(50) INTEGER SCORES
DIMENSION ALIST(100), KLIST(200),
XLIST(300) INTEGER XLIST REAL KLIST
7Declaration
- Array elements are indexed from 1 up to the size
of the array. - Example
- FORTRAN allows elements to be indexed from any
value. for that, the declaration should indicate
from where the index starts and ends. - Where m is the index of the first element and n
indicates the index of the last element. - The programmer must make sure that m lt n.
SCORES(1) the 1st element SCORES(2) the 2nd
element ... SCORES(50) the 50th element
TYPE name (m n)
DIMENSION name (m n) TYPE name
8Declaration
- The number of elements of arrays declared as
before is - Examples
n m 1
REAL YEAR (19831994)
DIMENSION TEMP(-2020)
TEMP(-20) the 1st element TEMP(-19) the 2nd
element ... TEMP(20) the 41st element
YEAR(1983) the 1st element YEAR(1984) the 2nd
element ... YEAR(1994) the 12th element
9Array Initialization
- Initialization of an array is to assign values to
the array elements. - A Do loop and READ statement can be used for
initialization. The choice of the method depends
on the situation. - To initialize all the elements to a certain
value, a Do loop is used.
REAL LIST(3) DO 5 K 1, 3 LIST(K)
0.0 5 CONTINUE
INTEGER POWER2(010) DO 7 K 0, 10
POWER2(K) 2 K 7 CONTINUE
10Array Initialization
- If the values of the array elements should be
read from the user, then READ statement is used
in two different ways. - First method (array name)
- Second method (implied loop)
- Example
- For arrays declared as a range (m, n), the
implied loop should start from m to n. - Example
READ, name
READ, (name (K), K 1, array_size)
READ, (SCORES(K), K 1, 50)
READ, (TEMP(K), K -20, 20)
11Array Initialization
- In the previous two methods, only one READ
statement is executed. This means that values are
read from the beginning of the input line. If the
line is not enough for the array elements,
reading will continue the next line. - All these inputs will initialize the array in the
same way - The difference between the two methods is that
implied loop can be used to initialize part of
the array (e.g. first 10 elements).
INTEGER LIST(4) READ, LIST
10 20 30 40
10 20 30 40
10 20 30 40
12Array Initialization
- Using a Do loop will have a different effect.
- This is because we have 4 READ statements and
each one starts reading from a new line.
DO 7 I 1, 4 READ, LIST(I) 7 CONTINUE
10 20 30 40
10 20 30 40 50 60 70 80
10 20 30 40 50 60
13Array Initialization
Array Initialization
To a known value Use a Do loop
Values from the user
Implied loop
Do loop
READ, name
All elements initialized
All or part of elements initialized.
All or part of elements initialized. Input must
be in each line
14Array Processing
- Common processing on arrays include displaying
the elements, searching for specific elements and
doing operations on array elements. - To display array elements, there are two methods.
- First method (array name)
- Second method (implied loop)
- If the array declared as a range, the implied
loop should start from m and ends at n. - Example
PRINT, name
PRINT, (name (i), i 1, array_size)
PRINT, (TEMP(i), i -20, 20)
15Array Processing
- A Do loop can also be used, but each element will
appear in a single line. - The output will be
DO 12 I 1, 4 PRINT, LIST(I) 12
CONTINUE
10 50 70 80
16Array Processing
- Whenever dealing with arrays, follow these rules
- Elements indices (subscripts) should not go
beyond array boundaries. - Indices must always appear as integer
expressions. - The value assigned to an array element must match
in type with array type. - Array must be declared before its elements are
initialized. - When declaring an array, its size must be integer
constant except in subprograms. - The first three errors may not be detected by the
compiler. It is the programmer responsibility to
make sure that these rules are satisfied.
17Array Processing
INTEGER GRADE(25), LIST(3) LOGICAL
FLAG(20) CHARACTER NAMES(5) 3
Statements Comments
GRADE(26) 0.0 26 is out of range
LIST(2.0) X 3 2.0 is not an integer
NAMES(4) 100 NAMES(4) is a character
FLAG(3) Ali FLAG(3) is logical
READ, (GRADE(K), K 1, 100) GRADE has only 25 elements
NEW(2) 3 NEW is not declared as an array
18Array Processing
- The following program reads a list of numbers and
then search for the maximum integer in this list. - Assume the input is
INTEGER LIST(7), MAX READ, LIST MAX
LIST(1) DO 1 K 2, 7 IF(LIST(K) .GT.
MAX) THEN MAX LIST(K) ENDIF 1
CONTINUE PRINT, MAXIMUM IS , MAX END
33
2
33
3
88
4
97
5
97
6
97
7
33 20 2 88 97 5 71
19Array Subprograms
- Arrays can also be used in subprograms and rules
discussed before apply here. - One exception occur where the array is passed as
an argument to the subprogram. In this case, you
have to pass the array name and its size as
arguments to the subprogram. - Example
INTEGER LIST(10), SUM, K READ, (LIST(K), K
1, 4) PRINT, SUM(LIST, 4) END INTEGER
FUNCTION SUM(MARK, N) INTEGER N, MARK(N)
SUM 0 DO 13 J 1, N 13 SUM SUM
MARK(J) RETURN END
20Array Subprograms
- Subprograms can also modify an array passed from
the caller program.
SUBROUTINE ARRABS(A, N) INTEGER K, N
REAL A(N) DO 44 K 1, N 44 A(K)
ABS(A(K)) RETURN END INTEGER J, N
REAL A(100) READ, N, (A(J), J 1, N)
PRINT, THE ORIGINAL ARRAY, (A(J), J 1, N)
CALL ARRABS(A, N) PRINT, THE NEW ARRAY ,
(A(J), J 1, N) END
21Exercise
- Write a program to read an array X and check if
all the elements of the array are in increasing
order. Print a proper message. - Write a subroutine REVRSE to reverse an array
with 5 elements. Write a main program to test the
subroutine. - The sum and difference of two vectors a (a1,
a2, , an) and b (b1, b2, , bn) is defined as
follows - Write a program that reads the two vectors and
calculates their sum and difference.
5 12 23 45 65 its reverse is 65 45
23 12 5
a b (a1 b1, a2 b2, , an bn) a b
(a1 b1, a2 b2, , an bn )