Beginning Fortran Fortran (77) Basics 22 October 2009 *Black text on white background provided for easy printing.

Slides:



Advertisements
Similar presentations
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Advertisements

Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Introduction to arrays
Input and Output READ WRITE OPEN. FORMAT statement Format statements allow you to control how data are read or written. Some simple examples: Int=2; real=
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Programming with MATLAB
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
ME1107 Computing Y Yan
Chapter 8 Introduction to Arrays Part II Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Pseudocode and Algorithms
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Computer Science 1620 Programming & Problem Solving.
CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
Introduction to FORTRAN-90 University of Liverpool course.
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Python.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Guidelines for the CMM coding project 5 October 2006 (or, “How to make your life easier in the long run”)
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Multi-Dimensional Arrays
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
How to Write a Fortran Jiffy (C)2005 Mark Rould University of Vermont.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
sequence of execution of high-level statements
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Visual Basic Programming
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Basics of Most C++ Programs // Programmer: Clayton Price date: 9/4/ // File: fahr2celc.cpp 03. // Purpose:
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
Control Structures RepetitionorIterationorLooping Part I.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
1 ENERGY 211 / CME 211 Lecture 6 October 3, 2008.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Control Structures RepetitionorIterationorLooping Part I.
Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing.
More on F90 Outline: I.Parameter statements II.Comments III.Program layout-- execution part IV.if/then/else V.Case statements VI.do loops VII.goto VIII.Intrinsic.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
EEE 161 Applied Electromagnetics
REPETITION CONTROL STRUCTURE
Programming For Nuclear Engineers Lecture 6 Arrays
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Logical Operators and While Loops
For Loops October 12, 2017.
Unit 2 Programming.
Number and String Operations
Logical Operators and While Loops
Unit 3: Variables in Java
Desk Checking.
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Beginning Fortran Fortran (77) Basics 22 October 2009 *Black text on white background provided for easy printing

Example Code Write a program to read in five values of temperature in Fahrenheit and convert to degrees Celsius OR Kelvin OR both.

Your Typical Program c PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE

Your Typical Program c PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE

Program Declaration You declare what kind of Fortran file you are writing on the first line. Syntax: c PROGRAM CONVERTF

Program Declaration You declare what kind of Fortran file you are writing on the first line. Syntax: c PROGRAM CONVERTF Specifies the file as a program Program name – something short but descriptive

Your Typical Program c PROGRAM CONVERTF STOP END Program Options Declaration of Variables MAIN CODE

Options and Variables There are numerous options – you can Google them if you are interested In general, there are two kinds: – You can “include” variables from another *.h file by putting include ‘.h’ in the options section. – You can switch on other options about how the code is run (Google it) – We are going to use implicit none

Options and Variables All variables we are going to use must be accounted for in the declaration section (no implicit variables allowed) – implicit none What do we need? – Temperature in Fahrenheit, Celsius, Kelvin – Logicals (do we want Celsius, Kelvin, both?) – Some integer to loop through all 5 values – Syntax:

Options and Variables c IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I

Options and Variables c IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Specify a special parameter – an unchangeable value that can immediately be used (unlike a variable, which can change value)

Options and Variables c IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Array of 5 REALs for Fahrenheit temps

Options and Variables c IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Array of 5 REALs for Kelvin temps

Options and Variables c IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Array of 5 REALs for Celsius temps

Options and Variables c IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Logical: Do we want to convert to Celsius (TRUE) or not (FALSE)?

Options and Variables c IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Logical: Do we want to convert to Kelvin (TRUE) or not (FALSE)?

Options and Variables c IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Integer that counts from 1 to 5 for loop over one-dimensional arrays

Your Typical Program c PROGRAM CONVERTF IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I STOP END MAIN CODE

Main Code We need to do several things: – Read in 5 values of temperature – Determine if we need to convert to Celsius, Kelvin, or both – Output values

F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User DOC DOK

F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User Read in 5 values of F into array DOC DOK

F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User For each of the five temperatures (for-loop): DOC DOK

F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User Compute C (we are going to do this no matter what, because we know that the output has to either be C or K or both, and we need C in order to calculate K anyway). DOC DOK

