Fortran: Genel Bilgiler Dr. Mürtezaoğlu. ENF 102 Bilgisayar Programlama Dili Fortran 90.

Slides:



Advertisements
Similar presentations
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.
Advertisements

FORTRAN Short Course Week 1 Kate T-C February 17, 2008.
Some computer fundamentals and jargon Memory: Basic element is a bit – value = 0 or 1 Collection of “n” bits is a “byte” Collection of several bytes is.
COSC 120 Computer Programming
16/13/2015 3:30 AM6/13/2015 3:30 AM6/13/2015 3:30 AMIntroduction to Software Development What is a computer? A computer system contains: Central Processing.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Program Design and Development
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Introduction to Computers and Programming. Some definitions Algorithm: –A procedure for solving a problem –A sequence of discrete steps that defines such.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Introduction to a Programming Environment
Introduction to C Programming
Introduction to Computers and Programming. Some definitions Algorithm: Algorithm: A procedure for solving a problem A procedure for solving a problem.
Chapter 1: Introduction To Computer | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 1 Introduction To Computers.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Computer Science 101 Introduction to Programming.
1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009.
CS102 Introduction to Computer Programming
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Introduction to FORTRAN-90 University of Liverpool course.
Chapter Introduction to Computers and Programming 1.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
High-Level Programming Languages: C++
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Computer Science 101 Introduction to Programming.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Input, Output, and Processing
Introduction to Computer Systems and the Java Programming Language.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1 October 20, October 20, 2015October 20, 2015October 20,
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
Basic Computer Organization Rashedul Hasan.. Five basic operation No matter what shape, size, cost and speed of computer we are talking about, all computer.
Computer and Programming. Computer Basics: Outline Hardware and Memory Programs Programming Languages and Compilers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CPS120: Introduction to Computer Science Variables and Constants.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Introduction to Computer Programming using Fortran 77.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Chapter 1: Introduction to Computers and Programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Topics Designing a Program Input, Processing, and Output
Lecture 2 Introduction to Programming
CSCE Fall 2013 Prof. Jennifer L. Welch.
Coding Concepts (Basics)
Low Level Programming Languages
CSCE Fall 2012 Prof. Jennifer L. Welch.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 6 Programming the basic computer
Basic Computer Organization
Presentation transcript:

Fortran: Genel Bilgiler Dr. Mürtezaoğlu

ENF 102 Bilgisayar Programlama Dili Fortran 90

Introduction What is a Computer? What is Hardware and Software? Telling a Computer What To Do Some Basic Terminology How Does Computer Memory Work? Numeric Storage Programming Languages High-level Programming Languages An Example Problem An Example Program Analysis of Program A Closer Look at the Specification Part A Closer Look at the Execution Part How to Write a Computer Program A Quadratic Equation Solver - The Algorithm A Quadratic Equation Solver - The Program A Quadratic Equation Solver - The Testing Phase Points Raised Bugs - Compile-time Errors Bugs - Run-time Errors Compiler Switches

What is a Computer? A simple computer may look like this: memory (RAM) -- used to store values during execution of a program, CPU (Central Processor Unit) -- does the `work', disc drive -- `permanently' stores files, keyboard -- allows user to input information, VDU -- visually outputs data, Introduction

–What is Hardware and Software? –A computer system comprises hardware and software. –Hardware is the physical medium, for example: –circuit boards –processors –keyboard –Software are computer programs, for example: –operating system –editor –compilers –a Fortran 90 program

