Presentation on theme: "Fortran 1- Basics Chapters 1-2 in your Fortran book."— Presentation transcript:
Fortran 1- Basics Chapters 1-2 in your Fortran book
About Fortran There are two versions that are commonly used: FORTRAN 77 and FORTRAN 90. FORTRAN 90 is the standardized version used internationally. The Fortran programming language has some very specific structural requirements of our programs, as well as a specific syntax for most of the commands that we will be using in creating a program.
About Fortran Programs With the use of a text editor we create a source code: – will typically have an extension for (for example program1.for) – essentially a set of instructions in the Fortran programming language that we wish the computer to execute. – Although it may not appear to be English, source code is essentially a group of English expressions or commands that we understand. The problem however is that the computer only understands machine language. The machine language that the computer understands is a binary language composed of 0’s and 1’s. All the numbers and letters that we want the computer to understand are essentially converted into a binary expression that the computer can understand.
Compilers a compiler translates the (English) source code into machine code so that the computer understands the commands The actual steps in the compiling process involve compiling the source code to create object files, then linking the object files together to form a single executable file. The executable file is a single file that will execute the desired computer instructions when invoked. The executable file has an exe extension; for example: program1.exe.
Variables Most programs will require the use of variables to make the program applicable to solve general problems. The variables that are to be used may consist of either letter or numeric characters. Variables take the following form: 1 to 6 characters in length The first character of a name must be alphabetic Fortran does not distinguish between upper and lower case letters Examples: DISTANCE - no good, too long TIME- valid PI- valid $- no good, illegal character($) TAX-RT- no good, illegal character (-) B3- valid 2X-no good, first character numeric
Data Types There are six basics types: Integers- whole number (positive, negative or zero); 32, -7 Real Values- decimal numbers or exponential; 15.45, 0.004 Double-Precision Values- 3.145936536 Complex Values- 1-2i, 5i, Character Values- constants or strings; ‘velocity’, ‘Report 3’ Logical Values-.True.,.False.
Types of Variables When we define a variable we ask the computer to reserve a spot in memory for our particular variable. The type of variable that we specify can be specified by two methods: – implicit typing- determined by the first letter of the variable that we select. By default, Fortran sets variable names beginning with I, J, K, L, M, or N as integers. Therefore, any variable such as ‘IX’ or N2 are reserved as integers. If we tried to set N3=2.45, the Fortran program will save N3 =2. The decimal portion of the number will be truncated. Variable names beginning with the other letters A-H or O-Z will be stored as real values. – explicit typing - Fortran statements are used to specify a particular variable type: INTEGER area, width, height (not case sensitive) REAL I, length, mean (not case sensitive) These statements tell the compiler to assign memory locations, and to specify the types of values that will be stored in these locations.
Truncation and Mix-Mode Operations Truncation- when the computer stores a real number in an integer variable, it ignores the factional portion, or truncates it to a whole number. ex: I= 3 J=5 (I+J)/3 = 2|.6666 2 but, (I+J)/3.0 = 2.6666 ex: I**(1/2) 1 1/2 will truncate to 0 (0|.5) Mix-mode operations- operations involving both real numbers and integers ex:MASS = 2.519*WEIGHT This expression will first result in a real value from the multiplication and the truncation will take place when the value stored in the integer memory location for MASS. If WEIGHT = 100, the result from the multiplication is 251.9 and the value that would be stored in MASS would be 251 (the decimal portion will be truncated). Intermediate results on operations between integers and real values are real values. The final results depend on the variable names that are used to store the results.
Intrinsic Functions FORTRAN has a number of built-in operators that we can use for calculations. Some of these functions include: SQRT( number or expression) COS (angle in radians) ABS (number or expression) etc. **Note ACOS, ASIN, ATAN are all “ARC” functions or the Trigonometric functions
Simple Input and Output List-directed input: we can receive input directly from the keyboard with the READ statement : READ*, variable list The program will wait for the user to input the required values. The data values from the user can be separated by commas or blanks. After the data has been typed into the command line, the user will press the return key and the program will continue. In general, the READ statement will have to be preceded by an instruction that asks the user to put the data in. This can be simply conducted with the List-Directed Output that takes the similar form as the List-Directed Input statements: PRINT*, expression list Values in the expression list should be separated by commas. Statements can be included in the Print statement by enclosing the desired expressions in single quotation marks or apostrophes: PRINT*, ‘Please input the area and length separated by commas’ this may be followed by: READ*, AREA, ALGTH Or if we have a variable named AREA PRINT*, ‘The Area of the rod is ’, AREA This will output the character string “The Area of the rod is” followed by the value of the variable AREA.
Formatting Output PRINT k, expression list where k is the address of a format statement for the output(recall that this is an address place in columns 1 through 5). For Example: Print 5, AREA, ALGTH 5 FORMAT (5X, ‘The area of the rod is ’, F6.2,/, + 5X,‘The length of the rod is ’, F6.3) The “5X” in the above expression is just specifying to leave 5 blanks before our character string. The basic form of this is #X, where # is the number of blanks that we wish to leave. The F6.3 in the format statement is telling the compiler the desired format for the values of AREA - at most 6 digits with 2 points past the decimal. The “/” in the above expression specifies to start a new line.
Formatting Data Types TypeForm Real Integer Exponential Fw.d Iw Ew.d w- total width of number including the decimal point and any minus sign d- number of digits after decimal If the actual output is larger than the widths specified in the formatting statement, the output will be asterisks. It is very important to remember to include room for the decimal and a potential minus sign for negative values. All of the values will be right justified so that if we specify too large of a space this will result in blank spaces preceding our output.
Statement Format Each statement that we put in a fortran program is entered on a single line. As we talk about the format of a line we will discuss a particular position on the line relative to column numbers. The makeup of each line must take the following form: 1)Columns 1 -5 Statement number or label- not every line will have a statement number. In some cases however we will want to number a particular line so that we can refer to the particular line in the program. 2)Column 6 is reserved for a continuation character. If a particular statement is too long, it will have to be continued on the next line. By placing a character (such as +) in the sixth column we tell the compiler that the statement on this particular line is a continuation of the previous line. 3)Columns 7-72 Fortran statement. The only exception to the above layout is that if an asterisk *, or a letter C are placed in column 1, the particular line is a comment statement. It is highly encouraged to use liberal comment statements in order to organize the material in your program. We can include blank lines anywhere in the program to separate portions of the program.
Program Structure The form of a program has some very basic requirements: The first line of a program usually consists of a PROGRAM statement. The program statement assigns a name to the program and takes the following form: PROGRAM program name The program name can be one to six characters (alphabetic or numeric) and must begin with a letter. PROGRAM TEST PROGRAM LOAD1 It is usually good programming format to have some comment lines follow the program statement to describe what the program does.
Stop and End Statements STOP statement: signals the computer to terminate execution of the program. In some instances, you may wish to exit the program if certain conditions exist. This is accomplished with a STOP statement. There are no arguments associated with the STOP statement, it is the only command on the particular line. The STOP statement is optional at the end of the program. If one is not placed in the program, the compiler will automatically place a STOP command in the executable to stop the program. END Statement: identifies the end of the FORTRAN PROGRAM. There are no arguments. The basic form of the program is therefore as follows: PROGRAM ROAD Specification Statements Executable Statements END Statement