Elementary FORTRAN Elementary FORTRAN 77 z All FORTRAN
- Slides: 69
Elementary FORTRAN
Elementary FORTRAN 77 z. All FORTRAN programs consist of data that is manipulated by a set of control structures to produce a result. z. Control structures are statements that implement the algorithm steps you have chosen when you designed the program. z. Data + algorithms = programs
Command structure rules z. FORTRAN 77 (and earlier versions) had a fixed-column method of structuring commands. z. Later versions of FORTRAN allow freeformat. z. Since the fixed format is so common in FORTRAN we will start out writing our programs that way.
Column-based (fixed) structure (f 77) 1234567890123456789012345678901234567890 FORTRAN programs were originally punched on cards. Modern FORTRAN still supports the conventions that were used in that day. For example, every FORTRAN command must obey the following set of rules. Column 1: reserved for comment marks only. Valid comment marks are c, C or * in f 77, f 90 adds ! Columns 2 -5: reserved for statement labels. These are integers used to mark a line so that other statements can get back to it. They are labels, not line numbers. Column 6: reserved for a continuation mark (either a + or a single digit integer 1, 2, 3, 4. . etc. These indicate that the line is a continuation of the previous one. Columns 7 -72: Your executable FORTRAN statements go here Columns 73 -80: For line sequence numbers. Not used any more.
Column-based structure (f 77) 1234567890123456789012345678901234567890 c this is a comment line, comments start in column 1 c * Either a c, C or * may be used to indicate a comment c ************************************ INTEGER i PRINT *, “This long line continues on the next one. To indicate + this I place a + in column 6 (the continuation column)” DO 10 (i=1, 10) PRINT*, “Hello world!” 10 CONTINUE END
Elementary FORTRAN 77 z. All FORTRAN programs consist of data that is manipulated by a set of control structures to produce a result. z. Control structures are statements that implement the algorithm steps you have chosen when you designed the program. z. Data + algorithms = programs
Program structure z. First you should put in comments z. Then specify to the compiler what data items (variables) you program will need. y. Give each a name y. Tell what type of data it is y. Specify how many (if more than 1) z. Then perform the executable statements that act on the data to produce results.
Program structure Comments Specification (variable declaration) Execution c c c This is a demo program by me integer num print*, “Enter a number” read*, num print*, “The number you entered is: ” print*, num end
Basic data types z. These are the data types supported by standard FORTRAN yintegers yreal numbers ydouble precision (f 77 only) ycomplex (f 90 only) ycharacter ylogical
Integers z. NOT intergers! z. Integers consist of positive or negative whole numbers or 0 z…, -2, -1, 0, 1, 2, … z. Declared as z INTEGER num (in f 77 or f 90) z INTEGER : : num (in f 90, preferred)
Binary representation of integers 128 64 32 16 8 4 2 1 0 0 0 1 64 + 1 = 65 This is an 8 -bit byte most PCs use 32 bit words (4 bytes) Often the first bit is a sign bit.
Sign bits 128 64 32 16 8 4 2 1 0 0 0 1 The first bit may be used as a sign bit and therefore unavailable to represent integers. This cuts the capacity for representing large integers in half (from 256 to 128).
Real numbers z. Any number that might have a decimal point. z 3. 14159, 4. 0, -234. 56 z. If you enter a real number without a decimal point, one will be inserted automatically. (4 becomes 4. 0)
Binary representation of real numbers 1001010000100100111000001001011000100110000100100 exponent 7, 324, 645, 336 x 10 mantissa 31, 254, 355, 218
Scientific notation z. Often called exponential notation z 12345. 6789 becomes z 12. 3456789 E 3 (x 1000) z 1. 23456789 E 4 (x 10000) z 0. 123456789 E 5 (x 100000) z 123456789 E-4 (x 0. 0001)
For all numeric data z. DO NOT include punctuation in input y. Please enter a number y 1, 234 is incorrect y$1234 is also incorrect z. Real numbers can take integers but not vice versa. y. Please enter a real number y 1234 is OK y. Please enter an integer y 123. 45 is incorrect
Double precision z. Doubles the representational size of a real number. z. Not used under FORTRAN 90 but common under FORTRAN 77 for some applications. z. We will probably not need it.
Complex numbers z. Contain both a real and an imaginary part. z. This is not a standard data type in any other computer language. z. Not supported in f 77 z. More on this later.
Character data z. Much easier to handle in FORTRAN than in most other languages (Pascal, C, C++, etc. ) z. Valid characters are all standard ASCII and UNICODE characters z. Character strings are enclosed in “” z“This is a line” z. Above string has length of 14 (spaces count)
Special cases z. Apostrophes work the same as quotes y‘This is a sentence’ ybut you cannot mix them: y‘This is a sentence” z. How do you handle embedded apostrophes or quotes? y‘don’’t’ y“I said “”Hi””” yuse two sets to produce one character
Character declarations z. In FORTRAN 77 y. Character *10 name y. Character fname*10, lname*20 z. In FORTRAN 90 y. Character(10) : : name, lname*20
Logical data z. Logical means true or false z. We will use logical data later in the course and spend more time on it then.
Mixed types z. Some types can be mixed y. REAL num 1 y. INTEGER num 2 ynum 2 = 5 ynum 1 = num 2 z. Others cannot ycharacter*10 name ynum 1 = name
Variable declarations z. When a variable is declared you give the name and data type of the variable. z. The compiler figures out the size that will be required. z. If you use a variable in your program that you forgot to declare, the compiler has assigned it a type: integer or real z. This assignment may not be appropriate
Implicit data type rule z. Any variable that has not been declared and begins with the letters i through n automatically becomes an integer. z. Variables beginning with any other character automatically become real numbers.
Implicit data typing problem z. INTEGER sum, n z… program reads data, stores the count of how many in n and the total of them in sum zmean = sum / n z. Since mean was not declared it becomes an integer. This is probably not what you want here.
Uses for implicit data typing z. Loop control variables are variables that only exist to count the number of time a loop has executed. z. They should be integers z. A very common convention is to use the undeclared variables i, j, k, l, m and n for loop control variables because implicit typing makes them integers by default.
Turning off implicit typing z. Most programming languages regard implicit typing as dangerous. z. If it is turned off, then all variables must be explicitly declared by you. zf 77 does not allow you to turn this off but f 90 does. Like this… z. IMPLICIT NONE z. This command is placed at the top of the executable program statements.
The important points z. When a variable is declared, three things happen. y 1. Space to store your data is allocated in memory y 2. That space is assigned a data type y 3. That space is assigned a name
Memory allocation Memory cells REAL length, angle, period Variable declaration first must find available memory cells to store this data in. It then allocates them for the program and assigns your identifier names to them. 1345243 1345244 1345245 1345246 1345247 1345248 1345249 1345250 1345251 1345252 1345253 1345254 1345255
Memory allocation Memory cells REAL length, angle, period length angle period 1345243 1345244 1345245 1345246 1345247 1345248 1345249 1345250 1345251 1345252 1345253 1345254 1345255
Identifiers z. So, now that we are familiar with the built -in data types of this language, what do we do with them? z. We will solve problems using data items that are of these basic types. z. These items will be given names. z. In fact, we will have to give names to many things in our programs. z. Names are called identifiers
Naming conventions z. Identifiers ymust begin with a letter ycannot be longer than 31 characters (8 is the common standard practice in f 77) y. Only letters, digits or underscores (_) are allowed.
Valid and invalid identifiers z. Valid identifiers ynumber, s 1, name, speed_of_light z. Invalid identifiers y 1 stnum, soc-sec-num z. A good rule of thumb is to keep the identifier names short and concise. yname, employee, id_num (good) yemployee_name (too much typing? ) ythe_first_number_entered_by_the_user
Variable initialization z. Initialization refers to the act of assigning a value to a variable. z. Example: yinteger num ynum = 10 z. The = sign is called the ‘assignment operator’ z. Do not think of it as ‘equal to’. It really means assign the right-hand value to the left-hand variable
Constants z. A constant is a data item whose value never changes after initialization. z. Use the parameter statement zf 77 yparameter (pi = 3. 14159, g = 980) zf 90 yreal, parameter : : pi = 3. 14159, g = 980
Mathematical expressions z. FORTRAN comes from the phrase FORmula TRANslation z. Coding mathematical formulas is an important part of the language. z. Example: yy = a * x + b z. The expression on the right is evaluated and the result is assigned to the variable on the left.
Arithmetic operators z. In order to carry out mathematical expressions the computer must understand what operations to perform. z. This means it needs to know a set of arithmetic symbols, what operations they stand for and which ones should be done before others.
Arithmetic Operators z** exponentiation z* multiplication z/ division z z z+ addition z- subtraction a = 2**3 a=2/3 a = 2 / 3. 0 a = 2 / real(3) a=2+3 a=2 -3
Mixed mode expressions z. Mixed mode expressions combine more than one data type. z. Example: ya = 4 + 7 / 2 * 3 - 7 ya = 14/5. 0 + 4 z. Watch out for integer division! z. Group expressions using parentheses rather than guessing about what will happen.
Integer division z. An integer divided by an integer IS AN INTEGER zinteger a za = 14 / 3 z. PRINT*, a z. The output from this is 4 znot 4. 666667 as you might expect
Precedence (priority) rules z. Exponentiation is performed first. Multiple exponentiation is performed right to left. y. A = 2 ** 3 ** 2 is same as a = 2 ** 9 ya = 5 - 3 ** 3 a becomes -22 z. Multiplication and division are next. If more than one, go left to right. z. Addition and subtraction are last. If more than one, go left to right.
Examples z 2 + 4 ** 2 / 2 z 2 + 16 / 2 z 2 + 8 z 10
Example z 5 * 11 - 5 ** 2 * 4 + 9 z 5 * 11 - 25 * 4 + 9 z 55 100 + 9 z -45 +9 z -36
Example with parentheses z(5 * (11 - 5) ** 2) * 4 + 9 z(5 * (6) ** 2) * 4 + 9 z(5 * 36) * 4 + 9 z(180) * 4 + 9 z 720 + 9 z 729
Expression trees z 4 - 7 ** 2 / 4 * 3 + 2 49 12 36 -32 -30
Functions z. INT( arg ) z. NINT ( arg ) z. REAL ( arg ) z. ABS ( arg ) z. IABS ( arg ) z. SQRT ( arg ) integer - drops fraction integer - rounds arg converts to real absolute value - integer square root
Functions z. EXP (arg) z. ALOG 10 (arg) z. SIN (arg) z. COS (arg) z. TAN (arg) natural exponent e^arg natural logarithm arg in radians
Functions z. MOD (A 1, A 2) z. MAX 0 (A 1, . . . An) z. AMAX 1 (A 1, . . . An) z. MIN 0 (A 1, . . . An) z. AMIN 1 (A 1, . . . An) remainder of A 1/A 2 largest value - integer largest value - real smallest value - integer smallest value - real
Examples of function use z. Calculate the volume of an oblate spheroid. The formula is: 2 z. V = (4/3) a b z. Fortran version yparameter (pi = 3. 14159) yv = (4/real(3))*pi*a**2*b
Other examples z. Algorithm zif num goes into 100 evenly then … z. Fortran version zif (MOD(100, num). eq. 0) then
MOD z. MOD stands for modulo z. It is a mathematical term used to denote the integer remainder from integer division. z. In other words, mod takes two integer arguments. It then divides the first one by the second and finds the integer remainder. z. This integer remainder is what MOD sends back to the program.
MOD in action z. PRINT*, MOD(14, 3) zwill print the value 2 z 3 goes in to 14 four times with 2 left over z 2 is the integer remainder from integer division. 4 r 2 3 14 12 2
Examples continued za = SQRT(b) b cannot be negative zt = TAN(angle) angle must be in radians zi = INT(4. 7) sets i to 4 zi = NINT(4. 7) sets i to 5 z. Note: to use SIN, COS, TAN, you must convert the angle from degrees to radians. This is done by multiplying the angle by pi and then dividing by 180.
Integer division and MOD z. This example will construct a program to determine the smallest number of coins to dispense as change. Useful if you are a vending machine. z. Rules: yamount of change is always < $1. 00 yvalid coins are quarters, dimes, nickels, pennies
Alogorithm 1. 2. 3. 4. 5. 6. Declare variables Prompt for amount of change to give back Read amount Figure out how many quarters can be taken out of it Subtract the quarters from the amount Figure out how many dimes can be taken out of the remaining amount 7. Subtract the dimes from the amount 8. Figure out how many nickels can be taken out of the remaining amount 9. Subtract the nickels from it and all you have left are the number of pennies 10. Add up the number of quarters, dimes, nickels and pennies you took out to get the number of coins 11. Print out the number of coins. 12. END
Program example: Vending machine change c c This program figures out the fewest number of coins to dispense as change in a vending machine INTEGER amount, coins, numq, numd, numn, nump PRINT*, “How much change should you give? <$1” READ*, amount numq = amount / 25 amount = MOD(amount, 25) numd = amount / 10 amount = MOD(amount, 10) numn = amount / 5 nump = MOD(amount, 5) coins = numq + numd + numn + nump PRINT*, “The machine will dispense”, coins, “coins” END
Trace of vending machine program: statement numbering 1 2 3 4 5 6 7 8 9 10 11 12 INTEGER amount, coins, numq, numd, numn, nump PRINT*, “How much change should you give? <$1” READ*, amount numq = amount / 25 amount = MOD(amount, 25) numd = amount / 10 amount = MOD(amount, 10) numn = amount / 5 nump = MOD(amount, 5) coins = numq + numd + numn + nump PRINT*, “The machine will dispense”, coins, “coins” END
Trace of 99 cents 1 2 3 4 5 6 7 8 9 10 11 12 Declare vars. Prompt for amount Read amount Compute # of quarters Compute new amount Compute # of dimes Compute new amount Compute # of nickels Compute # of pennies Add up coins print results END amount coins numq numd numn nump ? ? ? 99 3 24 2 4 0 4 9
Assignment statements z. Assignment statements are the very heart of this, and all, computer languages. z. They assign a value to a variable z. Example: angle = angle * pi / 180
The assignment operator (=) z. Assigns the value on the right hand side of the = to the location on the left. z. Do not think of it as ‘equals’ z. Think of it as ‘assigns’ or ‘is set to’ z. Example: count = count + 1 z(see next slide)
The nature of assignment z. The rvalue is put into the left-hand location (a variable) z. UMDid = 2123456 UMDid 2123456
The nature of assignment z. DO NOT read = as ‘equals’ or you will encounter algebraic nonsense. z. Count = Count + 1 5 Count 6 = 5 + 1 6
Mixed mode assignments z. If an integer is assigned to a real variable it becomes a real z. If a real is assigned to an integer you will get an error message. z. Be careful to only assign data of the correct type
Input statements: READ z. The READ statement gets it’s input from the keyboard. z. Later in the semester we will learn how to read data from files. z. The format is z. READ*, var 1, var 2, var 3 z. The * simply means ‘use the default method of getting data from input
Output statements: PRINT z. To print a message on the screen use the PRINT statement. z. To write your output to a file use the WRITE statement (covered later in the semester) z. PRINT*, var 1, var 2, var 3 z. The *, indicates that you are printing to the default output device
PRINT considerations z. You may print the contents of a variable or character strings or both. z. PRINT*, “Please enter an integer” z. READ*, num z. PRINT*, “You entered”, num z. PRINT*, “num is: “, num, “cm”
Output control z. Unfortunately, right now we cannot control what happens with our output very well. z. Num is 4. 00000 z. Later we will learn how to control everything (like the number of decimal points, etc. )
Files z. Chapter 2, section 10 deals with files. z. We will not do this until much later in the course.
- Name three line segments
- “cramped holes” is a metaphor for the
- Is fortran case sensitive
- Fortran specialist
- Fortran language
- Fortran keras bridge
- Function in fortran
- Comentarios en fortran
- Coarray fortran tutorial
- Fortran card
- Fortran 1957
- Objektum orientált programozás
- The fortran optimizing compiler
- Mpi hello world fortran
- Fortran
- Fortran 90 tutorial
- Fortran data types
- Sejarah fortran
- Fortran
- Arreglos en fortran
- Linguaggio fortran
- Cobol pascal
- Megan damon
- Fortran programming examples
- Fortran alog10
- Fortran programlama dili
- What is fortran
- Fortran goto
- Fortran
- .gt. fortran
- Fortran car
- Coarray fortran tutorial
- Input/output statement in fortran
- Compaq visual fortran
- Fortran to c converter
- Fortran
- Implicit real
- Fortran programski jezik
- Like a rose trampled on the ground
- Arteria mediana cubiti
- John donne born
- I work all day i work all night
- Silent night holy night all is calm all is bright
- No power of hell
- Love all serve all help ever hurt never
- Basic communication operations in parallel computing
- You are my all in all images
- Above all powers above all kings
- Interventi sociali rivolti all'infanzia e all'adolescenza
- Sistem all in all out
- All of you is more than enough for all of me
- Birmingham city schools intersession
- Lantern lane elementary school
- Elementary my dear watson meaning
- Caughman road elementary school
- Algonkian elementary school
- Stanton elementary school
- Findley oaks elementary school
- Elementary statistics 6th edition
- Greenville elementary logan utah
- Rolling terrace es
- Jane dee hull elementary
- Vclassdojo
- Toujia elementary school
- Gulf elementary school
- Glenn york elementary
- Sunsangnim
- Riojas elementary
- Leila davis elementary school
- Hobbs williams elementary