Week 2 Questions

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

General Programming Practices

. Exactly how is a program written?

The problem with any programming course is that we have to answer that question in small pieces. Set yourself the task of writing a program that adds the numbers 1 through 10 and prints the results. Look at my simple and modular examples, for clues and take a run at it. You should start by creating a text file with the command:

vi test.f

Enter insertion mode immediately by typing "I", then type the statement to start the program:

      program test
The name "test" is arbitrary. Be sure to hit the Space bar at least 6 times before typing "program test". Hit a return and include the mandatory line:

      implicit none
telling the compiler to make no implicit assumptions about variable types. Then declare the type for the result of your calculation:

      integer numsum
Now you are ready to do the actual work, so type an assignment statement.

      numsum = 1 + 2+3+4+5+6+7+8+9+10
A Fortran program will not print any results to the screen, unless you tell it to, so include a print statement:

      print *, ' Sum of numbers from 1 to 10 is : ', numsum
Here the "print *," is mandatory syntax to print something to your screen. Anything enclosed in single (or double) quotes is printed literally. Any variable name after the "print *," causes the value of that variable to be printed. The items that you want to print (literal strings and variables) must be separated by commas.

OK, now you are done, so you should tell the program to stop execution with :

and you must tell the compiler that you've reached the end of the program unit with:

Your vi screen should now look like this:

      program test
      implicit none
      integer numsum
      numsum = 1 + 2+3+4+5+6+7+8+9+10
      print *, ' Sum of numbers from 1 to 10 is : ', numsum
Hit the Escape key to leave insert mode, then type ":wq" to save your work.

You must now convert your Fortran into the binary language that the computer actually understands by typing:

f77 test.f

This creates a file named "a.out" containing machine instructions. Type "ls" to see that the file "a.out" is really there. If you open "a.out" with vi (not recommended), you with just see gibberish. "a.out" doesn't contain text as you understand it, and vi's attempt to translate it to text can range from goofy to fatal.

Now you are ready to execute the program, and look at the results. Just type the name of the executable file:


Hopefully it will tell you that the answer is 55.

How do I know if my programs are saved?

When I'm worried about the success of a save by an editor ( in vi the ":w" command), I do one of 2 things after finishing with the editor and/or copying the file to a new directory. The quickest is type "ls -l"while in the directory were you expect your file (for you "~/homework"), and check to see that the time of last modification is close to the reading on my watch (this assumes I've set my watch accurately). The more thorough method is to use the Unix "more" command. If I have just finished changing a file named "test.f" then I type:

more test.f

It displays the contents of the file on the screen one screenful at a time. To see more of the file, I hit the Space bar. If I've seen enough to know that the changes are in place, I type "q" to end the program. Another option to "more" is to use "view". It behaves like vi, but never changes your file.

Fortran Language

My question is what does Subroutine do?

Structurally a Subroutine provides a way to isolate a specific task in your program. From the standpoint of general function, a Subroutine can do anything within the capabilities of the Fortran language. You can include any Fortran executable statement or any Fortran data type within a Subroutine. Watch carefully how Subroutines are used in the first few homeworks, and examples associated with the lectures.

What does a Module do?

For now all a module does is 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.

My question is in regards to the floating point constants, can you explain them again?

There are three important things to remember about floating point constants.

If you don't write a decimal point in the number ( 2.0, 2., 2.0e0, 2.e0) Fortran will interpret it as an integer, and if an integer constant is paired with some other integer in any simple arithmetic operation (+,-,*,/) the integer arithmetic unit will be used (possible trouble with a divide).

You don't always get what you think when using an integer constant in an expression. For example I may write "x = y + 2.1", but the computer is probably actually doing something like "x = y + 2.09999900". This is because you can't represent 1/10 exactly with a binary (base 2) fraction. Usually this is part of the lack of absolute precision that you learn to live with on computers, but remember this point when you start trying to test values of numbers with the IF statement.

If "x" and "y" are double precision variables, then the expression "x = y + 2.1" will probably give you substantially less accuracy than you expect. You need to tell the computer to generate full double precision representation for "2.1". We will cover the fine points of this later in the semester.

Fortran Compiler

Also, you make it sound as though the compiler is machine specific, if so, does it come pre-programmed in each computer or is that why Fortran software is sold?

Most Unix Workstation vendors offer a Fortran compiler as optional software when you buy the computer. Most Personal Computer companies do not, and you have to look for a software company that has custom programmed a Fortran compiler for the CPU in your PC. Actually the entire compiler is not redone for each computer, only a relatively small fraction dealing with the direct translation of elementary operations to specific machine instructions needs to be built for each new CPU.


I'm still baffled about the "off the wall" commands.

Arrange a help session with me or with your TA. Your life in this class becomes much easier once the basic commands make sense to you.

What exactly does the man command do?

