INTRODUCTION TO FORTRAN PART II

Similar documents
Introduction to Programming with Fortran 90

Chapter 3. Fortran Statements

Chapter 4. Fortran Arrays

Review More Arrays Modules Final Review

ARRAYS COMPUTER PROGRAMMING. By Zerihun Alemayehu

Review of the C Programming Language

C-LANGUAGE CURRICULAM

Review of the C Programming Language for Principles of Operating Systems

The Fortran Basics. Handout Two February 10, 2006

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

ISO/IEC : TECHNICAL CORRIGENDUM 2

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Introduction. C provides two styles of flow control:

PLD Semester Exam Study Guide Dec. 2018

Goals for This Lecture:

Fundamentals of Programming Session 13

D-BAUG Informatik I. Exercise session: week 5 HS 2018

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Programming for Engineers Iteration

Questions Bank. 14) State any four advantages of using flow-chart

2 3. Syllabus Time Event 9:00{10:00 morning lecture 10:00{10:30 morning break 10:30{12:30 morning practical session 12:30{1:30 lunch break 1:30{2:00 a

ECE 122. Engineering Problem Solving with Java

Our Strategy for Learning Fortran 90

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Information technology Programming languages Fortran Part 1: Base language

Allocating Storage for 1-Dimensional Arrays

CS 230 Programming Languages

Intrinsic Numeric Operations

Practical Exercise 1 Question 1: The Hello World Program Write a Fortran 95 program to write out Hello World on the screen.

FORM 2 (Please put your name and form # on the scantron!!!!)

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Flow Control. CSC215 Lecture

Why Is Repetition Needed?

Introduction to Fortran95 Programming Part II. By Deniz Savas, CiCS, Shef. Univ., 2018

Chapter 8 Statement-Level Control Structures

CS201- Introduction to Programming Current Quizzes

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Fortran 90 - A thumbnail sketch

Introduction to Multithreaded Algorithms

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Control Constructs: Loops - while loops - iterative loops (counting loops)

AN INTRODUCTION TO FORTRAN 90 LECTURE 2. Consider the following system of linear equations: a x + a x + a x = b

Bits, Bytes, and Precision

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Welcome. Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at BST

Module 4: Decision-making and forming loops

142

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

A Brief Introduction to Fortran of 15

Shell CSCE 314 TAMU. Haskell Functions

MATLAB GUIDE UMD PHYS401 SPRING 2011

Introduction to Computer Science Midterm 3 Fall, Points

7 Control Structures, Logical Statements

PROBLEM SOLVING WITH FORTRAN 90

FORTRAN - ARRAYS. For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,

C/C++ Programming for Engineers: Matlab Branches and Loops

This is the basis for the programming concept called a loop statement

Informatica 3 Syntax and Semantics

Repetition CSC 121 Fall 2014 Howard Rosenthal

Chapter 6. A Brief Introduction to Fortran David A. Padua

Subroutines, Functions and Modules

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

International Standards Organisation. Parameterized Derived Types. Fortran

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

Introduction to C/C++ Lecture 3 - Program Flow Control

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

C: How to Program. Week /Apr/16

6.1 Expression Evaluation. 1 of 21

NCS 301 DATA STRUCTURE USING C

It can be confusing when you type something like the expressions below and get an error message. a range variable definition a vector of sine values

Last Class. While loops Infinite loops Loop counters Iterations

n Group of statements that are executed repeatedly while some condition remains true

Course Topics - Outline

1 Lexical Considerations

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Introduction to Modern Fortran

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

function [s p] = sumprod (f, g)

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

COS 140: Foundations of Computer Science

Ch. 7: Control Structures

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

CSC Web Programming. Introduction to SQL

Loops. CSE 114, Computer Science 1 Stony Brook University

Control Structures. Control Structures 3-1

Department of Computer Science COMP The Programming Competency Test

Chapter 8. Statement-Level Control Structures

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Operators in C. Staff Incharge: S.Sasirekha

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Student Performance Q&A:

Transcription:

INTRODUCTION TO FORTRAN PART II Prasenjit Ghosh

An example: The Fibonacci Sequence The Fibonacci sequence consists of an infinite set of integer nos. which satisfy the following recurrence relation x n =x n 1 x n 2,n 3 x 1 =1, x 2 =2 Originally it is a model for rabbit breeding The ratio of x n /x n-1 tends to the golden ratio = 5 1 2 The sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,... Write a program to generate the first 45 terms of the series and estimate the golden ratio.

ALGORITHM (1) Initial conditions, set x 1 =x 2 =1 (2) Initialize the iterator, set n=3 (3)Calculate x n =x n-1 +x n-2 (4) Print out x n and x n /x n-1 (5) Increment n (6) If n < 46 go back to step 3 What type of data type for n & x n? What variable names? How to implement step 6?

PROGRAM DESIGN Write x x n, y x n 1, z x n 2 Variable x,y,z,n are all of type INTEGER Iteration step is x=y+z Have to rename z=y, y=x before next iteration

FIBONACCI PROGRAM

GOTO/ GO TO Statement The syntax is: GOTO label or GO TO label Where the label is the statement label elsewhere in the program. The label must be the first thing on the line The GOTO statement transfers program control to the line of code with the statement label. It can be confusing, so one should try to avoid use this.

IF Statement The syntax is IF (logical expression) action statement If the logical expression is true then the action statement is executed. Note: Only one action statement What if there are more than one logical expression and more than one action statement? Use the IF construct

IF Construct The syntax is IF (logical expr) THEN block 1 ELSEIF (logical expr) THEN block 2... ELSE block n END IF ELSEIF must always come before ELSE

EXAMPLE OF USAGE OF IF BLOCK

Named IF statement The IF can be preceded by a <name> and the END IF followed by a name The loop name must match and be distinct. This helps for checking and clarity

The syntax is: The infinite DO loop DO code-block END DO When END DO is reached, control returns to the beginning of the loop The loop is exited only if there is an EXIT statement in the code-block. The EXIT statement takes the control of the program immediately after the next END DO

The finite DO loop INTEGER:: myvar, mymin, mymax,n..... DO myvar=mymin,mymax, n code block END DO myvar: the loop counter variable mymin: the starting value of the counter mymax: the end value of the counter n: increment of the counter by n, default is 1 NOTE: All variables should be INTEGERS There can be many DO loops within a DO loop. These are called nested DO loops Similar to IF there can be named DO loops

ARRAYS Array is an ordered sequence of many instances of the same data type Equivalent of a vector or a matrix Declared using the DIMENSION keyword: <DTYPE>, DIMENSION(<id>) :: <var> <id> is a comma separated list describing the index range in each dimension: A single value n means that n values in that dimension with indices {1,2,...,n} Values n:m means (m-n+1) values in that dimension, with indices {n, n+1,...,m}

Examples

SOME TERMINOLOGIES REAL:: a(0:100), b(5,5,5) Rank is the no. of dimensions eg. 'a' has rank 1 and 'b' has rank 3 Bounds are the upper & lower limits eg. 'a' has bounds 0:100 and B has 1:5, 1:5, 1:5 A dimension's extent is the UPB-LWB+1 eg. 'a' has an extent of 100 while 'b' has 5, 5 and 5 Size is the total no. of elements eg. 'a' has size 100 while 'b' has size 125 Shape is it's rank and extent eg. 'a' has shape (100) and 'b' (5,5,5) Arrays are conformable if they share a shape

Accessing array elements We access a particular element of an array via its subscript values So to assign to a variable x the value in the m-th row and n-th column of a matrix A, we would code x=a(m,n) The index specifiers should lie within the ranges given in the declaration Sub-array can be accessed using a range-specifier (as in a declaration).

Example of array assignment

Example of array assignment

Array operation Most built in operators/ functions acts element by element on arrays REAL, DIMENSION(50):: v1, v2, v3 v1=sin(v2)+exp(v3) REAL, DIMENSION(50):: v1, v2, v3 LOGICAL, DIMENSION(50):: flag flag=(arr1 > arr2.or. arr3 < 0.0)

Array intrinsic function SUM(x[,n])! Sum of all elements of x PRODUCT(x[,n])! Product of all elements of x TRANSPOSE(x)! x ij x ji DOT_PRODUCT(x,y)! i x i. y i z MATMUL(x,y)! k x ik. y kj z ij Second dimension of x must match with first dimension of y

Array Element Order Fortran arrays are laid out in rows REAL:: A(1:3, 1:4) The elements of A are stored in the order A(1,1),A(2,1),A(3,1),A(1,2),A(2,2),A(3,2),A(1,3),A(2,3),A(3,3), A(1,4),A(2,4),A(3,4) In general one need not worry about the order except for: I/O of arrays, especially unformatted ones Array constructors & array constants

SIMPLE I/O OF ARRAYS Arrays can be included in input & output. They are expanded in array element order REAL, DIMENSIONS(3,2):: array1 READ*, array1 It is same as REAL, DIMENSIONS(3,2):: array1 READ*, array1(1,1),array1(2,1),array1(3,1), & array1(1,2),array1(2,2),array1(3,2)

ALLOCATABLE ARRAYS Sometimes the size of the array may not be known Beforehand. In those cases use the key words ALLOCATABLE, ALLOCATE and DEALLOCATE

ACCESSING ARRAY SUBSETS It is possible to access any sub-array Syntax: must supply a comma-separated list of index descriptors (one for each dimension) Each index descriptor may be (in addition to a single value or colon separated pair): A colon (returns all the elements in that dimension) A colon preceded by a value: array(i:) is equivalent to array(i:ubound(array, 1)) A colon followed by a value: array(:i) is equivalent to array(lbound(array,1):i)

STRINGS A string is an array of type CHARACTER String length declared using the LEN keyword Sub-string referencing requires a colon, so to access i-th character of the string Concatenation operator //: to link together 2 or more strings

An example

Array sorting, an exercise Sort a list of numbers into ascending order Algorithm: Read the nos. and store them in an array Sort them into ascending order of magnitude Print them out in sorted order