Lecture 2 FORTRAN Basics. Lubna Ahmed

Similar documents
FORTRAN Basis. PROGRAM LAYOUT PROGRAM program name IMPLICIT NONE [declaration statements] [executable statements] END PROGRAM [program name]

Program Structure and Format

ME1107 Computing Y Yan.

Our Strategy for Learning Fortran 90

Data Types and Basic Calculation

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

Computer Programming in MATLAB

Fortran 90 Basics. Fall I don t know what the programming language of the year 2000 will look like, but I know it will be called FORTRAN.

CGF Lecture 2 Numbers

Dr Richard Greenaway

Built-in Types of Data

Choose the correct answer below. 2. Convert the angle to a decimal in degrees.

Language Fundamentals

Graphing Calculator Scientific Calculator Version 2.0

DEPARTMENT OF MATHS, MJ COLLEGE

Introduction to FORTRAN

C++ Programming: From Problem Analysis to Program Design, Third Edition

10 Using the PCFL Editor In this chapter

The Very Basics of the R Interpreter

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Introduction to Programming

Computing Fundamentals

Excel Tool: Calculations with Data Sets

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Introduction to TURING

Long (or LONGMATH ) floating-point (or integer) variables (length up to 1 million, limited by machine memory, range: approx. ±10 1,000,000.

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

2 Making Decisions. Store the value 3 in memory location y

Computational Physics

1001ICT Introduction To Programming Lecture Notes

9. Elementary Algebraic and Transcendental Scalar Functions

CHAPTER 3: CORE PROGRAMMING ELEMENTS

C Functions. 5.2 Program Modules in C

Pseudocode is an abbreviated version of the actual statement t t (or code ) in the program.

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

BC An Arbitrary Precision Desk-Calculator Language. Lorinda Cherry Robert Morris Bell Laboratories Murray Hill, New Jersey ABSTRACT

Downloaded from Chapter 2. Functions

Full file at C How to Program, 6/e Multiple Choice Test Bank

CS-201 Introduction to Programming with Java

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Full file at

Computer Programming CS F111

SCIENTIFIC CALCULATOR OPERATION GUIDE < EL-531TG/531TH/531TS >

Basic types and definitions. Chapter 3 of Thompson

Goals for This Lecture:

ELEMENTARY MATLAB PROGRAMMING

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Creating a C++ Program

Chapter 2. Outline. Simple C++ Programs

Scheme Quick Reference

Introduction to Programming with RAPTOR

VARIABLES Storing numbers:

EqualsEquals Language Reference Manual

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

String Computation Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

(Type your answer in radians. Round to the nearest hundredth as needed.)

Programming Language 2 (PL2)

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

UNIT- 3 Introduction to C++

Chapter 3 - Functions

The C++ Language. Arizona State University 1

Objectives. In this chapter, you will:

ANSI C Programming Simple Programs

Dr Richard Greenaway

MATLAB Constants, Variables & Expression. 9/12/2015 By: Nafees Ahmed

Introduction to MATLAB

LAB 1 General MATLAB Information 1

Algebra 1 Review. Properties of Real Numbers. Algebraic Expressions

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

JME Language Reference Manual

VBScript: Math Functions

Scheme Quick Reference

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

9 Using Equation Networks

17 USING THE EDITOR AND CREATING PROGRAMS AND FUNCTIONS

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

SML A F unctional Functional Language Language Lecture 19

The following information is for reviewing the material since Exam 3:

Chapter 2, Part III Arithmetic Operators and Decision Making

Java Programming Fundamentals. Visit for more.

Fortran 90 Two Commonly Used Statements

Chapter 2: Introduction to C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

Programming in QBasic

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Fundamental of Programming (C)

Intrinsic Functions Outline

CSCE 110 PROGRAMMING FUNDAMENTALS

Operators and Expressions:

Mirage. Language Reference Manual. Image drawn using Mirage 1.1. Columbia University COMS W4115 Programming Languages and Translators Fall 2006

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Transcription:

Lecture 2 FORTRAN Basics Lubna Ahmed 1

Fortran basics Data types Constants Variables Identifiers Arithmetic expression Intrinsic functions Input-output 2

Program layout PROGRAM program name IMPLICIT NONE Declaration statements(data defining) Executable statements (data processing) Data input Calculation Data output END PROGRAM program name Example 3

Statements Form the basis of any Fortran program, and may contain from 0 to 132 characters. All statements begins with a keyword except assignment statement. INTEGER, REAL, LOGICAL, READ, WRITE, PRINT, OPEN etc are some keywords. Generally, there will be one statement per line. However, multiple statements may appear on a line if they are separated by semi-colons but not recommended. A = 1; B = 1; C = 1 Comments Any characters following an exclamation mark (! ) are commentary, and are ignored by the compiler. Significance of Blank Blanks are generally not significant, i.e. you can use them to improve readability. 4

FORTRAN Alphabets Fortran only uses the following character: Letters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h I j k l m n o p q r s t u v w x y z Digits: 0 1 2 3 4 5 6 7 8 9 Special Character: space () * + - / : = _! & $ ; < > %?,. Continuation of line If a statement is too long to fit on a line, it will be continued on the next line if the last non-blank character in it is an ampersand (&): A = 174.6 * &(T - 1981.2) ** 3 & The above is equivalent to A=174.6*(T-1981.2)**3 5

Data types Data type Integer Characteristic Whole numbers stored exactly Real Double precision Numbers, which may have fractional parts, with limited precision Similar to real but with greater precision Complex Stored as an ordered pair of real numbers Logical A Boolean value: either true or false Character A string of characters of fixed length 6

Data types The data that are handled by a program fall into two main types: - Constants: Constants do not change during the execution of FORTRAN program. - Variables : in which the program will store its input, output, constants and intermediate results, may change value during execution of program. - There are five intrinsic or built-in types of FORTRAN constants and variables. Three of them are of numeric (types INTEGER, REAL and COMPLEX),one is logical(type logical) and one consists of string of character(type CHARACTER). 7

Constants Constant are the token used to denote the value of a particular type. Values of constants do not change during program execution. Integer constant An integer constant is any number that does not contain a decimal point. The general form is a sign (plus or minus) followed by a string of one or more digits. All other characters are prohibited. If the number is positive the plus sign is optional. Examples of valid integer constants: 0 137-2516 +17 Some invalid integers: 5,28,000 (embedded commas are illegal) 16.0 (If it has a decimal point, it is not an integer constant) - - 5 8

Constants cont d Real Constant There are two representation 1. Decimal representation:- A decimal point must be present, but no commas are allowed, can have an optional sign. Correct Examples : 23.45.12 123. -0.12 -.12 Incorrect Examples : 1,435.95 75 123.5- $12.34 2. Exponential representation:- It consists of an integer or a real number in decimal representation, followed by exponent. Correct Examples: 12.3456E2 or 12.3456e2: equal to 1234.56-3.14e1 or -3.14e1 : equals to -31.4-1.2E-3 or -1.2e-3 : equals to -0.0012 12E3 or 12e3 : equals to 12000.0 0E0 or 0e0 : equals to 0.0 Incorrect Examples: 12.34E1.2 : the exponential part must be an integer constant 12.34-5 : there is no exponential sign E or e 9

Constants cont d Complex constants A complex constant has the form of two real or integer constants separated by a comma enclosed in a pair of parentheses, e.g. (A,B) for A+iB. Examples: (3.14,-5.67) (0,0) (-0.999,2.718E15) Logical constants There are only two possible logical constants. They are expressed as.true. and.false. Example: X.GT.Y is.true. (if X is greater than Y) X.GT.Y is.false. (if X is not greater than Y) 10

Constants cont d Character String A character constant consists of a string of characters enclosed in a pair of apostrophes which act as quotation marks.. Correct Examples: John or John : content = John and length = 4 or : content = a single space and length = 1 John Dow#2 or John Dow#2 : content = John Dow#2 and length = 10 or : content = nothing and length = 0 (empty string) Incorrect Examples: you and me : the closing apostrophes is missing. Hello, world : the opening apostrophes is missing. Hi or Hi : the opening and closing quotes do not match. Note: If single quote is used in a string, the double quotes should be used to enclosed the string: John s pen 11

Implicit Type Rule First letter of the name A to H I to N O to Z Implicit type Real Integer Real This (unhelpful) rule still applies in Fortran 90 by default,to ensure compatibility of code written under earlier versions. Example:- Interest rate = 0.12 program where Interest rate is not declared explicitly will assign the value 0 to the variable. To guard against such errors it is strongly recommended that the statement IMPLICIT NONE be used at the start of all programs. 12

Variables In mathematics, a symbolic name is often used to refer a quantity. For example, the formula: A = l.w is used to calculate the are (A) of a rectangle with a given length (l) and a given width (w). These symbolic names, A, l, w, are called variables. And the same thing can be said about the variables in Fortran. The type of a FORTRAN variable must be one of the six data types mentioned before. The type of each variables determines the type of value that may be assigned to that variable. 13

Variables cont d A variable is a memory location whose value may be changed during execution of a program. A variable's name is constructed in a similar way as the memory name. A variable has a type which determines the type of constant it may hold. It is given a type in a type declaration. INTEGER X REAL INTEREST CHARACTER REAL :: A = 1 LETTER Here, X, INTEREST, LETTER are undefined variable and A is defined variable. Double colon (::) is used to initialize a variable. 14

Variables cont d program vertical! Vertical motion under gravity real :: g! acceleration due to gravity real :: s! displacement real :: t! time real :: u! initial speed ( m / s)! set values of variables g = 9.8 t = 6.0 u = 60! calculate displacement s = u * t - g * (t**2) / 2! output results write(*,*) Time =,t, Displacement =,s end program vertical There are four variables g, s, t, u 15

Variables cont d Some rules for FORTRAN variable name: 1. 1 to 31 characters in length. 2. May use A-Z, a-z, 0-9 and space. Upper and lower cases are equivalent. For example therefore the variables T and t, and the program names vertical and Vertical are identical. 3. First character of a name must be alphabet A-Z, a-z. For example: 3DYR is an incorrect variable. 4. Don t use keywords or library functions as a variable name. 5. If a variable name is undeclared in the program it is assumed a numerical variable by the compiler and if first character of the name is I, J, K, L, M or N, then the variable will be an integer variable and if the first character is any of A-H, O-Z then it will be real variable. 6. Always use meaningful variable. 16

Character Variable and Declaration Since a character string has a length attribute, a length value must be attached to character variable declarations. There are two ways of to do this: CHARACTER (LEN= 15) :: name, street CHARACTER(15): : name, street If a variable can only hold a single character, the length part can be removed. CHARACTER(LEN =1) :: letter, digit CHARACTER(1) :: letter, digit CHARACTER :: letter, digit If you want to declare character variables of different length with a single statement, then CHARACTER(LEN = 10) :: city, nation*20, box, digit*1 If character variable is declared in the following way CHARACTER(LEN= *) :: city, nation Then, the actual length of these character variables city and nation are unknown and will be determined elsewhere. 17

Parameter Attribute In some cases, one wants to assign a name to a particular value. For example: value of Pi = 3.1415926 It is tedious to write the value of Pi in every time in a program. To avoid this parameter attribute is used. REAL, PARAMETER :: E=2.71828, PI = 3.1415926 CHARACTER (LEN = 4), PARAMETER :: Name = John, City = LA 18

Arithmetic Operators Fortran has four types operators: Arithmetic, Rational, Logical, Character. Types Operator Associability Arithmetic ** Right to left Rational < < = * / Left to right + - Left to right > > = = = /= None Logical.NOT. Right to left.and..or. Left to right Left to right.eqv..neqv. Left to right 19

Arithmetic Operators cont d The order of evaluation of an expression: Sub-expressions in parenthesis Function reference Exponentiation, i.e., raising to a power Multiplication and division Addition and subtraction Within each of these groups evaluation proceeds from left to right, except that exponentiations are evaluated from right to left. Therefore X**Y**Z is equivalent to X**(Y**Z) 20

Arithmetic Operators cont d Incorrect Problem Correct A** -B Consecutive operators A**(-B) A(B+ 3.6) Missing operator A*(B+ 3.6) A* 2.75 B*(C+ D)) Unpaired brackets A* (2.75 B*(C+ D)) 21

