Week 3 Questions


Why does Fortran require both a "stop" and "end" or a "return" and an "end" back to back? That seems redundant.

Actually, the use of stop and return in this case is an old and admittedly redundant programming practice. As we get into the use of IF and related statements you will see the potential for using STOP and RETURN on their own. The Fortran 77 and 90 standards state that if an END statement is reached in a main program it will terminate execution (same as STOP), and if an END is reached in a subprogram, it has the same effect as a RETURN.

What kind of Loops does Fortran have (For loops, While Loops, Repeat Loops)?

Older Fortrans had a fairly simple DO loop statement. Fortran 90 has enhanced these capabilities with DO WHILE constructs, the WHERE, CYCLE and EXIT statements. WHERE is a standalone loop type structure for use with arrays, and CYCLE and EXIT are statements that are used in conjunction with DO and DO WHILE loops. We'll get to this stuff later.

What is the difference between a PARAMETER and DATA statement?

The difference is if I set a parameter "x1" with:

parameter (x1=1.1534e-07)

x1 will always have that value, within the program unit containing the parameter statement. I can't change the value associated with x1 later. However, if I set x1 with:

data x1/1.1534e-07/

I can later change the value if necessary:


Choice between the two depends on your needs in the program.

Can you call an intrinsic function in a parameter statement?

No. You can only use simple operations +, -, *, and /, combined with constants (1.0, 1, 1.e1, etc) and previously defined parameters. You can use **, but only if the exponent is of type integer so that the compiler can generate the answer without using the intrinsic functions "exp" and "log".

Is it possible to print our Fortran code for reference during debug?

You can bring a copy to your own PC with FTP and print. Sometimes the "print Screen" button works on your PC, but Windows usually messes that one up. You can also copy the listing from your Telnet window using the Edit menu, and Paste it into a Word Processor window for printing. If you are in 316 Hammond, and want a copy of the file "hw4.f", type:

lpr -Pascii hw4.f

I am still unsure exactly how to put these functions and subroutine to good use. What exactly do the do and why are the better than a normal straight-out program?

First functions. The intrinsic "sin" function is a good example of why to use a function. There are many times that I want to use the sine of some number within a larger expression. For example the height at time , t, of a shell fired with initial speed, v0, and elevation angle, angrad, can be computed with the assignment statement:

height = v0*sin(angrad) - 0.5*g*t**2

The use of the function in this line is a natural match with what I would write as an equation of motion, and saves me preceding the evaluation of height with the many Fortran lines needed to generate a good approximation to the sine of angrad. Somebody else worried about the programming to get sin(angrad). This function is special only in that it used more often than most others. If I'm working with a heat conduction problem, I might have a statement requiring the use of a zero order Bessel function.

T= 300.+10.*exp(5.*time)*Bess0(4.*r)

Fortran doesn't provide Bessel functions in its set of intrinsic functions, but it lets me write my own.