Introduction Telling a Computer What To Do To get a computer to perform a specific task it must be given a sequence of unambiguous instructions or a program. We meet many examples of programs in everyday life, for example, instructions on how to assemble a bedside cabinet. These instructions are generally numbered, meaning that there is a specific order to be followed, they are also (supposed to be) precise so that there is no confusion about what is intended: insert the spigot into hole `A', apply glue along the edge of side panel, press together side and top panels attach toggle pin `B' to gromit `C'... and so on If these instructions are not followed `to the letter', then the cabinet would turn out wonky.

Introduction Some Basic Terminology It is necessary to cover some terminology. Hopefully, much of it will be familiar -- you will hear many of the terms used throughout the course. Bit is short for Binary Digit. Bits have value of 1 or 0, (or on or off, or, true or false), 8 Bits make up 1 Byte, 1024 Bytes make up 1 KByte (1 KiloByte or 1K), (``Why 1024?'' I hear you ask. Because KBytes make up 1 MByte (1 MagaByte or 1M), 1024 MBytes make up 1 GByte (1 GigaByte or 1G), all machines have a wordsize -- a fundamental unit of storage, for example, 8-bits, 16-bits, etc. The size of a word (in Bytes) differs between machines. A Pentium based machine is 32-bit. a flop is a floating point operation per second. A floating point operation occurs when two real numbers are added. Today, we talk of megaflops or even gigaflops. parallel processing occurs when two or more CPUs work on solution of the same problem at the same time.

Introduction How Does Computer Memory Work? Here the wordsize is 8-bits: A computers memory is addressable, each memory location will contain some sort of `value', Each location has a specific `number' (represented as hexadecimal [base-16], e.g., 3F2C), Fortran 90 allows (English) names to be given to memory locations, the value of a location can be read from or written to. The CPU can say, `fetch the contents of memory location 3F2C' or `write this value to location 3AF7'.

Introduction Numeric Storage In general, there are two types of numbers used in Fortran 90 programs INTEGER s (whole numbers) and REAL s (floating point numbers). INTEGER s are stored exactly, often in range (-32767, 32767). REAL s are stored approximately. –They are partitioned into a mantissa and an exponent, –Exponent can only take a small range of values. You can get numeric exceptions: overflow -- exponent is too big, underflow -- exponent is too small. In Fortran 90 you can decide what numeric range is to be supported. CHARACTER s are stored differently.

Introduction Programming Languages Programming languages must be: totally unambiguous (unlike natural languages, for example, English), expressive -- it must be fairly easy to program common tasks, practical -- it must be an easy language for the compiler to translate, simple to use. All programming languages have a very precise syntax (or grammar). This ensures all syntactically-correct programs have a single meaning.

Introduction High-level Programming Languages Assembler code is a Low-Level Language. Fortran 90, FORTRAN 77, ADA, C and Java are High-Level Languages. a program is a series of instructions to the CPU, could write all programs in assembler code but this is a slow, complex and error-prone process, high-level languages are more expressive, more secure and quicker to use, the high-level program is compiled (translated) into assembler code by a compiler.

Introduction To convert from F (Fahrenheit) to C (Centigrade) we can use the following formula: To convert from C to K (Kelvin) we add 273. The program would accept a Fahrenheit temperature as input and produce the Centigrade and Kelvin equivalent as output. PROGRAM Temp_Conversion IMPLICIT NONE INTEGER :: Deg_F, Deg_C, K PRINT*, "Please type in the temp in F“ READ*, Deg_F Deg_C = 5*(Deg_F- 32)/9 PRINT*, "This is equal to", Deg_C, "C" K = Deg_C PRINT*, "and", K, "K" END PROGRAM Temp_Conversion This program, called Temp.f90, can be compiled: chad2-13{adamm} 26> f90 Temp.f90 NAg Fortran 90 compiler v2.2. New Debugger: 'dbx90' and run: chad2-13{adamm} 27> a.out Please type in the temp in F 45 This is equal to 7 C and 280 K

Introduction Analysis of Program The code is delimited by PROGRAM... END PROGRAM statements. Between these there are two distinct areas. Specification Part –specifies named memory locations (variables) for use, –specifies the type of the variable, Execution Part –reads in data, –calculates the temp in C and K and –prints out results.

Introduction A Closer Look at the Specification Part IMPLICIT NONE -- this should always be present. Means all variables must be declared. INTEGER :: Deg_F, Deg_C, K -- declares three INTEGER (whole number) variables. Other variable types: –REAL -- real numbers, e.g., ,, –LOGICAL -- take vaules.TRUE. or.FALSE., –CHARACTER -- contains single alphanumeric character, e.g., 'a', –CHARACTER(LEN=12) -- contains 12 alphanumeric characters, a string, Fortran 90 is not case sensitive. K is the same as k and INTEGER is the same as integer.

