One Dimensional Arrays

An array is a set of related variables that have the same name and are of the same type (REAL, INTEGER, CHARACTER). For example, a group of 10 students are given marks for a particular essay. The marks could be assigned to 10 individual variables each of a different name, MARKl, MARK2 ... MARK10.

Or this set of values could be given a group or array name of MARK and each individual value, called an array element, can be referred to by a subscript number from 1 to 10. The first student's mark is referred to as MARK(l), the second as MARK(2) and so on. A note on pronunciation: MARK(l) would be said as mark sub

Because accessing the values in the various elements of an array is done by just referring to the subscript, working with groups of values becomes relatively easy. For example, finding the total of the marks given could be found using a DO loop index to refer to successive addresses as follows:

      SUM = 0
      DO 25 I = 1,10
      SUM = SUM + MARK(I)
  25  CONTINUE

To do a similar calculation without an array, you would have to write one huge assignment statement:

SUM = MARKl+MARK2+MARK3+...+MARK9+MARK10

Imagine how long that assignment statement be if you wanted to sum 100 or even 1000 numbers!

Array Declarations

Arrays must be declared at the beginning of the program. The easiest way to do this is to state what type of values the array will contain (REAL, INTEGER, CHARACTER) followed by the name of the array followed by the number of elements in the array enclosed in brackets.

INTEGER NAME1(n)
REAL NAME2(n)
where:
NAMEl and NAME2 are array names and n iS the number of array elements

Example:

     INTEGER MARK(10)
     REAL TABLE(25), TIME(50)

In the above example, MARK will hold 10 integer values with addresses (subscripts) beginning at 1. TABLE will hold 25 real values with addresses beginning at l and TIME will hold 50 real values with addresses beginning at 1. It is important to differentiate between an address of an array element and the contents of an array element. The contents of an array element can change, the address cannot.

Sometimes you may want the addresses of an array to begin at a different number than 1. In these instances, you can use an extended form of the declaration, stating the minimum and maximum values of the subscript.

INTEGER NAME1(L:U)
REAL NAME2(L:U)
where:
NAME1 and NAME2 are array names of 6 alphanumeric characters. L iS the lower subscript value. u is the upper subscript value.

Example

   INTEGER ERROR(-10:10)
   REAL SECNDS(0:50)

Initial Values for Arrays

You can use an assignment statement to fill up arrays:

       MARK(1) = 23.5
       MARK(2) = 45
       MARK(10) = 85 

Values for array elements can also be read from the keyboard within a loop:

        DO 10 I = l,N
        READ *, AGE(I)
10      CONTINUE

However, for arrays of any significant size, reading from a file is more efficient:

        COUNT = 15
        DO 20 J = 1, COUNT
        READ (1,*) NAME(J), NUMBER(J)
20      CONTINUE

The program segment above will repeat the following sequence 15 times: read two values from the file opened with unit number 1, put the first value into the Jth address for NAME and the second value into the Jth address for NUMBER.

This works fine if you have two columns in the data file, the first column goes into the array NAME and the second column goes into the array NUMBER. What if you know the first 15 values in the file will go into one array? A statement referring to the array without subscripts like the one below will do the trick.

    REAL NUMBER(15)
    READ (1,*) NUMBER

Printing of Arrays

All the elements of an array will be printed if the array name is used without a subscript:

Now go to the exercises and work your way through the array problems.


Work your way through the following components attempting the exercises as you come across them:

| Programs | Variables | Arithmetic Operations | Input and Output | Looping in Programs | Arrays in Programs | Checking variables | Subprograms and functions |