MAE 185, Mechanical Eng. Dept. UCI

FORTRAN Exam.

Thursday April 14,2006

Questions. 1

Solution. 1

First problem.. 1

Second problem.. 1

Third problem.. 2

Fourth problem.. 2

Fifth problem.. 2

 

Questions

These are the questions.

Solution

First problem

Total 15 points.

 

For the first problem the grading was done as follows:

+1 point for correct declaration of program and variables used

+4 points for correct initialization of first row of matrix

+3 points for correct filling of at least one row in the matrix after first row

+6 points for correct filling of all rows in matrix

+1 point for correct printing of last row

 

These are 2 solutions. The first used FORTRAN90 which makes it a little easier to implement. The second uses FORTRAN77. 

 

The output of the program will be as follows

 

 $ g95 prob1.f90

 $ ./a.exe

 78729 78730 78731

Source code:

prob1.f90    FORTRAN90

prob1.f        FORTRAN77

 

Second problem

 

Total 10 points.

 

The grading was done as follows:

+1 point for correct declaration of variables used

+3 points for summing terms correctly even if each term was not correctly evaluated

+3 for calculating the terms themselves correctly

+1 for multiplying by x correctly

+1 for flipping the sign of the series terms from positive to negative

+1 for defining the function correctly

 

Source code

 prob2.f90

 

Third problem

Total 10 points.

 

The grading was done as follows:

+4 points for correct declaration of variables and subroutine

+1 for using the CALL statement since subroutine

+5 for finding the multiple in any method

 

Source code

prob3.f90

 

 

Fourth problem

Total 10 points.

 

The grading was done as follows:

+4 points for correct declaration of variables and subroutine

+1 for using the CALL statement since subroutine

+5 for correctly generating the Fibonacci sequence

 

Source code

prob4.f90

 

Fifth problem

(a) DEPENDS on FORTRAN  version 

Values assigned to a PARAMETER type variable must be known at compile time. These ‘variables’ are not really variables in the normal usage. They are constants and their values can not be changed at run-time. Think of them as READ ONLY variables. Hence the value of the PARAMETER constant must be known at the time the code is compiled. Since ACOS() is a function call, its result (the value it will return) will not be known until the program is run. Hence it is not possible to use its value at compiler time. Hence this statement will produce a compile error when using F77.

 

Example

                   PROGRAM prob5           

         PARAMETER (PI=ACOS(-1.))

         END PROGRAM

 

$ g77 prob5.f

prob5.f: In program `prob5':

prob5.f:4:

            PARAMETER (PI=ACOS(-1.))

                          ^

Invalid declaration of or reference to symbol `acos' at (^) [initially seen at (^)]

 

However, the above behavior depends on the compiler. A smart compiler might detect this, and set the value correctly (there are number of ways of doing this at compile time). For example, using F90, the above will NOT produce compile time error as shown below:

 

         PROGRAM prob5          

         PARAMETER (PI=ACOS(-1.))

         PRINT *,PI

         END PROGRAM

 

$ g95 t.f

$ ./a.exe

 3.1415927

 

So, no points will be taken off or added for this question.

 

(b)In F77 You can GOTO a continue label, but the GOTO needs to be inside the DO block. So the answer is TRUE. Notice that if the GOTO was outside the DO block, then F77 will give a compile time error, but F90 will give a warning.

 

Example

        PROGRAM prob5           

 

         GOTO 10

 

         DO 10 I=1,10

            PRINT*,I

  10     CONTINUE  

 

         END PROGRAM

$ g77 prob5.f

prob5.f: In program `prob5':

prob5.f:5:

            GOTO 10

                 1

prob5.f:7: (continued):

            DO 10 I=1,10

               2

Reference to label at (2) inconsistent with earlier reference at (1)

 

But when the GOTO inside the DO block, no error:

PROGRAM prob5          

        

       DO 100, I=1,3

          PRINT *,I

          GOTO 100

 100   CONTINUE

      

       END PROGRAM

 

 

(c)FALSE

You can’t use the same name for the main program and for the name of a function

 

Example

        PROGRAM prob5           

 

         END PROGRAM

 

         real function prob5(n)

         end function

 

$ g77 prob5.f

prob5.f:3:

            PROGRAM prob5

                    1

prob5.f:7: (continued):

            real function prob5(n)

                          2

Global name `prob5' defined at (2) already defined at (1) [info -f g77 M GLOBALS]