Unix keeps a bunch of files tucked away called manual pages, containing descriptions of just about every command and utility program that comes with the operating system. The "man" command provides a convenient way of locating and viewing these files. When I type "man ls", "man" locates the file containing documentation for "ls", if necessary converts it to a "nice" format, then feeds the result to the Unix utility "more" to display one page at a time on your screen. When you want to see more of a document hit the Space bar, when you've seen enough, type "q" to quit. You can also search for a particular word or phrase by typing "/" followed by that word or phrase, followed by a Return (Enter).

I was working on the vi tutorial in my dorm room and if I typed in a wrong letter and wanted to delete it, the backspace button would not work. It kept typing a question mark (^?) instead. I was wondering if there was something I could do about that.

At times confusion develops between Unix and your terminal emulator about what to use for an Erase command. If Backspace isn't working, holding the Ctrl key while hitting Backspace, usually does. One way to deal with it is to check the menus on your terminal software to see if there is something about setting Erase or DEL. The other method of fixing the problem is implemented directly under Unix. After you login type "stty erase" followed by a space then hit the key that you want to use for erasures (probably Backspace), then hit return. If you find yourself using stty all the time, you can include the following line in your ".cshrc" file:

stty erase '^?'

When I type "vi fall.f" I get this message "There is not enough space in file system." I was wondering how to correct this?

The problem was that the directory named "/var/tmp" has filled up. The system administrator found the problem on 7 of the machines and cleaned it up. If this happens again, your easiest way out is to move to another machine, but please send E-mail to "ptpadmin@psu.edu" so they can clean things out.

What is the purpose of vi?

To create files containing simple text. In this class that means: "to create files containing Fortran programs." Also, take a look at the next pair of questions.

Is there something special about Unix that will help in our programming, either now or later?

How will we use the things we learned about UNIX and vi and use them in this course about Fortran. Its not that I don't comprehend the individual things but if they are connected and how.

There is not much special about Unix relative to other operating systems, except that it currently dominates the Workstation market for Computing in Science and Engineering. When you program in Fortran, C, C++, Ada, Basic, or any of a dozen other languages, you need to create a standard text file containing the statements in your language that a compiler will later convert to another file containing executable machine instructions. Unix is one fairly good way to manage these text and executable files. It provides several text editors, including "vi" to create your program text files. It then keeps track of the files in an orderly way, protects them from tampering by others, lets you copy or move them as needed, and does a whole lot more that we won't try to cover. When the time comes to execute your program, Unix protects you and the computer from drastic mistakes, and gives you a fair amount of flexibility in picking up input and routing output. Other operating systems will do the same things for you with varying degrees of comfort or discomfort. Perhaps Windows NT will replace Unix in the next few years, perhaps not, but bet that Unix will be superseded within your professional lifetime. What is important now is that you get used to the general work pattern of creating, changing, neatly storing, compiling, and executing programs. I haven't seen these patterns shift in 30 years using 10-20 operating systems, and they are not likely to shift drastically in the future.

After playing with the VI tutor, I found it very aggravating and annoying. Is it possible to write the programs on say notepad or Word?

Watch me work sometime, and you will see that I almost never use vi. It drives me nuts! However, I always keep a few vi commands (x, dd, yy, p, i, a, r, :wq, :q!) in my brain, so that I can sit down at any Unix Workstation and do something useful. Yes, you can use Notepad or Word (with Word save the results as a Text file). You then need to know how to use FTP to move the file between your PC and the Unix machines. If you work directly in Hammond, try "xedit". For example to change the file "fall.f" type:

xedit fall.f

How come the cursor much of the time does not respond to my typing in vi?

Probably because you are in command mode and not in insert mode. In command mode the cursor is moved by typing j (down), k (up), h (left), or l (right) or using the arrow keys. If these don't do anything for you, try typing "set term=vt100" before your first use of vi.

In vi, when do you have to hit the Escape key to end a command and when can you just remain in the Command Mode and move on to your next task?

The basic rule is that if what you are currently typing is appearing on the screen as text to be included in your file, then you must hit the Escape key to use other vi commands. Otherwise, you can continue to type other commands without intervening Escapes. Note that if you use a command beginning with ":", it will appear at the bottom of the screen, but is not being entered in your file. Commands beginning with a colon must be ended by hitting the Return (Enter) key.

I am curious why DOS and Unix commands are represented by different characters, since they do the same thing.

The operating systems were written by different people who had different ideas about what is easy to remember and use. Both groups of people were aware of syntax in older operating systems, and so some commands are the same. Unix operated under the further constraint that it was developed at a time when keyboard response on computer terminals was truly awful (you had to use one to really understand this problem). Minimizing the command "copy" to "cp" was believed to have a significant impact on a programmers ability to do work.

DOS has a command to clear the screen and take the cursor to the top. Is there an equivalent in vi?

First in "vi" you are building a text file and don't usually want to clear out your text. The closest thing to what you seem to want would either "G" which positions you at the last line of the file, or "z" followed by a Return (Enter) which repositions the display so that the line currently containing the cursor is at the top of the screen.

The closest parallel to what you want to do is to clear the screen and put the cursor and prompt at the top when you are entering Unix commands. The way that you do this is to type "clear" at the Unix shell prompt.

