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.

INTEGER :: num(:)<br />

CHARACTER :: letter(:)<br />

END SUBROUTINE convert<br />

END INTERFACE<br />

PRINT *, 'Numerical score:', test_score<br />

CALL convert(test_score, letter_grade)<br />

PRINT '(A,10A3)', ' Letter grade: ', letter_grade<br />

END PROGRAM main<br />

SUBROUTINE convert(num, letter)<br />

! declare the dummy arguments as assumed-shape arrays<br />

INTEGER :: num(:)<br />

CHARACTER :: letter(:)<br />

! use the WHERE statements to figure the letter grade<br />

! equivalents<br />

WHERE (num >= 90) letter = 'A'<br />

WHERE (num >= 80 .AND. num < 90) letter = 'B'<br />

WHERE (num >= 70 .AND. num < 80) letter = 'C'<br />

WHERE (num >= 60 .AND. num < 70) letter = 'D'<br />

WHERE (num < 60) letter = 'F'<br />

END SUBROUTINE convert<br />

Expressions and assignment<br />

Assignment<br />

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

asamplerun:<br />

$ f90 score2grade.f90<br />

$ a.out<br />

Numerical score: 75 87 99 63 75 51 79 85 93 80<br />

Letter grade: C B A D C F C B A B<br />

The next example is a subroutine that uses the WHERE construct to replace each positive<br />

element of array a by its square root. The remaining elements calculate the complex square<br />

roots of their values, which are then stored in the corresponding elements of the complex<br />

array ca. IntheELSEWHERE part of the construct, the assignment to array a should not appear<br />

before the assignment to array ca; otherwise,allofca will be set to zero.<br />

SUBROUTINE find_sqrt(a, ca)<br />

REAL :: a(:)<br />

COMPLEX :: ca(:)<br />

WHERE (a > 0.0)<br />

ca = CMPLX(0.0)<br />

a = SQRT(a)<br />

ELSEWHERE<br />

ca = SQRT(CMPLX(a))<br />

a = 0.0<br />

END WHERE<br />

END SUBROUTINE find_sqrt<br />

Chapter 5 101

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

Saved successfully!

Ooh no, something went wrong!