Arithmetic Operators cont d Example Variables a, b, c, d, e, f, and g have been initialized to the following values: a= 3. b= 2. c= 5. d=4. e=10. f=2. g=3. Evaluate the following FORTRAN assignment statement: Output = a* b+ c*d+ e /f**g Solution: Fill in the numbers : output = 3.* 2. + 5. * 4. +10. / 2. ** 3. Evaluate 2. ** 3. : output = 3.* 2. + 5. * 4. +10. / 8. Evaluate multiplications and divisions from left to right: Output = 6. + 5. *4. + 10. / 8. Output = 6. + 20. + 1.25 Evaluate additions: output = 27.25 22

Integer Division Integer division always produces a result which is another integer value. Any fractional part is truncated, i.e., rounded towards zero. Example: 8/3 = 2 This makes it especially important to provide a decimal point at the end of a real constant even if the fractional part is zero. Mixed Mode Expression Fortran 90 allows operand in an expression to be of different type. The general rule is that the weaker or simpler type is converted into the stronger type. Since integer type is the simplest, this means that operations involving real and integer operand will be done in real arithmetic. Example: 1.0/4 1.0/4.0 0.25 3.0+8/5 3.0+1 3.0+1.0 4.0 3+8.0/5 3+8.0/5.0 3+1.6 3.0+1.6 4.6 3/4*5.0 0*5.0 0 5.0*3/4 5.0*3.0/4 15.0/4 15.0/4.0 3.75 23

