elementary fortran. elementary fortran 77 zall fortran programs consist of data that is manipulated...

Download Elementary FORTRAN. Elementary FORTRAN 77 zAll FORTRAN programs consist of data that is manipulated by a set of control structures to produce a result

Post on 29-Dec-2015

215 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

  • Elementary FORTRAN

  • Elementary FORTRAN 77All FORTRAN programs consist of data that is manipulated by a set of control structures to produce a result.Control structures are statements that implement the algorithm steps you have chosen when you designed the program.Data + algorithms = programs

  • Command structure rulesFORTRAN 77 (and earlier versions) had a fixed-column method of structuring commands.Later versions of FORTRAN allow free-format.Since the fixed format is so common in FORTRAN we will start out writing our programs that way.

  • Column-based (fixed) structure (f77)12345678901234567890123456789012345678901234567890123456789012345678901234567890FORTRAN programs were originally punched oncards. Modern FORTRAN still supports the conventionsthat were used in that day. For example, every FORTRANcommand must obey the following set of rules.

    Column 1: reserved for comment marks only. Valid comment marks are c, C or * in f77, f90 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 hereColumns 73-80: For line sequence numbers. Not used any more.

  • Column-based structure (f77)12345678901234567890123456789012345678901234567890123456789012345678901234567890c this is a comment line, comments start in column 1c* Either a c, C or * may be used to indicate a commentc*********************************************************************** 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 77All FORTRAN programs consist of data that is manipulated by a set of control structures to produce a result.Control structures are statements that implement the algorithm steps you have chosen when you designed the program.Data + algorithms = programs

  • Program structureFirst you should put in commentsThen specify to the compiler what data items (variables) you program will need.Give each a nameTell what type of data it isSpecify how many (if more than 1)Then perform the executable statements that act on the data to produce results.

  • Program structureCommentsSpecification(variable declaration)Executionc This is a demo programc by mec

    integer num

    print*, Enter a number read*, num print*, The number you entered is: print*, num end

  • Basic data typesThese are the data types supported by standard FORTRANintegersreal numbersdouble precision (f77 only)complex (f90 only)characterlogical

  • IntegersNOT intergers!Integers consist of positive or negative whole numbers or 0,-2, -1, 0, 1, 2, Declared as INTEGER num (in f77 or f90) INTEGER :: num (in f90, preferred)

  • Binary representation of integers 0 1 0 0 0 0 0 1128 64 32 16 8 4 2 164 + 1 = 65

    This is an 8-bit bytemost PCs use 32 bit words (4 bytes)Often the first bit is a sign bit.

  • Sign bits 0 1 0 0 0 0 0 1128 64 32 16 8 4 2 1The first bit may be used as a sign bit andtherefore unavailable to represent integers.This cuts the capacity for representing largeintegers in half (from 256 to 128).

  • Real numbersAny number that might have a decimal point.3.14159, 4.0, -234.56If you enter a real number without a decimal point, one will be inserted automatically. (4 becomes 4.0)

  • Binary representation of real numbers10010100010000100100111000001001000101101011000100110000100100exponentmantissa7,324,645,336 x 1031,254,355,218

  • Scientific notationOften called exponential notation12345.6789 becomes12.3456789E3 (x 1000)1.23456789E4 (x 10000)0.123456789E5 (x 100000)123456789E-4 (x 0.0001)

  • For all numeric dataDO NOT include punctuation in inputPlease enter a number1,234 is incorrect$1234 is also incorrectReal numbers can take integers but not vice versa.Please enter a real number1234 is OKPlease enter an integer123.45 is incorrect

  • Double precisionDoubles the representational size of a real number.Not used under FORTRAN 90 but common under FORTRAN 77 for some applications.We will probably not need it.

  • Complex numbersContain both a real and an imaginary part.This is not a standard data type in any other computer language.Not supported in f77More on this later.

  • Character dataMuch easier to handle in FORTRAN than in most other languages (Pascal, C, C++, etc.)Valid characters are all standard ASCII and UNICODE charactersCharacter strings are enclosed in This is a lineAbove string has length of 14 (spaces count)

  • Special casesApostrophes work the same as quotesThis is a sentencebut you cannot mix them:This is a sentenceHow do you handle embedded apostrophes or quotes?dontI said Hiuse two sets to produce one character

  • Character declarationsIn FORTRAN 77Character *10 nameCharacter fname*10, lname*20In FORTRAN 90Character(10) :: nameCharacter(10) :: name, lname*20

  • Logical dataLogical means true or falseWe will use logical data later in the course and spend more time on it then.

  • Mixed typesSome types can be mixedREAL num1INTEGER num2num2 = 5num1 = num2Others cannotcharacter*10 namenum1 = name

  • Variable declarationsWhen a variable is declared you give the name and data type of the variable.The compiler figures out the size that will be required.If you use a variable in your program that you forgot to declare, the compiler has assigned it a type: integer or realThis assignment may not be appropriate

  • Implicit data type ruleAny variable that has not been declared and begins with the letters i through n automatically becomes an integer.Variables beginning with any other character automatically become real numbers.

  • Implicit data typing problemINTEGER sum, n program reads data, stores the count of how many in n and the total of them in summean = sum / nSince mean was not declared it becomes an integer. This is probably not what you want here.

  • Uses for implicit data typingLoop control variables are variables that only exist to count the number of time a loop has executed.They should be integersA 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 typingMost programming languages regard implicit typing as dangerous.If it is turned off, then all variables must be explicitly declared by you.f77 does not allow you to turn this off but f90 does. Like thisIMPLICIT NONEThis command is placed at the top of the executable program statements.

  • The important pointsWhen a variable is declared, three things happen.1. Space to store your data is allocated in memory2. That space is assigned a data type3. That space is assigned a name

  • Memory allocation Memory cellsREAL length, angle, period

    Variable declaration first mustfind available memory cells to storethis data in.

    It then allocates them for theprogram and assigns your identifier names to them.1345243134524413452451345246134524713452481345249134525013452511345252134525313452541345255

  • Memory allocation Memory cellsREAL length, angle, period1345243134524413452451345246134524713452481345249134525013452511345252134525313452541345255lengthangleperiod

  • IdentifiersSo, now that we are familiar with the built-in data types of this language, what do we do with them?We will solve problems using data items that are of these basic types.These items will be given names.In fact, we will have to give names to many things in our programs.Names are called identifiers

  • Naming conventionsIdentifiersmust begin with a lettercannot be longer than 31 characters (8 is the common standard practice in f77)Only letters, digits or underscores (_) are allowed.

  • Valid and invalid identifiersValid identifiersnumber, s1, name, speed_of_lightInvalid identifiers1stnum, soc-sec-numA good rule of thumb is to keep the identifier names short and concise.name, employee, id_num (good)employee_name (too much typing?)the_first_number_entered_by_the_user

  • Variable initializationInitialization refers to the act of assigning a value to a variable.Example:integer numnum = 10The = sign is called the assignment operatorDo not think of it as equal to. It really means assign the right-hand value to the left-hand variable

  • ConstantsA constant is a data item whose value never changes after initialization.Use the parameter statementf77parameter (pi = 3.14159, g = 980)f90real, parameter :: pi = 3.14159, g = 980

  • Mathematical expressionsFORTRAN comes from the phrase FORmula TRANslationCoding mathematical formulas is an important part of the language.Example:y = a * x + bThe expression on the right is evaluated and the result is assigned to the variable on the left.

  • Arithmetic operatorsIn order to carry out mathematical expressions the computer must understand what operations to perform.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** exponentiation a = 2**3* multiplication a = 2 * 3/ division a = 2