Week 5 Questions


General Programming Practices, Fortran Language, Fortran Compiler, Unix, General Computing, Numerical Methods, Class Procedures


General Programming Practices

How many comments are enough and how specific do you want us to be?

You should have 3 classes of comments: 1) Your Name and date; 2) A short definition (one sentence or less) of each significant variable (don't bother with do loop indices); and 3) Short (1-4 sentence) description of the purpose of the code.

Fortran Language

Do we always need to write ELSE IF or can we just list the equations?

First ELSE IF is optional in any IF, THEN structure. There are times it never appears. Second, there is no need to repeat the same condition for every thing that is done when the condition is true. For example if I want to set x=1, y=2, and z=3 whenever j is greater than 2

What is a module?

A module is a way to reserve space in the computer memory for specific variables. For example

      module example1
      real x, y, z
      end module
Sets up specific memory locations for 3 real (floating point) numbers, naming them "x", "y", and "z". Any program unit (for example a Subroutine) that contains the statement "use example1" can use or change the contents of these 3 variables. Look at how the Modules in Homework 3 are used to setup variables that are used by several subroutines.

Can you go over IF statements again?

An IF statement is a direct reflection of the branching logic in a CPU. The parentheses contain a logical expression which evaluates to either .TRUE. or .FALSE.. (except for arithmetic IF statements, but forget that for now). In a simple if statement such as:

IF ( x.gt.0) y = log(x)

Whatever immediately follows the right parenthesis is executed when the expression in the parentheses evaluates to .TRUE., if the evaluation is .FALSE. then execution moves immediately to the next line.

For an IF THEN the pattern is different. If the logical expression is true, then any code immediately following the IF (...) THEN is executed, until an ELSE is hit, then all lines are skipped, until the line after the ENDIF associated with that IF, THEN. When the logical expression is false, execution branches to the first "ELSE" or "ELSE IF" that is found, or if none are found it branches to the line after the ENDIF associated with that IF, THEN.

Why can numbers like 100 or 200 be in columns 1-5 and still compile correctly?

The compiler looks for integers in these columns, and if found (without a C, c, or * in column 1), decides that the number is a line label. It uses that number as a identification number for the line for later use in GOTO branching or references to FORMAT statements.

Will the test cover stuff that we covered in the very beginning (CPU and that kind of stuff).

Yes. Take a look at last years exam for an example of the level of detail. You are particularly expected to know what range of real and integer numbers you can represent with 32 bits and the range of reals that can be covered with 64 bits.

I still need help understanding GO TO statements and how to convert them into SELECT CASE.

First understand that only the Computed GO TO converts to a SELECT CASE. Beyond that, its just a case of figuring what label goes with what value of the index variable. For example look at:

      go to (100, 200, 300) I
      print *, 'Bad value of I '
  100 y =x
      go to 400
 200 y = x**2
      go to 400
 300 y = x**3
 400 continue
looking at the GO TO we see from counting that label 100 is the case of I=1, label 200 is the case of I=2, and label 300 is the case of I=3. If "I" has any other value execution just drops through to the line "print *, 'Bad value of I '". Now we are ready to convert these words into some code. First add lines to make note of the 4 cases that I've listed.

      go to (100, 200, 300) I
      case default
      print *, 'Bad value of I '
      case (1)
  100 y =x
      go to 400
      case (2)
 200  y = x**2
      go to 400
      case (3)
 300  y = x**3
 400  continue
next you can replace the GO TO with the appropriate SELECT CASE, and noting that the GOTO is over at label 400 insert an END CASE

      select case (I)
      case default
      print *, 'Bad value of I '
      case (1)
  100 y =x
      go to 400
      case (2)
 200  y = x**2
      go to 400
      case (3)
 300  y = x**3
      end select
 400  continue
the code above will work, but the "go to 400" statements aren't needed because each case branches to the "end select" after it's last line (before the next "case"). This makes the "400 continue" statement unnecessary. Also if you look, nothing refers to labels 100, 200 and 300, so you may as well pull them too.

      select case (I)
      case default
         print *, 'Bad value of I '
      case (1)
         y =x
      case (2)
         y = x**2

      case (3)
         y = x**3
      end select
I've added some indentation to make things look nicer.

How do we know when to use the IF statement versus the SELECT CASE Statement?

Consider SELECT CASE, when you find yourself writing an IF THEN with lots of ELSE IFs containing simple tests of the same integer variable (or expression) against various constant values,

      if (j.eq.1) then
      ...
      else if (j.eq.2) then
      else if (j.ge.3.and.j.le.6) then
      else if (j.eq.7) then
      etc.
or when you are generating a similar structure comparing the same CHARACTER variable to quoted character strings.

      if (name.eq.'John') then
      else if (name.eq.'Sally') then
      else if (name.eq.'Bill') then
      else if (name.eq.'Jill') then
Note the pattern of repeated use of "j" and "name" in the above examples.


Fortran Compiler

Unix

General Computing

Numerical Methods

I don't understand the equation we used for the linear interpolation. How is it derived?

It is really just a case of finding a straight line that passes through two points. When you do an interpolation you are always given the values of some dependent variable at two specific values of a dependent variable. For example I know the value of the velocity of a car at two points in time. Call these V1 at t1 and V2 at t2. When I graph velocity versus time these are points (t1,V1) and (t2,V2). Lacking other information I can approximate the velocity at any time between t1 and t2 from the equation of a straight line through (t1,V1) and (t2,V2). I like to derive the equation of that line through ratios. For any point (t,V) on the line we know that:

( V  - V1 )    ( t  - t1 )
----------- =  -----------
( V2 - V1 )    ( t2 - t1 )

or

                    (t  - t1 )
V = V1 + ( V2 - V1 )----------
                    ( t2 - t1)
or

     (t  - t1 )              (t  - t1 )
V =  ---------- V1 + [ 1 -   ---------- ] V2
     ( t2 - t1)              ( t2 - t1)

Class Procedures

Is our test going to be similar to those you have on file? Has the material changed since those exams were given, or are we expected to know the same things?

Similar, yes. However, the material has changed. For example MODULEs are covered earlier this year. Look for questions that match material covered in the Web notes through 2/17/97. Also, be aware that the "essay" portion of the test is a larger fraction this year (~50%), but for the most part bears a stronger resemblance to the homework.

Back to other 1997 Questions and Answers / Home


Maintained by John Mahaffy : jhm@psu.edu