Document Sample

EASTERN MEDITERRANEAN UNIVERSITY Department of Mechanical Engineering CMPE 106 Fundamentals of Computing 1979 CHAPTER 6 INTRODUCTION TO FORTRAN 95 Pro 5.5 PRELIMINARY INFORMATION In the previous weeks, you have learned what the term programming means, why a computer needs the translation of a Fortran 95 Language Program (compiling & linking), etc. In the example Fortran programs given to you, you have seen many features of Fortran programming language, and you used Lahey FORTRAN 95 Pro. 5.5. Currently, Fortran 90 and Fortran 95 are the most widely used versions of the Fortran programming language. There are also some minor differences between the Fortran compilers developed by different companies (for example, Microsoft, and Lahey). In this course, the Standard Fortran Programming Language will be taught which is basically Fortran 95. This week you will start from the beginning and learn Fortran 95 Programming Language so that you can start writing your own programs. Please note that for being a good programmer, understanding the materials presented in tutorials is not enough. You should practice by trying all these on a computer, and obtain experience by yourself. Specifically in a Standard Fortran Program, a Fortran statement has the following rules: 1. Each statement can be written on 80 column (i.e. characters) line. 2. The actual Fortran statement can only be written within the columns 7 through 72 inclusive. 3. Column 6 is called the continuation column and is reserved for the following purpose. If a Fortran statement is too long for the above-mentioned 66 columns of the line, than it can be continued (within columns 7 through 72) on up to 19 additional lines. Any symbol (other than zero) written in column 6 tells the computer that the statement is a continuation of the last statement. 4. The statement can be labeled for reference, and such labels should be written within columns 1 through 5 of the labeled statement. The label is any unsigned positive integer number, and is called a statement number. 5. The computer ignores the content of column 73 through 80. SERKAN ABBASOGLU CMPE 106 Chapter 6 Page…1/8 6. Comment may be added to a program by writing a “!” or “c” in column 1 of a line. The comments itself may be written anywhere between column 2 and column 80. The comments are usually used to identify a program and for explanation. An example FORTRAN program: 1 67 Column ----------------------------------------------------------------------- ! This program calculates the area of a circle whose radius ! is given by the user PROGRAM CAREA PI = 3.14 PRINT*, ‘Please specify the radius of the circle: ’ READ*, Radius AREA = PI * RADIUS * RADIUS PRINT*, ‘The area is: ‘, AREA STOP END Reserved Words in FORTRAN: Words like PROGRAM, WRITE, READ, STOP, END, etc, are reserved words and have special meanings in FORTRAN. So, you cannot use them for any other purpose. For example they cannot be the name of your variables, etc. In the Lahey FORTRAN 95 , those reserved words will show up as yellow on the Lahey editor window. No Case Sensitivity: Fortran is not case sensitive, which means Fortran does not make any distinction between uppercase and lowercase letters (except in character and string constants, or values of variables). Area, area, AREA or aReA, for example, are all the same identifier. In the same way, Write, WRITE are all the same reserved word of Fortran language. The Assignment Statement: It is used to assign values to variables. The expression on the right of = is assigned to the variable on the left. The expression on the right may be a constant, another variable to which a value has previously been assigned, or a formula to be evaluated. If you write a very complex expression, using ( ) is a very good idea to clarify the order of operations that will be made. Examples: Radius = 5 XCoord = 4.27 AREA = PI * RADIUS * RADIUS X1 = (-b-sqrt(d)) / (2*a*c) Root1 = X1 X = Y+2 X = X+1 SERKAN ABBASOGLU CMPE 106 Chapter 6 Page…2/8 Sum = Sum+X Name = ‘Simona’ Letter = ‘A’ Old = false Arithmetic Operations – Integer and Real Arithmetic: The following Table shows the symbols for the five basic arithmetic operations of Fortran. Operator Operation Algebraic Fortran Expression Expression + Addition A + B A + B - Subtraction A - B A – B * Multiplication A B A * B / Division A/B A/B ** Exponentiation Ab A**B As there are two types of numbers, real and integer, so are there two types of arithmetic, real arithmetic, and integer arithmetic. These two types of arithmetic are performed differently, so it is very important to have clear understanding of the difference. Integer Arithmetic Operation If the operands (i.e. variables or numbers) are integer, then integer arithmetic is performed to yield an integer. For example, integer arithmetic is used to evaluate 5+3 , 5*3 , and 5-3 yielding the integers 8, 15, and 2, respectively. Integer division in Fortran, say evaluating I/J where J and I are integers, also yields an integer. That is, the fractional part of the result is deleted (truncated) in integer division. For example, 5/4 = 1 , 4/5 = 0 , -7/2 = -3 Real Arithmetic Operation If the operands (i.e. variables or numbers) are real, then real arithmetic is performed to yield a real value. Thus, real arithmetic is used to evaluate 5. + 3. , 5.*3. , and 5. – 3. yielding the real values 8., 15., and 2., respectively. Division by real arithmetic in Fortran is similar to ordinary division. That is, 5./4. = 1.25 , 4./5. = 0.8 , -7./2. = -3.5 The most important part f the above discussion is the fact that the type of operand determines the type of arithmetic (or the mode of operation), which then yields the same type of resultant. SERKAN ABBASOGLU CMPE 106 Chapter 6 Page…3/8 Mixed Arithmetic Operation When the computer evaluates an arithmetic expression, the type of the operands determines the type of arithmetic (i.e. mode of operation) that is used. If an expression has one operand that is real and the other integer, this is called mixed arithmetic (mode) operation. Most of compilers allow mixed-mode operation. In such case, whenever the operands of an operation are of two different types, i.e., one integer and the other real, the integer is first converted to real and then real arithmetic is performed to yield a real value. Evaluation of Arithmetic Expressions: You must understand how the computer evaluates arithmetic expressions in order to write them correctly. The computer evaluates a parenthesis-free expression (i.e. an expression without parentheses) using the following conventional precedence table: First precedence : Exponentiation (**) Second precedence : Multiplication (*) and Division (/) Third precedence : Addition (+) and Subtraction (-) Since there are three levels of priority, a computer scans a parenthesis-free arithmetic expression from left to right three times. The first time, it looks for exponentiation; the second time, it looks for multiplication and division; and finally, it looks for addition and subtraction. Since the computer scans from left to right, if two operations are of the same precedence, the leftmost one is performed first. Example: C 2 Arithmetic Expression: AB F D Fortran Expression : A*B + C/D * F**2 Build-In Mathematical Functions: The Fortran language has a library of mathematical functions that can be used by a programmer. Some of more common ones are listed in the following table. Mathematical Operation Mathematical Fortran Square root of x x SQRT(X) Absolute value of x |x| ABS(X) Exponential of x ex EXP(X) Sine of x Sin x SIN(X) Cosine of x Cos x COS(X) Log of x Log x ALOG10(X) Natural log of x Ln x ALOG(X) Truncate real x into integer IFIX(X) Convert integer to real FLOAT(I) SERKAN ABBASOGLU CMPE 106 Chapter 6 Page…4/8 Note : “x” used in these operations should be a real number. The Unformatted Input / Output Statements: To display the results on some output devices (ex: monitor’s screen) we need to use output statements, and to allow the user to enter new values during the program execution (run), we need to use input statements. In Fortran, there are two types of I/O statements: unformatted, and formatted. READ* and READ(*,*) are unformatted input statement, whereas PRINT* and WRITE(*,*) are unformatted output statements. Later on, the formatted I/O statements will be discussed. The following table shows how these statements are used, and give brief explanations to each. Statement Explanation Displays Enter radius on the screen and PRINT*, ‘Enter radius’ advances to the next line on the monitor screen. Displays Enter radius on the screen and WRITE(*,*) ‘Enter radius’ advances to the next line on the monitor screen. Displays the value of the variable AREA on the PRINT*, AREA screen (Ex: 18.25) PRINT*, ‘Area is ’, Area Displays: Area is 18.25 Waits for the user to enter the value of radius, READ*, Radius and after typing its value press ENTER key. Read(*,*) Radius Same As Above + advances to a new line Waits for the user to enter values for X and Y, READ*, X, Y respectively Formatted Input / Output Statements: Formatting provides a degree of control over the I/O functions that the simple list-directed commands to do. Formatting instructions must be used only with either input or output command, which it is often, called an I/O FORMAT pair. In general, the input pair will have the following structure (syntax): READ sl , variable1, variable2, …. sl FORMAT (specifier1, specifier2, ……, specifierN) READ (*, sl ) variable1, variable2, …. sl FORMAT (specifier1, specifier2, ……, specifierN) In a similar way, the output pair will look like this: PRINT sl , variable1, variable2, …. sl FORMAT (specifier1, specifier2, ……, specifierN) WRITE (*, sl ) variable1, variable2, …. sl FORMAT (specifier1, specifier2, ……, specifierN) where sl = The statement label SERKAN ABBASOGLU CMPE 106 Chapter 6 Page…5/8 specifier = instruction for individual variable Format specifiers fall into two main categories. The first contains the rules for controlling numerical and character data, while the second controls spacing functions. The general form of the data edit descriptor is TYPE width (.decimals) where TYPE = a letter width = total width of space desired decimals = total number of decimal digits Category Type Function Form Example Numerical I Integer Iw I5 F Real Fw.d F6.2 E Real (exponential) Ew.d E12.4 D Double Precision Dw.d D20.8 G Real (general) Gw.d G8.2 Character A Character variable Aw A20 ‘’ Character strings ‘xxx’ ‘example’ Spacing X Individual space rX 5X T Tab to column c Tc T20 TR Tab right s space TRs TR3 TL Tab left s space RLs TL5 / New line / / Repeat r( ) Repeat r times in ( ) r( ) 2(F6.2, I3) SERKAN ABBASOGLU CMPE 106 Chapter 6 Page…6/8 EXPERIMENTAL WORK 1. Type the program at page 2 that reads the value of radius from the keyboard, and displays the result (i.e. area) on the monitor screen. 2. Run this program, and see the result. Check the result whether correct or not to test the program. 3. Change PRINT* statement with WRITE(*,*), and READ* statement with READ(*,*), and save your new program. Then, compile your program, and run it. 4. Open a data file(named as “RESULT”) and save the area inside of it. 5. Write the flowchart and the program which is given below. ! This is a program that calculates the AVARAGE of mid term marks PROGRAM MIDTERM INTEGER TOT PRINT*, “ENTER STUDENT NUMBERS AND TOTAL STUDENT NUMBER” READ(*,*) ALI,MARY,FATIMA,SHARON,DAVID,TOT !it will read the exam results and no. of total ! persons and accept them as student names and (TOT) AV=(ALI+MARY+FATIMA+SHARON+DAVID)/TOT OPEN(UNIT=1,FILE="AVARAGE") ! open a file named as “AVARAGE” and label it as “1” WRITE (1,3) TOT ,AV !Write no. of students and result inside this file 3 FORMAT ("NO. OF. STU.",3X,I1,3X, "AVARAGE:",3X,F5.2) ! format these outputs CLOSE (1) !Close this file PRINT*, "AVARAGE:",AV !Write the result onto the screen also STOP END SERKAN ABBASOGLU CMPE 106 Chapter 6 Page…7/8 6. Write the flowchart and the program which is given below. ! This is a program that calculates the AVARAGE of mid term marks PROGRAM MIDTERM INTEGER TOT OPEN(UNIT=1,FILE="MARKS") !open a file named as “MARKS” and label it as “1” READ(1,*) ALI,MARY,FATIMA,SHARON,DAVID,TOT !it will read the exam results and no. of total ! persons and accept them as student names and (TOT) CLOSE(1) !Close this file AV= (ALI+MARY+FATIMA+SHARON+DAVID)/TOT OPENv(UNIT=1,FILE="AVARAGE") ! open a file named as “AVARAGE” and label it as “1” WRITE (1,3) TOT,AV !Write no. of students and result inside this file 3 FORMAT ("NO.OF. STU.", 3X, I1, 3X, "AVARAGE:", 3X, F5.2) ! format these outputs CLOSE (1) ! Close this file PRINT*, "AVARAGE:", AV !Write the result onto the screen also STOP END 7. Draw a flowchart and write a program that calculates the area of a triangle. The program should read two numbers (for example, B for base and H for height of triangle) from a file (Named as “DATA”) . Then, the program calculates the area (AREA=(B*H)/2.0) of the triangle. It should display the B, H, and AREA on the monitor screen and store these outputs inside a file(Named as “OUTPUT”) .Format all of the results on the screen and also in this file. SERKAN ABBASOGLU CMPE 106 Chapter 6 Page…8/8

DOCUMENT INFO

Shared By:

Categories:

Stats:

views: | 29 |

posted: | 5/26/2012 |

language: | |

pages: | 8 |

How are you planning on using Docstoc?
BUSINESS
PERSONAL

By registering with docstoc.com you agree to our
privacy policy and
terms of service, and to receive content and offer notifications.

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.