Computer Science
1107
AN INTRODUCTION
TO FORTRAN
PROGRAMMING
FOR SCIENTISTS AND ENGINEERS
Fall 2001
Objective: To become familiar with FORTRAN 77 program development in the UNIX
environment.
Description: The following is a series of steps that will familiarize you with the steps you will go through in order to create a FORTRAN program in the UNIX environment. The text that follows the prompt ``%'' in most of the steps is what you should type.
Brief steps in this assignment:
Register your account: If you do not already have an itlabs account, the first thing to do is to register your account. Do this as soon as possible by going to https://wwws.cs.umn.edu/account-management/. If you have trouble with this please ask the class TA or lab attendant in one of the IT labs. It will probably take almost a full day for your account to be activated once you register it. So do not wait until right before the first lab is due!
When you register your account write down your login id and your password. Do not forget them. Do not tell anyone what your password is. If you have problems, see a lab consultant or your TA immediately. NOTE: when you type your password nothing will show up on the screen -- this is deliberate, your password is secret.
Login: To login from the UNIX or LINUX machines in the lab (4-250): just pick a machine that is open, and in the login box, type your login and your password. To login from home or elsewhere on campus: see the information at http://www.itlabs.umn.edu/help/offsite/it-offsite.html
After you have logged in successfully two windows will appear on the screen (except at home). By placing the mouse cursor in one of the windows you activate that window. The `%' is the UNIX prompt. UNIX is an operating system and the prompt is how you communicate with UNIX. One of the jobs of the operating system is to do what you tell it to do (if it can). When you type ``% a.out'' you are saying ``Hey! Operating System! Run this program for me.'' Or when you type ``% emacs lab1.f'' you are saying ``Hey! Run emacs and tell it to create/modify the file lab1.f.
Creating a file:
Use one of the text editors, like vi or emacs, to type in lab 1
(which is at the end of this file) by typing:
% pico lab1.f
or
% vi lab1.f
or
% emacs lab1.f
Here is a very short summary of some more commonly used emacs commands: Other infomation on the text editors is available online. There may also be handouts in the lab.
Some emacs commands:
To edit or create a file:
% emacs filename
To insert text: simply type the text.
To move around, delete text, etc.:
^n down one line
^p up one line
^v down one screen
^f forward one character
^b back one character
^a go to beginning of line
^e go to end of line
^d delete character
^k delete line
^x^c save and exit
^h help
Here ^h, for example, means hold down the control key and h key simultaneously.
Also, you can use the mouse to select useful commands (like cut and paste)
from the title bar.
It is up to you which of pico, vi, or emacs to learn, but it will probably be better to pick one and stick with it. Pico is fairly straightforward but not very powerful. Emacs and vi have more capabilities. Emacs is more user-friendly than vi. Type in lab 1 (listed at the end of this file) exactly as it appears below - even the errors, but change the personal information at the top to your own. Please type the code in rather than just copying it --- this will give you more practice with the text editor.
There are just a couple of things you need to know about FORTRAN right off the bat. One, FORTRAN is a column-oriented language, meaning that what column things appear in is very important. If a * or a c appears in column 1, then the whole line is considered a comment and is ignored by the compiler. All FORTRAN statements, the actual program, must appear between column 7 and column 72. Columns 2 -- 6 are reserved for special stuff we will see later. Two, FORTRAN is not case sensitive. That means it interprets upper case and lower case letters the same way. In the past, all FORTRAN statements were typed using upper case letters. Three, the program name, variable names, and anything else you get to give a name to can only consist of up to 6 letters or digits with the stipulation that the first character of the name must be a letter. So, In Lab 1 we have variables named INCOME, RATE, and TAXES which are all good names, but the names ABCDEF123 and 123ABC are both bad names.
Once you have finished typing in the program, save it and exit the
word processor, then type:
% f77 lab1.f
If you get an ``f77: Command not found'' message try typing:
% g77 lab1.f
If this gives you the same message see the lab attendant.
Errors: The f77 command tries to compile your program to an executable file. However, you should get two error messages (and maybe a warning message as well): The first error refers to the fact that on line 1 of your program you should have ``PROGRAM LAB1'' not ``PROGRAM LAB1.F'' --- the file extension .f should not be in the program statement. Also, on line 29, the print statement is missing an asterisk, it should be ``PRINT *'' . Usually the syntax messages are very cryptic. Your best bet is to just use the line numbers as a guide. One helpful hint is to fix the errors in the order that they appear. This helps because sometimes errors ``cascade'' - meaning one syntax error will cause multiple error messages.
To see, or view, your program on the screen page by page, use the more
command below. To view your program on the screen page by page with
line numbers, use the second command. You can do this any time you
need to. The second command has a vertical bar in it called a pipe.
% more lab1.f
% cat -n lab1.f | more
To print out a copy of just your program, use the lpr command below. To print out a copy of just your program with line numbers, use the second command. Both will print to the printer called ``cs4204'' which is in the lab in CS4204. You can do this any time you need to. This is useful for showing your TA when you have a problem. (Note: there is a printing charge, so please do not print out anything until you have read the information in the lab about the charge.)
% lpr -Pcs4204 lab1.f
% cat -n lab1.f | lpr -Pcs4204
Use your choice of editor to fix the syntax errors (ignore the warning
message for now --- just fix the errors on lines 1 and 29).
Then resave your program, exit the word processor, and recompile your
program:
% f77 lab1.f
It is important to compile your program after each change you make.
This time the compile should have worked (although the warning may still
be there). If you type:
% ls
you will see the contents of your directory: lab1.f, a.out.
Next, if you type:
% a.out
your program will run. It will print out:
Please enter your income.
Type in a number like 10000 (for $10,000) and hit return. When you type in 10000, type it as shown. Do not include any commas, etc.
Semantic Errors: You ran your program, but the answer you got was most likely not correct. Why? Look over the program again and see if you can identify the problem. Hint: the warning message might help.
The problem is an example of a semantic, or logic, error. Semantic errors are often more difficult to find than compiler errors. Some can be very subtle. If you got the compiler warning, this one might not be difficult to track down --- in line 26 the variable RESULT is used instead of TAXES. Note we did not declare result, but the compiler still compiled the program. Imagine how difficult a similar type of problem would be if there were no compiler warning (not all FORTRAN compilers will provide a warning on this), and the program was a lot longer!
One way to track down bugs is to put a lot of PRINT statements in your program. The PRINT statements will either print out a message such as ``I'm here'' or will print out the values of some variables. The first type of message will allow you to see how far and where your program got to before crashing. For example, if you see the message ``I'm here'' you will know your program got at least that far in its execution. Make sure all of these types of messages are unique. The second thing is to print out variables. This will help you determine if your equations are working or not. You can do the math on paper and determine whether or not your program is giving you the expected answer. This is basically what we are doing in lab 1.
Use the editor again to fix the semantic error. Recompile it and type:
% a.out
to run it again. Keep testing your program with different inputs to make
sure that it really works. The more testing you do the better off you
will be later.
Now, type the following sequence of commands:
% script
The ``script" command causes everything that you type and everything
that is written to the screen to be also written to a file named
typescript until you type ``exit."
% cat lab1.f
This puts a listing of your program in the typescript file. DO NOT USE
THE -N OPTION WITH CAT HERE!!
% f77 lab1.f
This will show us that your program compiles.
% a.out
Execute your program for the first case: 10000. It should
look like this:
Please enter your income :
10000
Taxes on $ 10000 are $ 1500
Execute your program for a second test case : 1000
% a.out
Execute your program for the third test case : 100. Note testing a program may involve running a.out several times.
% exit
Type ``exit" to end your script run. If you forget to type ``exit'' to stop the script, you will give yourself some big headaches.
% ls
The contents of your directory should now be: lab1.f, a.out and typescript.
When you are done type:
% logout
It is important that you logout before leaving. If you have any
trouble, talk to the lab consultant.
X Windows
X Windows, or just X, allows you to have multiple windows on the
screen, with each window having its own UNIX prompt. The window
concept is similar to a Mac, or Microsoft Windows; you can have any
number of windows, you can move them, kill them, etc. At the most
simple level, you can just do all of your work in one window and not
worry about the other one. One of the most common uses of two windows
is to have the editor running in one window and do everything else,
like running the compiler, in the other window. To make this work
successfully, though, you need to know one thing: After making changes
to your source code in the editor and before you go to the other
window to run the compiler, you need to save the changes you have
made, but not quit the editor. This can be done
in emacs by typing:
^x^s
Try this technique, it is really nice to have the source code in one
window and the compiler error messages in the other.
A short UNIX summary
To copy one file called "file1" and call the copy "file2":
% cp file1 file2
To rename a file called "file1" as "file2":
% mv file1 file2
To create a subdirectory called "dir1":
% mkdir dir1
To move to a subdirectory called "sub1":
% cd sub1
To move to your home directory:
% cd
To list the contents of the directory you are in:
% ls
To print a text file called "lab1.f" on the monitor:
% more lab1.f
or
% cat -n lab1.f | more
To print out the program "lab1.f" on the printer in the 4-204
lab (warning: there is a printing charge, so please do not print out
anything until you have read the information on the printing charge in
the lab.)
% lpr -Pcs4204 lab1.f
or
% cat -n lab1.f | lpr -Pcs4204
To print out the final typescript when you are satisfied with your
results:
% lpr -Pcs4204 typescript
To create or modify the file "lab1.f" with pico, vi, or emacs:
% pico lab1.f
or
% vi lab1.f
or
% emacs lab1.f
To run the mail program so that you can send mail, read any mail
you have, etc.
% Mail
Code for Lab 1:
PROGRAM LAB1.F ************************************************************************ * Name : Your name here * Login ID : your login @itlabs.umn.edu * * Program : lab1.f * Due Date : 09/12/2001 * Description : This program calculates a flat tax on an input income. * Variables : RATE - flat tax rate * INCOME - income input by user * TAXES - taxes owed ************************************************************************ INTEGER INCOME, TAXES REAL RATE ***** Set rate RATE = .15 ***** Prompt the user for input by using a PRINT and a READ PRINT *, 'Please enter your income :' READ *, INCOME ***** Calculate taxes owed RESULT = INCOME * RATE ***** Print out result PRINT PRINT *, 'Taxes on $', INCOME, ' are $', TAXES END
Because everyone needs to be familiar with this programming environment, each person must due this assignment alone - no teams for this lab!
Course Policies | Tentative Schedule | Help and References |
Assignments | Grades & Announcements | Home |