lectarrays - Chapter 8 Arrays All variables we have used so...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 8 Arrays All variables we have used so far are scalar single values, meaning that the storage space is reserved to store only real or one double precision . Often time however we need to work with a collection of these variables. Many engineering and scientific calculations also involve vectors and matrices which are rectangulars arrays of numbers. Fortran provides a special data structure called an array to represent vectors and matrices. An array is essentially an ordered list of scalar elements, each of which can be referred to by its location in the array. 8.1 Vectors The program in 8.1 declares two arrays, sets their value, and compute the mean of one of them. The new feature introduced here is in line 4 of the program where two vectors are declared, x(11) and f(11) whose dimension is 11; this is done simply by appending the size in parenthesis, (11) , to the name of the array. There are various ways of declaring arrays in FORTRAN, depending on where in the code they are declared (i.e. a main program, a subroutine or in a module). However, array declarations with known dimensions at compile time are allowed anywhere in the code, i.e. the dimensions should be either constants (such as the number 11 in the example) or named constants declared in an integer, parameter statement. The latter is preferred in large codes as it simplifies changing the array dimension later by specifically naming the array dimension. The following are valid array declarations in classical FORTRAN integer, parameter :: n=200 real, dimension(n) :: x,f real :: y dimension y(n) 69 70 CHAPTER 8. ARRAYS By default the index of a FORTRAN array starts at 1 (it starts at 0 in C). The default can be overridden by specifying explicitly the bounds of an array as in integer :: xcord(-2:20) In this case the array index starts at-2 and ends at 20 so that the array has (20 + 2 + 1) = 23 elements In classical FORTRAN the elements of an array are stored in contiguous mem- ory locations, so that x occupies a total of 11 adjacent words. An element outside the dimension of an array, such as x(0) or x(12) , is a memory location that is probably occupied by some other data such as other variables in the program or by some machine instruction belonging the present executable or possibly the op- erating system. Unpredictable behavior ensues if your code tries to use an array element outside the dimensioned size of the array. The particular behavior depends on the data that happens to be in that memory location. In the actually happy event that you try to access operating system memory a segmentation fault occurs; the program dies an ignominous death and you know for sure something in the code is wrong. In other circumstances the erroneous value used does not cause the code to crash but produces wrong or unexpected results but without any warning that something went wrong. Such out of bounds array indexing must be zealously gaurded against by carefully checking the array indexing logic in the...
View Full Document

This note was uploaded on 01/08/2012 for the course MSC 321 taught by Professor Staff during the Fall '08 term at University of Miami.

Page1 / 9

lectarrays - Chapter 8 Arrays All variables we have used so...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online