Arithmetic Intrinsic Function Trigonometric functions: SIN (x) sine of x radians COS (x) cosine of x radians TAN (x) tangent of x radians ASIN (x) arc sine of x COS (x) arc cosine of x ATAN (x) arc tangent of x Transcendental functions SQRT (x) square root LOG (x) natural logarithm EXP (x) returns the exponential LOG10 (x) logarithm to base 10 Others MIN (x 1, x 2, ) MAX (x 1, x 2,...) ABS (x) MOD (x 1, x 2 ) 24

Input- Output Fortran provides two types of input /output statements: Formatted input/output List-directed input/output List-directed output PRINT*, output-list Or, WRITE(*,*), output-list List-directed input READ*, input-list 25

Input- Output Example:- PROGRAM INPUT_OUTPUT IMPLICIT NONE REAL X, Y,SUM! Variable declaration WRITE(*,*), GIVE THE VALUE OF X AND Y READ*, X,Y SUM = X+Y! Executable statement WRIRE(*,*), SUMMATION OF GIVEN NUMBER:, SUM END PROGRAM INPUT_OUTPUT 26

27

Assignment 1. An orifice meter is used to measure the flow rate in pipes. The flow rate is related to the pressure drop by an equation of the form: where u = fluid velocity Δp = pressure drop ρ = density of the flowing fluid c = constant of proportionality Write a computer program that reads pressure drop and density and calculates the fluid velocity. A constant of proportionality must be defined and used also. 28