EDITING FORTRAN: Unlike in Basic, a Fortran program is not typed - TopicsExpress



          

EDITING FORTRAN: Unlike in Basic, a Fortran program is not typed in a Fortran window. Instead, a program is typed and saved with an editor (i.e., a word processor), and the program is then turned into an executable file by a Fortran compiler. To begin the process of creating a Fortran program in the math lab, you must open an editor. It is preferable to use a simple editor - such as Notepad or the DOS editor - because fancy word processors might add extraneous formatting notation that will hang up Fortran. A most peculiar feature of Fortran 77 is its line structure, which is a carryover from the old days when programs were typed on punch cards. A punch card had 80 columns, and so does a line of Fortran code. A c in column 1 indicates a comment (similar to REM in Basic). Columns 2-5 (usually left blank) are reserved for line numbers. Column 6 is used only to indicate a continuation of a line too long to fit on the card. Columns 7-72 contain the instructions of the program. Columns 73-80 were originally used for numbering the punch cards, but are rarely used nowadays - leave them blank and the compiler will ignore them. Fortran is case insensitive - that is, it does not distinguish between capital and small letters. Thus x and X refer to the same variable. Many programmers for simplicity use all small letters, but you may do as you like. Also, after column six Fortran does not recognize spaces (except for spaces inside quotations as in print statements). In general, spaces are mostly for the purpose of making code more readable by humans. When you type a Fortran program with an editor, make certain the editor indents more than six spaces; then if you begin every line with an indent you do not have to worry about counting six spaces at the beginnings of lines. Let us go through the steps of editing, compiling, and running a short program. First open Notepad under Windows, or type edit (and return) under a DOS prompt to open the DOS editor. (When you double-click the Fortran icon on a math lab computer, you get a DOS prompt.) Beginning each line with an indent (except for the fourth line, where the c must be placed in the first column), type the program exhibited below; the program computes the area of a circle of radius r, as input by the user. The resulting file that you save is called the source file for the program. program circlearea real r, area, pi parameter (pi = 3.14159) c This program computes the area of a circle. print *, What is the radius? read *, r area = pi * r ** 2 print *, The area is, area print *, Bye! end The first statement above gives the program name, the second declares that r, area, and pi will be single precision real quantities, and the third announces that pi has the value 3.14159. The fourth statement, beginning with c in column 1, is a comment describing what the program does; such comments are for the benefit of the programmer and are ignored by Fortran. The fifth statement prompts the user for the radius of the circle, and the sixth accepts this input. The seventh statement computes the area and the eighth informs the user of this area. Finally, the last two statements bid goodbye and terminate the program. The name for a source file in Fortran must end with the extension .f before the compiler recognizes it. After you have typed the above program, save the file as area.f. (If you type the file in Notepad, include the whole name in quotes when you save it, as otherwise the extension .txt will be added to the name.) The file will be saved to your h directory in the math lab. Under a DOS prompt you can view the files in this directory by typing dir and enter; under Windows you can double-click My Computer and then the icon for the h drive.
Posted on: Sun, 09 Mar 2014 10:06:07 +0000

Trending Topics



Recently Viewed Topics




© 2015