Presentation is loading. Please wait.

Presentation is loading. Please wait.

FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.

Similar presentations


Presentation on theme: "FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language."— Presentation transcript:

1 FORTRAN

2  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.  FORTRAN 66  First HLL Standard  FORTRAN 77  Character data types  Improved Do Loops and If statements  Fortran 90  Free format source code  Modern control structures  Derived Data Types  Powerful array notation  Dynamic Memory allocation  Operator overloading  Keyword argument passing  The INTENT attribute  Control of numeric precision  Support for Modules  Fortran 95  minor improvements  Fortran 2003  Object oriented Fortran  Fortran 2008  Concurrent Programming  Fortran 2015  Minor improvements BACKGROUND Programming Languages Family Tree

3  You will want to be comfortable using an text-editor like vi or emacs – that supports syntax highlighting and auto-indentation etc... There is an ide called Photran that I have never used.  I prefer emacs but both vi and emacs have similar functionality.  I usually run emacs in the terminal (fast!)  emacs program.f90 –nw  Important emacs commands to remember  ctrl-x ctrl-f (find a file)  ctrl-x ctrl-s (save current file)  ctrl-x ctrl-c (close emacs)  ctrl-s (search)  ctrl-x 2 (split window horizontally)  ctrl-x 3 (split window vertically)  ctrl-x 0 (close current window)  ctrl-x o (switch to next window)  ctrl-x k (kill current file)  ctrl-x b (switch to next file  alt-% (search and replace)  ctrl-a (start of line)  ctrl-e (end of line)  Also good to remember 'ctrl-z' to suspend emacs and 'fg' to resume DEVELOPMENT ENVIRONMENT

4  Open a terminal in mac or linux  Putty or MobaxTerm in windows  ssh @bluehive.circ.rochester.edu  mkdir fortran_class  cd fortran_class  emacs hello_world.f90 CONNECTING TO BLUEHIVE

5  I will use intel compiler although gfortran compiler will also work.  module load intel  ifort hello_world.f90 ./a.out COMPILING

6  /public/jcarrol5/fortran/ex1.f90  Program structure  Comments (! This is a comment)  Continuation & lines  Variable Declarations  Data Types  INTEGER  REAL  CHARACTER  LOGICAL  Operators  Arithmetic: +, -, /, *, **  CHARACTER concatenation: //  Relational: >, =, <=, /=, ==  Logical:.AND.,.OR.,.XOR.,.NOT.,.EQV.,.NEQV.  Use () to group expressions  Intrinsic numerical functions  MOD, FLOOR, CEILING, SIGN, MAX, MIN  Intrinsic math functions  SQRT, LOG, LOG10, EXP, SIN, COS, TAN, SINH, COSH, TANH, ASIN, ACOS, ATAN  Basic I/O  READ, WRITE FORTRAN BASICS

7  Exercise 1: Write a program that uses Heron's formula to calculate the area of a triangle given the length of its three sides. EXERCISE 1

8  /public/jcarrol5/fortran/ex1b.f90  Format specifiers.  write(*,'(A10,2F12.5,I3,L1)') string1, float1, float2, int1, logical1  I – integer  A – string  F – float  E – exponential  EN – scientific notation  ES – engineering notation  All format specifiers can be modified with a repeat count and width. In addition, the precision can be specified for Format specifiers for real numbers.  4F10.5 means use a floating point format of width 10 and 5 decimal points 4 times. FORMAT SPECIFIERS

9  /public/jcarrol5/fortran/ex2.f90  Fortran programs can contain subroutines and functions.  Functions return a value  Subroutines do not  Fortran normally passes variables by reference – so be careful when modifying function or subroutine arguments. Best to declare the intent to avoid common mistakes.  If a constant or an expression is passed as an argument, a temporary variable is created to store the result of the expression before being passed to the subroutine or function. SUBROUTINES AND FUNCTIONS

10  Modify previous program to use a function to calculate the area of the triangle.  Use format specifiers for the output. EXERCISE 2

11  /public/jcarrol5/fortran/ex3.f90  Fortran has 3 basic control structures  DO loops  With a counter (an optional increment)  DO counter=start, end[, increment] ...  END DO  With a while statement  DO WHILE (a < b) ...  END DO  With an exit statement  DO ...  IF (condition) EXIT  END DO  IF blocks  IF (condition) THEN ...  ELSE IF (other condition) THEN ...  ELSE ...  END IF  SELECT CASE statements  SELECT CASE (var)  CASE(constant1)  execution statements  CASE(constant2)  execution statements  CASE DEFAULT  execution statements  END SELECT CONTROL STRUCTURES

12  Write a Fortran program to calculate the number of uniq combinations of size r taken from a set of size n – ie 'n choose r'.  Validate that n and r are both non-negative and that n is greater than or equal to r.  Write a factorial function  Write a combination function that calls the factorial function  Format the output EXERCISE 3

13  /public/jcarrol5/fortran/ex4.f90  Often a subroutine or function may want to call itself. This is called recursion.  Recursive subroutines and functions require the recursive keyword.  Need to have some termination condition RECURSIVE FUNCTIONS AND SUBROUTINES

14  Modify the factorial function in your combination program to be recursive. EXERCISE 4

15  Modules  Derived Data Types  Public, Private Entities  Arrays  Namelists  Reading/Writing to files  Pointers TOMORROW


Download ppt "FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language."

Similar presentations


Ads by Google