Augustana University College

COMPUTING SCIENCE 370
Programming Languages


Fortran Exercises and Examples



Exercises

  1. Use arithmetic IFs and assignment statements to store +1 in S if X > 0, store -1 in S if X < 0, and store 0 in S if X = 0.

    . See a sample solution and another sample solution.

    Do the same with logical IFs.

    . See a sample solution.

  2. Write a FORTRAN IV leading-decision loop that doubles N until it is greater than 100.

    . See a sample solution.

  3. Translate GOTO (10, 20, 30, 40), I into IF statements. (No solution given for this one -- it's too easy.)

  4. Write a computed GOTO that, on the basis of a number M representing a month, branches to label 100 if the month is February, to label 200 if the month has 30 days, and to label 300 if the month has 31 days.

    . The sample solution uses variable MONTH to represent the number M.

  5. Rewrite the following DO loop using only IF and GOTO.

          DO 100 I = 1, N
          A(I) = A(I) * 2
     100  CONTINUE
    

    . See a sample solution.

  6. Write a DO loop that computes into SUM the sum of the array elements A(1), A(2), . . ., A(100).

    . See a sample solution.

  7. Define a subroutine PUTABS (X, Y) that computes the absolute value of X and stores it in Y.

    . See a sample solution.

  8. Derive the addressing equation for a two-dimensional array with M rows and N columns stored in row-major order, using 1-based indexing (as in FORTRAN).

    . Do you want to see the solution?

    What would the equation be if we used 0-based indexing (as in C)?

    Do the same for a three-dimensional array of dimensions M x N x P stored in both column-major order with 1-based indexing (like FORTRAN) and row-major order with 0-based indexing (like C).

  9. What is the result of running this Fortran program if the parameters are passed by reference? What if they are passed by value-result? [This is based on MacLennan's Exercise 2-15, p. 60, but with the variable names corrected for implicit type.]

  10. What is the result of running this Fortran program if the parameters are passed by reference? What if they are passed by value-result, assuming that the address of the actual parameter is computed at subprogram entry only? What if the address of the actual parameter is computed at subprogram entry and recomputed at subprogram termination?

  11. Describe the output of this Fortran program if the parameters are passed by value-result, assuming that the address of the actual parameter is computed at subprogram entry only? What if the address of the actual parameter is computed at subprogram entry and again on exit? [This is based on MacLennan's Exercise 2-16, p. 61, but with the X changed to M for implicit typing as INTEGER.]

  12. Consider the Fortran subroutine SWAP, which swaps the values of two variables without using a temporary variable:

          SUBROUTINE SWAP (I, J)
          I = I + J
          J = I - J
          I = I - J
          RETURN
          END
    

    Does it work in this program?

    What about in this program, in which the subroutine is called with the same variable for both parameters?

    Here is an example using array elements instead of simple variables. Does it work as intended?

    Does it work in this program, where the array elements are made the same by EQUIVALENCE?

  13. Suggest a new syntax for the DO loop that is less prone to the mistake that was reputed to have caused the loss of the American Mariner I Venus probe.

    . Do you want to compare solutions?

  14. Classify the statement on line 5:

          DIMENSION FORMAT(100)
      5   FORMAT (11H) = 10*(I-J)
    

    . Answer

  15. Compare the precedence conventions of Fortran with those of C, Modula-2, and APL.

Examples

. common_block.f -- illustration of a subroutine using a COMMON block to pass values

. complex_func.f -- illustration of the use of type COMPLEX

. average.f -- illustration of the use of a FUNCTION subprogram

Copyright © 2000 Jonathan Mohr