Next: Array Details
Up: Arrays
Previous: Why Use Arrays?

An Array Example

Take a look at ``array1.f'' in your ``examples'' directory (or view it directly). This program prompts the user for and reads ten numbers, and then prints them all back out. What is clumsy about this program?

Click here for the answer

The program cries out to be simplified by using a DO loop to repeat the statements

PRINT *, 'Please enter an integer'
READ *, N1

ten times over, and by using another DO loop to repeat the statement

PRINT *, N1

ten times over. Unfortunately, this is not possible (at least without arrays!). Why not?

Click here for the answer

By using an array of ten elements to store the input, instead of ten distinct variables, we can vastly shorten the program. Take a look at ``array2.f'' (or view it directly).

This program is very similar to the first version. What are the differences?

Click here for the answer

Now it is possible to shorten this program by using a DO loop. Take a look at ``array3.f'' (or view it directly). What have we done to take advantage of arrays?

Click here for the answer

Does this convince you of the utility of arrays? If not, consider what changes would be required for ``array1.f'' if you wanted to read 100 numbers. Compare that to the changes required for ``array3.f'' to accomplish the same thing.


Next: Array Details
Up: Arrays
Previous: Why Use Arrays?

Hamlet Project
hamlet@cs.utah.edu