function bess0(x) . . (a few hundred boring lines here that you don't want to see) . return end The nicest part of this is that I'm going to use this Bessel function many times in a given program, and use it in a lot of programs over the years. On set of Fortran Statements generates the values I need anywhere in the program. In new programs, all I have to do is to include a reference to the function in any appropriate Fortran statement, and include a copy of the Bessel function subprogram taken from one of my older programs (or my personal library of useful functions and subroutines). New work is minimal.

Look at htcoef1.f for a simple example of a function. More will follow during the semester.

The subroutine is appropriate when I'm doing things that don't naturally produce a number to be used in the middle of some formula. The best reason for the subroutine is when this group of statements will be used many times by your program (see htcoef2.f). However, it is also a good idea to group lines that perform a well defined task into a subroutine, even if they are used just once. It adds to the clarity of your program, helping to create an outline structure. You will gain further appreciation for the value of subprograms as the semester goes on.

Are subroutines compiled as separate programs?

In effect yes. The compiler processes each program unit (main program, subroutine, function) alone, without thinking about the other units. Without telling you, it passes along its results to another program called a loader or linker that takes care of the final details of adjusting address references so the main program and subprograms can all talk to each other. Later in the semester, I will show you an example where I compile some subroutines by themselves, without creating an executable program. In a separate operation I will compile the main program and link it to the results of the earlier subroutine compilation.

I don't understand when you want to debug a program and you type f77 -g debug.f , to debug the program your working on, or is it a special program for debugging.

My choice of examples was poor. The file "debug.f" was just another Fortran program. I could have used the program "htcoef2.f". To prepare for debugging, I would have then typed:

f77 -g htcoef2.f

Please note that at this point, the only unusual thing I have done is to write some extra information to the executable file "a.out" for possible later use by the debugger. I can execute "a.out" just as before, and may never decide to do any debugging. The actual process of debugging begins when I type:

dbx a.out

I had trouble when I typed f77 to compile my 1st program. I was in the homework directory, do I have to be in the main directory? I was told by Unix that I had too many parameters when typing "f77 hw3.f".

The "too many parameters" message coming from Unix usually means you put a space somewhere you shouldn't like "hw3 .f". When you type the command you should be in the same directory as the file "hw3.f".

Must you use Real and Integer in homeworks?

For any homework assignment, you must use REAL and INTEGER statements to declare the appropriate type of all variables that you use. At one time or another, you will be forced to use both of these data types. You will also eventually be using LOGICAL and CHARACTER types and DOUBLE PRECISION (REAL(8)).

Do you have to count spaces or is there a command to show what space you are in?

I don't know of any way to do this in vi, other than count spaces. You can include comment lines in the program that act as space counters:

c 1 2 3 4 5 6 7 c23456789012345678901234567890123456789012345678901234567890123456789012 How does dbx work?

I've never looked at the details of dbx. Debuggers have a couple ways of doing what they do. In the simplest implementation dbx is acting as a conduit for the instructions in your program. In this mode dbx is pulling instructions or blocks of instructions out of your executable file "a.out", and passing them to the CPU (of course the CPU is running dbx too, but sorting such things out is the operating system's problem). When you ask for a breakpoint at line 100, dbx looks in the symbol table to find the precise instruction in "a.out" that corresponds to the beginning of that line. When you type "run" or "cont", it then passes all program instructions up to that one at the beginning of line 100 on to the CPU, before prompting you for more dbx commands. If you ask for the value of "x" using "print x", dbx looks through the symbol table for the memory address of x and the type of x (real or integer), grabs the value from memory, and prints it in the appropriate format.

Why are different languages used in different fields. Why do EE's use C and other Engineers Fortran? How come no one seems to use Pascal or Quick Basic?

As I've said before different languages permit execution of different classes of programs with varying degrees of difficulty. If I need to do a lot of manipulation of character strings, or need very good access to all of the Unix systems routines, I use C. If I need to do serious number crunching on the fastest machines for simulations of things like fluid flows or the dynamics of mechanical systems, I use Fortran to take advantage of its array constructs and intrinsic functions. At one time I would have chosen Fortran, simply because it was more standardized, but standardization of C is good now too. I don't know of International standards for Pascal or Basic, but they are a different story.

