Chapter 23. Arrays

Table of Contents

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

Motivation

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
enddo
        

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.