Why is DOS dying out and UNIX growing? What are the differences which make UNIX so much more appealing?

DOS was designed for use on isolated single user Personal Computers. Unix was designed for use on multi-user, interconnected computers. DOS was designed with some very conservative assumptions on maximum needs for machine memory and disk space. Unix was designed with no serious limits on machine memory or disk space. DOS has been very tightly coupled to the INTEL family of CPU chips. Unix was designed for easy adaptation to just about any machine, and in fact has been implemented on just about every type computer. Although the success of Unix is indirectly responsible for the slow death of DOS, the direct reason is the rise of Windows 95 and Windows NT. Unix may soon be eclipsed by Windows NT, which has similar features and capabilities. Hard to tell how various marketing operations will prosper.

General Computing

How do you set up E-mail?

If you still have problems after the Monday Recitation talk to me or your TA.

I would like to know more about FTP.

You should get some instruction as part of the Monday Recitation. Also take a look at my past answers to FTP questions.

Are there differences when Telneting from a computer that's not yours?

Probably, the standard distribution called CACTWIN contains a different Telnet program than is provided in the CAC labs. They could have different default interpretations of the Backspace key, and not all Telnet packages (including the one in CACTWIN) manage to tell Unix that you have a vt100 terminal emulation. Check my comments above on the "stty" command and always check to see if the command "set term=vt100" improves things.

Can we use ftp to transfer our work into our homework directories?

Yes. This is particularly useful if you make program changes on your PC Word Processor.

Is it possible to get into Netscape from telnet?

No. Telnet is just a simple terminal session with another computer. Usually, it only deals with you in simple text. If you are really lucky a good Telnet package will do something called "Tektronics" emulation, permitting you to put simple x-y plots on your screen. Netscape is a separate Internet program from Telnet with very different uses.

When I log onto the Hammond machines from my PC, I always get a message that I have "mail", but can't read it by typing mail.

Last I checked, the Hammond machines were checking your standard "psu.edu" mailbox, but weren't bright enough to actually read it with the "mail" utility. The computer center made a decision that Hammond lab was for class work, and have placed no priority on making it any easy place to read mail. Those people are heavily overworked, so don't look for any improvement during your stay as a student.

Numerical Methods

Class Procedures

How close are we following the syllabus? I printed out the notes, but I'm not sure where we are in them.

We will follow the notes fairly closely (Monday is the lecture on Intrinsic Functions). However, I enough flexibility in the schedule to slow things down when there are class questions. I've added dates in the list of lectures, next to the lecture numbers to help you track the progress of the class.

Are there notes available from morning lectures?

Only those on the Web (www.nuce.psu.edu/~jhm/201/lectures/)

Could we be told how to do things in our homework earlier in Recitation?

Will see what can be done in recitation and lecture. Your best bet is to hit us with questions when something is not clear. I've been doing this stuff too long and need to be reminded from time to time that certain things are not obvious.

Instructions for Homework are a little shaky. I'm new at programming.

I'll try to refine them a bit. However, I've found that it is very difficult to write instructions that register first time with everybody. That's why I go out of my way to talk to you individually. If you start working through a Homework with me or a TA around, it is usually fairly easy to iron out misunderstandings.

Is it true that the only two sources to check for Homework assignments on the homepage are: 1) Homework Assignments; and 2) Application Labs.

Those are the only two places that have homework assignments affecting your grade.

Do you recommend printing the front page of a lecture notes and not any of the links off of the page?

Generally, yes. But use common sense. Once in a while the pages like the one introducing the modular form of my simple program, that contain a major part of my lecture. Later in the semester, such important linked pages will be even more obvious, because they are Postscript files containing material that I didn't have time to convert to a standard Web page. I would not print any of the standard "Definition" pages.

What will be the format of the exams?

Mixture of true/false, multiple choice and fill in the Fortran. See the page with past exams.

When we moved add.f to snarf.f, I didn't understand because I thought that there already ad to be a snarf directory in there. Then you had us check to see that it wasn't here anymore once we got rid of it, but I didn't think it was really there to begin with.

That exercise was probably moving to fast. The root of your confusion is that "mv" can act in to ways. It can move a file into a named directory:

mv add.f ~/homework
or it can move a file to another file name (change the name of the file):

mv add.f snarf.f
"mv" decides which mode of operation you are asking for by looking at its second argument. In my first example it looks for "~/homework" and finds a directory with that name. Therefore, it moves "add.f" into that directory, maintaining the name "add.f". In the second example, it looks for "snarf.f", finds no directory with that name, so says "fine, I'll give the file add.f the new name snarf.f". If in the process, a file already exists with the name snarf.f, "mv" gets rid of the old one to make way for the new "snarf.f" (unless you used the "-I" option: mv -i add.f snarf.f). If you type "ls" after "mv add.f snarf.f" you should see that no file exists with the name "add.f", but one does exist with the name "snarf.f".

Back to other 1997 Questions and Answers / Home

Maintained by John Mahaffy : jhm@psu.edu