Another major driving force behind use of languages, is history. A language can come to dominate a field simply because most applications in that field have been historically written in that language, and members of that specialty get used to the particular language from modifying these older applications. At some point this accumulation of history can work in the opposite direction. I may remain obscure as a modifier of old codes, but make a major reputation for myself as the first to implement a significant application in a trendy new computer language (regardless of the actual merits of the language). A similar dynamic is at work in the proliferation of computer languages. As a computer scientist I could become a lot more famous inventing a new language, than finding better ways to modify an old one (yes, I'm getting a little cynical in my old age).

However, history and the accumulation of applications tends to dominate. The cost in dollars of converting old applications to a new language is so large, that groups working in a given area train their new workers and students to use the language of history, rather than let them redo old work in whatever language they first learned. If the language of choice is found to be less than adequate, it is easier to extend the language with new standards (e.g. Fortran 90) than to throw out everything and start from scratch with a totally different language. Pascal, Basic and a host of other languages, never really dropped into the historical development of major fields of research at the right times, nor did they generally arrive with significantly advantageous features. However, when the new area of writing software for imbedded systems (things like missile guidance systems, fire control, etc.) took off in the military an opening existed for a new language, and Pascal was reincarnated as Ada.

If you ever want a taste of the costs of converting to different languages, take a look at decisions to acquire new computers within major organizations. It is a general rule that when a change in computers is accompanied by a change in operating system, the cost of converting existing applications to interface with the new operating system exceeds the cost of the new hardware by a substantial factor. Adapting a program to use a new operating system involves only a partial recoding, not a total reprogramming effort.

Can Fortran do Graphics?

No, and Yes. Specific Graphics capabilities are not part of the Fortran standard, but specific Graphics capabilities are not part of the ANSI C standards. Graphics eventually require hardware specific actions to put the right color and/or intensity at a given point on a screen. Functions and subroutines are available on the vast majority of systems that can be called from Fortran to do such jobs with varying levels of convenience. For Workstations, you should look for ways to call XWindows routines for Graphics. For PC's check the manual of your specific Fortran compiler.

What is the command to compile and run a Fortran program? I was able to write but not run hw3.f.

You can't compile and run with one command with Fortran on Unix (unless you write a script). The two commands you type to compile and run hw3.f are:

f77 hw3.f a.out


Why do I always type "a.out" to execute a file after every Compilation. Why for instance can't I type "mult.out" after I compile my "mult.f" file?

The reason is that "a.out" is the name of the file created by f77, containing the machine instructions corresponding to your Fortran program. It's just a default convention used by f77 (and the C compiler "cc"). If you would like another name use the -o option. If you would like to type "mult.out" then compile with the line:

f77 mult.f -o mult.out

How do I get a file like "add.f" up on my screen to edit and change?

If you want to change it, you must copy it to your own directory space first. Change to the subdirectory where you what it with the "cd" command then use "cp" to copy it in and "vi" to edit it.

cd homework cp ~jhm/201/add.f . vi add.f

How do I save my homework directly into my homework subdirectory?

After you login, type "cd homework", before starting "vi" to create a program file. When you are done the file will be left in the homework directory. The commands:

cd homework vi hw4.f will result in a file named hw4.f in your homework subdirectory.

How do I deal with strange behavior of the screen and cursor in vi?

If you just see a few lines that stay at the top of the screen while everything else moves, its because you have more than the vt100 standard of 24 lines visible on the screen. No big deal there. If lines are abruptly vanishing when they shouldn't, or the cursor is jumping to unexpected positions, use the "set term=vt100" command (see below)

Why do you sometimes type "set term=vt100"? Should I do this every time I use Unix?

Use of the "set term" command is necessary when Unix is confused about how to display things (particularly in programs like "vi") on your screen. Most of the Telnet programs on campus leave Unix in this state of confusion when you login. When I'm using one of these, by typing "set term=vt100", I tell Unix exactly how to deal with display to my screen. Telnet is running something called a "vt100" terminal emulation. At the Hammond Workstations, your terminal windows are called "xterm", but you normally don't need to type "set term=xterm", because the Xterm software is better at talking to Unix. If you follow the instructions below to force a "vt100" setting at login time, you may want to type "set term=xterm" when sitting at one of the Hammond workstations (or use my "hamm" alias). I say may, because the Xterm software is smart enough to deal with "vt100" communications. You just don't get all of the power of a full Xterm.

Is there a way I can program or alias so that I won't always have to type "set term=vt100" and the stty command?

Yes, if you are only going to be using 316 Hammond rarely, and generally will do your work from a Telnet session, you can embed commands in your ".cshrc" file, so they are executed whenever you login. As a preparation you need to figure out what the character is coming from the key that you want to use for erasures. Run through the standard drill of typing "stty erase" followed by a space followed by hitting the key you want for erasure (usually the Backspace key). Look at the screen to see what pops up when you hit that erasure key. I usually see a caret followed by a question mark (^?). If you see something different replace it below at the point where I include "^?". The next thing you should do is go to your home directory (typing "cd" will do it), and issue the following copy command to protect yourself.

cp .cshrc old.cshrc

You are about to change an important file, so you need the backup. Next open ".cshrc" with vi and position yourself at the end of the last line. Go into insert mode and enter the following lines, including the single and double quotes as I've given them (but adjusting for the question mark if needed).

set term=vt100

stty erase '^?'

alias hamm "set term=xterm;stty erase '^H'"

The last line creates an alias to make your life easier in 316 Hammond. When working directly at the workstation type "hamm" before doing anything else in a given Xterm window. You could mimic this alias for setting needed on a Telnet session, if you normally use Hammond, but occasionally use Telnet sessions.

Is there some way to increase the font size of Netscape on Unix machines?

In theory, yes. I am not an XWindows guru, and don't keep the options in my head. However, you have a wide range of control on fonts of any X application. I'll try some stuff, during the week when I have access to a Workstation, and will update this answer (watch this file for a new answer).

General Computing

When I telnet from my home computer, I sometimes have a problem copying files from your directory, like trig.f. What is the correct procedure?

Use the "cd" command to put you in the directory (on the Hammond machine) where you want my file then type:

cp ~jhm/201/trig.f .

Don't forget that last period, or the space before the period. Sometimes, I screw-up and don't set the right protections on such files. If you get a message about wrong protections or privileges, send me mail. Note that if you want to copy such files to your own PC, the "cp" command is of no use. The easiest thing you can do is to open the file through Netscape then use the "Save as" option from the "File" menu.

Class Procedures

Will there be more Hammond Help sessions?

Willie's 3:30-5:00, Tuesday and Wednesday hours in 316 Hammond are good all semester. Count on a 12:30 - 2:00 session on Thursdays. If they won't give us the classroom then, Willie or I will simply sit in the laboratory section of the room on the other side of the temporary partition. You can't make one of these times, remember that I have a Workstation in my office and can demonstrate most stuff there 9-10 MWF. If all this fails, feel free to set up an appointment.

Is there a book that explains the heat transfer stuff that you talk about.

Many such books. I don't have a personal favorite, but when I want to learn about something new, as you do with heat transfer, I take advantage of the Dewey Decimal system. Look for the call numbers on a few books listed under the general areas of "convective heat transfer" or just "heat transfer", using Lias. When you see an obvious clustering of numbers, go park yourself at that location in the shelves of the Engineering Library. Page through some books until you find one that makes sense to you. Remember that this is a fairly old field. Just because a book has a 1990's copyright doesn't mean that it will be the clearest text on the subject.

I have not received any E-mail saying that my second homework was received, but I did get mail for the third, is this true for the whole class?

Good chance that you didn't get E-mail on the second. We will be posting our current understanding of the full class status on homework. Please check and let me know if there are problems, either in person, or with E-mail to cs201@mowgli.psu.edu.

Will there be a review session for the exam?

One class will be devoted to review for each mid-term exam, and two classes to review at the end of the semester. Come to these classes prepared to ask questions.

Is my hw3.f supposed to be under my "homework" directory?


Will the grades in this course be scaled if they are too low.

No scaling on homework. I will "renormalize" test scores using a program that you will see later in the semester.

Do I have to run "setup" for each homework?

No, that was a one shot deal. Don't run it again.

Are we really getting accounts in the ECSEL lab?

They have not been very responsive in setting up accounts. We are officially authorized to use the lab, but there has been a disconnect with the systems manager. I'll try again.

I'm a little scared of using the Workstations. Could you or Willie help me out some time?

Certainly. If you can't catch Willie 3:30-5:00 on Tuesdays and Wednesdays, or me during office hours, talk to me after class or via E-mail to set up an appointment. Just don't ask me to give you much help on a PC with Windows 95, that really scares me.