Introduction A Closer Look at the Execution Part This is the part of the program that does the actual `work'. PRINT*, "Please type in the temp in F" -- writes the string to the screen, READ*, Deg_F -- reads a value from the keyboard and assigns it to the INTEGER variable Deg_F, Deg_C = 5*(Deg_F-32)/9 -- the expression on the RHS is evaluated and assigned to the INTEGER variable Deg_C, –* is the multiplication operator, –- is the subtraction operator, –/ is the division operator, (takes longer than *) –= is the assignment operator. PRINT*, "This is equal to", Deg_C, "C" -- displays a string on the screen followed by the value of a variable (Deg_C) followed by a second string ("C"). By default, input is from the keyboard and output to the screen.

Introduction How to Write a Computer Program There are 4 main steps: specify the problem, analyse and break down into a series of steps towards solution, write the Fortran 90 code, compile and run (i.e., test the program). It may be necessary to iterate between steps 3 and 4 in order to remove any mistakes. The testing phase is very important.

Introduction A Quadratic Equation Solver - The Algorithm The problem Write a program to calculate the roots of a quadratic equation of the form: The roots are given by the following formula The algorithm READ values of a, b and c, if a is zero then stop as we do not have a quadratic, calculate value of discriminant if D is zero then there is one root:, if D is > 0 then there are two real roots: and, if D is < 0 there are two complex roots: and, PRINT solution.

Introduction A Quadratic Equation Solver - The Program PROGRAM QES IMPLICIT NONE INTEGER :: a, b, c, D REAL :: Real_Part, Imag_Part PRINT*, "Type in values for a, b and c" READ*, a, b, c IF (a /= 0) THEN ! Calculate discriminant D = b*b - 4*a*c IF (D == 0) THEN ! one root PRINT*, "Root is ", -b/(2.0*a) ELSE IF (D > 0) THEN ! real roots PRINT*, "Roots are",(-b+SQRT(REAL(D)))/(2.0*a),& "and", (-b- SQRT(REAL(D)))/(2.0*a) ELSE ! complex roots Real_Part = -b/(2.0*a) ! D < 0 so must take SQRT of -D Imag_Part = (SQRT(REAL(-D))/(2.0*a)) PRINT*, "1st Root", Real_Part, "+", Imag_Part, "i" PRINT*, "2nd Root", Real_Part, "-", Imag_Part, "i" END IF ELSE ! a == 0 PRINT*, "Not a quadratic equation" END IF END PROGRAM QES

Introduction A Quadratic Equation Solver - The Testing Phase The output from the program is as follows: uxa{adamm} 35> a.out Type in values for a, b and c Roots are and uxa{adamm} 36> a.out Type in values for a, b and c Root is uxa{adamm} 37> a.out Type in values for a, b and c st Root i 2nd Root i uxa{adamm} 38> a.out Type in values for a, b and c Not a quadratic equation Its can be seen that the `test data' used above exercises every line of the program. This is important in demonstrating correctness.

Introduction Points Raised The previous program introduces some new ideas, comments -- anything on a line following a ! is ignored, & -- means the line is continues, IF construct -- different lines are executed depending on the value of the Boolean expression, relational operators -- == (`is equal to') or > (`is greater than'), nested constructs -- one control construct can be located inside another. procedure call -- SQRT(X) returns square root of X. type conversion -- in above call, X must be REAL. In the program, D is INTEGER, REAL(D) converts D to be real valued. To save CPU time we only calculate the discriminant, D, once.

Introduction Bugs - Compile-time Errors In previous program, what if we accidentally typed: Rael_Part = -b/(2.0*a) the compiler generates a compile-time or syntax error: uxa{adamm} 40> f90 Quad.f90 NAg Fortran 90 compiler v2.2. Error: Quad.f90, line 16: Implicit type for RAEL_PART detected at Error: Quad.f90, line 24: Symbol REAL_PART referenced but never set detected at [f90 terminated - errors found by pass 1]

Introduction Bugs - Run-time Errors If we had typed Real_Part = -b/(.0*a) then the program would compile but we would get a run- time error, uxa{adamm} 43> a.out Type in values for a, b and c *** Arithmetic exception: Floating divide by zero - aborting Abort It is also possible to write a program that gives the wrong results!

Introduction Compiler Switches Compiler is invoked by f90 f90 Quad.f90 This: checks the program syntax generates executable code in a file a.out Switches or flags can be supplied to compiler: -o : gives executable a different name, for example, Quad f90 Quad.f90 -o Quad-time: report execution times f90 Quad.f90 -o Quad –time For more information about the compiler type: man f90