Chapter 24. Arrays

Table of Contents

24.. Motivation
24.. Design vs. Implementation
24.. Array Basics
24.. Dynamic Memory Allocation
24.. Array Constants
24.. Static Array Initialization
The Fortran DATA Statement
Look-up Tables
24.. Arrays as Arguments
24.. Allocate and Subprograms
24.. Sorting
24.. When to Use Arrays
24.. Array Pitfalls
Address Calculation
Out-of-bounds Errors
Pinpointing Crashes with a Debugger
24.. Fortran 90 Vector Operations
Implicit Loops
24.. Code Quality
24.. The Memory Hierarchy
Virtual Memory
Memory Access Time
24.. Performance
Code Examples
24.. Self-test


Write a program that reads in a list of 1000 numbers and prints them in the order they were entered. This is easy:

int     c;
double  number;

for (c = 0; c < 1000; ++c)
    scanf("%lf", &number);
    printf("%f\n", number);
integer :: i
double precision :: number

do i = 1, 1000
    read *, number
    print *, number

Now, write a program that reads a list of 1000 numbers from the standard input and prints them in reverse order.

In order to achieve this, the entire list must be stored in a way that it can be traversed backwards.

There are basically two places to store a list of values:

  • In a file
  • In RAM

Whether it's better to store the list in a file or in RAM depends on how large the list is and how fast we need to be able to read the list backwards. Disks are much larger than RAM, so they will accommodate larger lists. RAM is many times faster than disk, however, and will therefore lead to a faster program.