02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 4-3 vector_sub.f90<br />

PROGRAM main<br />

! m is a rank-1 array that has been<br />

! initialized with the values of an array constructor<br />

INTEGER, DIMENSION(4) :: m = (/ 2, 3, 8, 1/)<br />

INTEGER :: i<br />

! initialize a (a rank-1 array) with the values<br />

! 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 11.0<br />

REAL, DIMENSION(10) :: a = (/ (i*1.1, i=1,10) /)<br />

! b is an uninitialized 4x2 array<br />

REAL, DIMENSION(4,2) :: b<br />

! print a section of a, using a vector subscipt<br />

PRINT *,a(m)<br />

! assign the values 5.5, 11.0, 6.6, and 5.5 to the first column<br />

! b; this is an example of a many-one array<br />

b(:,1) = a( (/ 5, 10, 6, 5/) )<br />

! the vector subscript MIN(m,4) represents a rank-1 array with<br />

! the values 2, 3, 4, 1; the second column of b is assigned<br />

! the values 11.0, 6.6, 5.5, 5.5<br />

b(:,2) = b(MIN(m,4),1)<br />

! increment a(2), a(3), a(8), and a(1) by 20.0<br />

a(m) = a(m) + 20.0<br />

! print the new values in a<br />

PRINT *,a<br />

END PROGRAM main<br />

Arrays<br />

Array sections<br />

Here are the command lines to compile and execute the program, along with the output from<br />

asamplerun:<br />

$ f90 vector_sub.f90<br />

$ a.out<br />

2.2 3.3 8.8 1.1<br />

21.1 22.2 23.3 4.4 5.5 6.6 7.7 28.8 9.9 11.0<br />

Chapter 4 69

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!