FORTRAN 77 Programming Lecture 1 January 2001 Dr

  • Slides: 23
Download presentation
FORTRAN 77 Programming. Lecture 1 : January 2001 Dr. Andrew Paul Myers 5/20/2021 1

FORTRAN 77 Programming. Lecture 1 : January 2001 Dr. Andrew Paul Myers 5/20/2021 1

Course Methods. n n n n 5/20/2021 Lectures. Handout notes. Supervised practical work. Daily

Course Methods. n n n n 5/20/2021 Lectures. Handout notes. Supervised practical work. Daily submission of exercises. Daily feedback on exercises. Model Answers. Final project. Weekly clinic for final Project. 2

Introduction. n n n A course in FORTRAN 77. Why FORTRAN? Objectives : •

Introduction. n n n A course in FORTRAN 77. Why FORTRAN? Objectives : • • n General education in computing. Introduction to programming. First write a simple program, then… Write complex program. Course Benefits : • I. T. is a part of all our lives and getting more so! • FORTRAN and computing skills for other courses. • Future employment. 5/20/2021 3

Tools For The Job. n Programming Environment : § § § 5/20/2021 CFS system

Tools For The Job. n Programming Environment : § § § 5/20/2021 CFS system and Exceed 6. 2. Unix system (SG IRIX 6. 5). Silicon Graphics Desktop Windows Text Editor (emacs). FORTRAN 77 Compiler (Mips 7. 2. 1). Workshop Debugger (cvd). 4

Programming Languages. n n n You are users! How computers work. Machine code. High

Programming Languages. n n n You are users! How computers work. Machine code. High level languages. Fortran : FORMula TRANslation. Other languages : • Basic • C/C++ • Pascal 5/20/2021 5

Programming Cycle. n n n n 5/20/2021 Analyse the task. Plan the program, a

Programming Cycle. n n n n 5/20/2021 Analyse the task. Plan the program, a structured approach! Flowcharts & Dry running. Edit your source code. Compile and link program. Execute and debug program. Edit and recompile as necessary. 6

IRIX f 77 Compiler. n n Edit source program (*. f) with “emacs” editor.

IRIX f 77 Compiler. n n Edit source program (*. f) with “emacs” editor. Save file. Compile and run on Unix command line in a shell window : /disk/n/gps> f 77 –o test. f /disk/n/gps> test 5/20/2021 7

Simple Structure of A FORTRAN Program. n n n 5/20/2021 Program name. Declare variables

Simple Structure of A FORTRAN Program. n n n 5/20/2021 Program name. Declare variables and structures. Assign values to variables. Process data. Print results. End program. 8

Flow of a Program. n n n n 5/20/2021 Linear sequence. One command per

Flow of a Program. n n n n 5/20/2021 Linear sequence. One command per line. Position on line : Very Important! Comments Statements (ignored). Repetition : Loops. Selections : Conditional statements. Always finish with an END statement. 9

Position on a line. n 1 2 -5 C 9 9999 5/20/2021 The layout

Position on a line. n 1 2 -5 C 9 9999 5/20/2021 The layout of FORTRAN program dates back to old 80 column punched cards, which were used for program input. 6 7 -72 Total=x_value+y_value & +z_value Comment line. 73 -80 FORMAT(‘Answer =‘, I 4) 10

Variable Declarations. n Variable names : § § 5/20/2021 Must be at least one

Variable Declarations. n Variable names : § § 5/20/2021 Must be at least one alphabetic character long, up to a maximum of 31 alphanumeric characters. Must start with an alphabetic character. Case insensitive. Alphanumeric characters are : a-z, 0 -9 and the underscore ( _ ). Implicit variables. I to N integers! 11

Examples. n Valid names : • • n Invalid names : • • 5/20/2021

Examples. n Valid names : • • n Invalid names : • • 5/20/2021 X THEDAY Min_cur Time 28 X*Z THE TIME 7 YEARS _no_way$ 12

Basic Data Types. n n n REAL INTEGER COMPLEX LOGICAL CHARACTER x=5. 0 i=20

Basic Data Types. n n n REAL INTEGER COMPLEX LOGICAL CHARACTER x=5. 0 i=20 z=(1. 4, 3. 2) test=. TRUE. char=‘Hello’ More advanced data types can be made from these basic types. 5/20/2021 13

Declarations. <Data Type> <variable> [, <variable(s)>] e. g. REAL x REAL radius, volume INTEGER

Declarations. <Data Type> <variable> [, <variable(s)>] e. g. REAL x REAL radius, volume INTEGER loop, temp CHARACTER string*10, name*30 5/20/2021 14

Parameters. n Parameters are constants, their value, once defined, can not be changed. REAL

Parameters. n Parameters are constants, their value, once defined, can not be changed. REAL g, pi INTEGER days PARAMETER (days=365) PARAMETER (g=9. 81, pi=3. 142) 5/20/2021 15

Assignments. <variable> = <value> | <variable> | <expression> radius=2. 5 y=z test=value+loop-temp volume=(4. 0*pi*radius**3.

Assignments. <variable> = <value> | <variable> | <expression> radius=2. 5 y=z test=value+loop-temp volume=(4. 0*pi*radius**3. 0)/3. 0 Expressions follow the BODMAS precedence rule. Operators +, -, *, / and ** 5/20/2021 16

Control Structures. Basic building blocks of programs. They control the flow of the program.

Control Structures. Basic building blocks of programs. They control the flow of the program. There are 3 different types : n n n 5/20/2021 Linear Sequence. Selection. Iteration or Loop. 17

Other Statements. n PROGRAM [ program name ] END n C or * n

Other Statements. n PROGRAM [ program name ] END n C or * n n n A comment. PRINT*, ’Hello’ PRINT*, ’Value of X = ‘, x This is free format output. 5/20/2021 18

Data Input. n n n 5/20/2021 Programs are useless without data! Use the READ

Data Input. n n n 5/20/2021 Programs are useless without data! Use the READ statement to allow users to input data. Prompt user for the data too! e. g. PRINT*, ’Enter values for x & y : ’ READ*, x, y 19

Character Input. n A normal read statement can not be used to enter character

Character Input. n A normal read statement can not be used to enter character variables. Use the following: PRINT*, ’Continue (y/n) : ‘ READ ‘(A 1)’, yes_or_no ‘(A<n>)’ – <n> is the number of characters. 5/20/2021 20

Good Programming Style. n n n 5/20/2021 Comment your program! FORTRAN keywords in upper

Good Programming Style. n n n 5/20/2021 Comment your program! FORTRAN keywords in upper case. Variables in lower case. Use descriptive variable names. Blanks may be used to improve readability. Indent code with “tabs”. 21

General Program Layout. PROGRAM [ program name ] [ comments ] [ declaration statements

General Program Layout. PROGRAM [ program name ] [ comments ] [ declaration statements ] [ executable statements ] STOP END 5/20/2021 22

References. A Crash Course in FORTRAN 77. Donald M. Monro. (Edward Arnold). Irix Insight

References. A Crash Course in FORTRAN 77. Donald M. Monro. (Edward Arnold). Irix Insight On-line Help. SGI Developer Section. MIPSpro FORTRAN 77 Programmer’s guide. 5/20/2021 23