F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User Output F to user (this should be done just to make sure that the input was read correctly). DOC DOK

F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User If DOC = TRUE, then output C as well. DOC DOK

F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User If DOK = TRUE, then compute K from C and output to user as well. DOC DOK

Main Code F F From User c DO I = 1, NT READ(*,*) F(I) ENDDO

Main Code F F From User c DO I = 1, NT READ(*,*) F(I) ENDDO READ is a Fortran command that is used for input. Syntax: READ(, ) Location (*) = read in from the terminal Format (*) = no particular format

Main Code F F From User c DO I = 1, NT READ(*,*) F(I) ENDDO WRITE(*,*) ‘Convert to C?’ READ(*,*) DOC WRITE(*,*) ‘Convert to K?’ READ(*,*) DOK

Main Code F F From User c DO I = 1, NT READ(*,*) F(I) ENDDO WRITE(*,*) ‘Convert to C?’ READ(*,*) DOC WRITE(*,*) ‘Convert to K?’ READ(*,*) DOK Write to screen with no particular formatting.

Main Code c DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO

Main Code c DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO For each temperature:

Main Code c DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO For each temperature: Compute Celsius temp.

Main Code c IF (DOK.EQV..TRUE.) THEN DO I = 1, NT K(I) = C(I) ENDDO ENDIF

Main Code c IF (DOK.EQV..TRUE.) THEN DO I = 1, NT K(I) = C(I) ENDDO ENDIF Logical trap: If we want to calculate Kelvin:

Main Code c IF (DOK.EQV..TRUE.) THEN DO I = 1, NT K(I) = C(I) ENDDO ENDIF Logical trap: If we want to calculate Kelvin: Loop through temperatures and calculate Kelvin temps. (If DOK =.FALSE., this entire loop is avoided)

Main Code c IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..FALSE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C’ ENDDO ENDIF c IF ((DOC.EQV..FALSE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIF c IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C ’, K(I), ‘K’ ENDDO ENDIF

Main Code c IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..FALSE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C’ ENDDO ENDIF c IF ((DOC.EQV..FALSE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIF c IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C ’, K(I), ‘K’ ENDDO ENDIF

c PROGRAM CONVERTF IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I DO I = 1, NT READ(*,*) F(I) ENDDO WRITE(*,*) ‘Convert to C?’ READ(*,*) DOC WRITE(*,*) ‘Convert to K?’ READ(*,*) DOK DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO IF (DOK.EQV..TRUE.) THEN DO I = 1, NT K(I) = C(I) ENDDO ENDIF IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..FALSE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C’ ENDDO ENDIF c IF ((DOC.EQV..FALSE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIF c IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C ’, K(I), ‘K’ ENDDO ENDIF STOP END

Program Start Options/Variable Declaration Main Code Program End c PROGRAM CONVERTF IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I DO I = 1, NT READ(*,*) F(I) ENDDO WRITE(*,*) ‘Convert to C?’ READ(*,*) DOC WRITE(*,*) ‘Convert to K?’ READ(*,*) DOK DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO IF (DOK.EQV..TRUE.) THEN DO I = 1, NT K(I) = C(I) ENDDO ENDIF IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..FALSE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C’ ENDDO ENDIF c IF ((DOC.EQV..FALSE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIF c IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C ’, K(I), ‘K’ ENDDO ENDIF STOP END

Compilation Compilation is performed in the terminal: Syntax: -o

Compilation Compilation is performed in the terminal: Syntax: -o Depends on system: f77, g77, pgf77, etc.

Compilation Compilation is performed in the terminal: Syntax: -o We wish to create an object that is an executable file with the following name

Compilation Compilation is performed in the terminal: Syntax: -o Use this *.f file to compile the executable

Compilation Compilation is performed in the terminal: Syntax: -o Also depends on compiler. Some frequent options: Mextend – allows you to go over column 70 in the code Mbounds – if you attempt to reference an array index out of bounds, will notify you Mbyteswapio – some formats require a byte-swap

Compilation Compilation is performed in the terminal: Syntax: -o pgf77 –o CONVERTF.exe CONVERTF.f