1 Chapter 2 Basic Fortran. 2 Attendance Requirements  Attendance is required for both class and lab. hours  Minimum %70 for class attendance  Minimum.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Introduction to C Programming
1 ICS 101 – LAB 2 Arithmetic Operations I Putu Danu Raharja kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University.
Chapter 2 Basic Elements of Fortan
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Introduction to C Programming
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
A First Book of ANSI C Fourth Edition
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
Input & Output: Console
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Chapter 2 Variables.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CHAPTER 1 INTRODUCTION TO COMPUTING 1.1 Introduction.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
 Constants A constant is a fixed value of a data type that cannot be changed Integer Constants Whole numbers → Do not have decimal points Examples: 83.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 JavaScript: Introduction to Scripting
Introduction to the C Language
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Introduction to Scripting
Basic Elements of C++ Chapter 2.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to C++ Programming
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 2 Variables.
CS 100: Roadmap to Computing
elementary programming
Chapter 2: Introduction to C++.
Chapter 2 Variables.
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES AND OPERATIONS
Presentation transcript:

1 Chapter 2 Basic Fortran

2 Attendance Requirements  Attendance is required for both class and lab. hours  Minimum %70 for class attendance  Minimum %80 for lab. Attendance  This is one and only requirement for taking the final exam.

3 Text Books  Main text book of the class: Programming in F T.M.R. ELLIS, Ivor R. PHILLIPS, Addison-Wesley, 1998  Auxiliary text book: Essential Fortran Loren P. Meissner, PWS Publishing, 1997

4 2.1 Data types  There are five basic data types in fortran  1) INTEGER  2) REAL  3) COMPLEX  4) CHARACTER  5) LOGICAL Numerical-data types Strings of characters Logical data values Non-numerical data types

5 Integers  An Integer is a whole number (positive, negative, or zero) and does not contain commas or a decimal point. Examples for valid integer numbers are: the followings are the examples for invalid integer number: 8,675 (Commas are not allowed in numerical constants) 26.0 (Integer constants may not contain decimal points) --5 (Only one algebraic sign is allowed) 7- (The algebraic sign must precede the string of digits.)

6 Reals  A real constant must contain a decimal point, but no commas are allowed. Valid real constants are: invalid real constants 15,627 (commas are not allowed in numerical constants.) 786 (Real constants must contain a decimal point.) a real constant can be represented by an exponential. An exponent written as the letter E with an integer constant following. For example: may also be written as E3 which means X10 3 or similarly; E E4 and E-1

7 Complex  A complex number is represented as a pair of real (i.e., floating point) numbers. The first component of the pair represents the real part of the complex data object, and the second represents the imaginary part. For example; Z = a + i b Z = (a,b) NOTE: Since you don’t have enough background on complex numbers, you are not fully responsible from complex numbers and its related examples. But, you should now as much as taught.

8 Character strings  Character constants, also called strings, are sequences of symbols from the ANSI standard character set for Fortran.  The number of a character constant between double quotes is the length of the constant For example: ”PDQ123-A” (Character constant of length 8) ”John Q. Doe” (Character constant of length 11)

9 Logical type  An object of logical type has the value true or false, or (0 or 1), or (yes or no).

10 Arithmetic operators in F Operator Meaning +Addition -Substraction *Multiplication /Division **Exponentiation (or ‘rising the power of’)

11 Arithmetic operator priorities Operator Priority **High * and /Medium + and -LowExamples: W=c/d*b Total=2**3+5*2=18 W=x+z-y

12 Application  How to download and install the F_W95.EXE ? 1) Web page of the Class 2) Network neighborhood --> Bim -> Ntserver -> F 3) password is install READING Study pages (Ellis & Philips’s book)

13 Names & Declarations  A data object is a constant that never changes or a variable that can change during program execution.  Data object may have names. For example, Average, X, Y, Einstein, or Potential_Energy. Names in a program must conform to 3 rules: 1) A name may contain up to 31 letters, digits, and underscore characters 2) The first character of a name must be a letter 3) Imbedded blank characters are not permitted in a name IMPORTANT: keywords such as program, write, and end are not actually names

14 Type Declarations  Every variable and named constant must appear in a type declaration  The type of a Fortran variable determines the type of value that may be assigned to that variable.  In every F program, the specification statement implicit none must immediately follow the program statement program Research implicit none. end program Research Type Declaration Form Type name ::List of names Consider the following example

15 Type Declarations implicit none integer :: Counts, Loop_Index real :: Current, Resistance, Voltage Names defined as part of the F language, including keywords and intrinsic function names (such as sin, tan, abs, etc.), must be written in lower case. Names that you invent can use any combination of upper and lower case, but each name must be written consistently.

16 Type properties: Kind & Length Kind : A variable of any numerical type has a kind type parameter, which designates a subtype or variant of the type. Each type has a default computer representation For each numerical data type, F defines a set of integers to be used as kind type parameter values (i.e., the number 4 for real representation, number 8 for the higher-precision variant) Length : A variable of character data type has a string length property. A character type declaration must specify string length A type declaration appears in parentheses after the type name. If no kind parameter is specified, F selects the default computer representa- tion Type name (Type properties) :: List of names

17 Type properties: Kind & length  Other data attributes may be specified between the type properties and the double colon. Type name (Type properties), Attributes :: List of names Example: integer, parameter :: ARRAY_SIZE=12, SL=20 character (Len=SL), save :: Element_Name integer, dimension (ARRAY_SIZE) :: chart, list Exercise 1: Do exercises 1.2 on page 42 (Meissner’s book)!.

18 Constants  A constant in a program may have an explicit form, or it may be represented by a name. EXPLICIT CONSTANTS A constant in a program has a fixed value during the execution of the program. Consider the cases that: 1) A value may need to be changed before the program is executed again. 2) A constant in a declaration, such as the size of an array or the length of a character string, may need to be revised. Therefore we prefer to use a named constant instead of an explicit constant to prevent searching for all the appearances of a certain constant within the program which is a tedious and an error-prone task.

19 Constants  The name of a constant looks like the name of a variable and it must be listed in the type declaration  The keyword parameter designates a named constant  Houdini Principle: Don’t use magic numbers use a named constant rather than a explicit constant give always explanations ( use !)

20 Declaration for a Named Constant  Declaration of a named constant is as follows: Type name, parameter :: List of initializations where each list item has the form Name = Value definition The value definition is an explicit constant. Examples: integer, parameter :: LENGTH=12 real, parameter :: PLANK= e-34, PI= real, parameter :: GRAVITY=9.807, AVAGADRO= e23, & twoPI=2.0*PI integer, parameter :: A=20, HIGH=30, NEON=67 character (Len=2), parameter :: units=”Cm” ATTENTION: Continuation line with ampersand symbol.

21 Exercise Exercise 2:  Do exercises 1.3 on pages (Meissner’s book).  Study pages between (Ellis & Philips’s book).

22 Simple Input & Output Read (unit = *, fmt = *) Input List Write (unit = *, fmt = *) Output List  An asterisk as the unit in a read or write control list designates the default input device (the keyboard) or the default output device (The terminal screen)  An asterisk as the format designates list-directed formatting. Input data values for on-line list-directed input are entered at the computer keyboard in free form. Consecutive values must be separated by blanks. For example: read (unit = *, fmt = *) Radii, I, Current, Top can be entered as

23 Simple Input & Output  IMPORTANT: The items in an input list must be variable names. write (unit = *, fmt = *) ” Please enter the diameter of a circle” read (unit = *, fmt = *) Diameter write (unit = *, fmt = *) ” Diameter = ”, Diameter, ”circumference =”, & *Diameter, ”Area = ”, *Diameter**2

24 NUMBER REPRESENTATION NUMERICAL PRESICION AND RANGE  There is an increasing demand for high precision  Computer hardware representation varies from 8-bit to 128 bit word length  Precision requirements should be considered for both different type of computer hardware and problem type we have. For example: 9 decimal digits of numerical precision can be satisfied by 1) Single-precision arithmetic if the computer word length is 64 bits, 2) double-precision arithmetic if the word length is 32 bits.  A Kind option provides flexible control of integer and real precision and range. 1) Each data type has a default computer representation 2) The kind type parameter of a data object determines its computer representation

25 NUMBER REPRESENTATION 3) Each kind is designated by a different integer kind selector value. 4) A kind value may be specified in the type declaration for a variable or named constant.  A typical convention is that the kind selector value is the number of bytes (e.g., 4 or 8) occupied by the computer representation.  But it is recommended that these processor-dependent values not be used as kind selectors because of the problems that result when a program is moved to a different processor.  Therefore use named constants instead of explicit integer constants The intrinsic inquiry function selected_real_kind(P) returns the kind value for a processor representation that supplies at least P decimal digits of precision.

26 NUMBER REPRESENTATION Example: selected_real_kind(6) returns a processor kind value that provides at least 6 decimal digits of precision A numerical constant may be followed by an underscore and a kind selector: 678_SHORT 12_LONG _HIGH The kind selector must be a named constant of integer type EXAMPLES: integer, parameter :: NORMAL = selected_int_kind(9), & LOW = selected_real_kind(6), HIGH = selected_real_kind(15) integer (kind = NORMAL) :: First, Second

27 NUMBER REPRESENTATION real (kind = LOW), parameter :: FOURTH = 12.0 Exercise 3: Do the example 1.4 on page 58 to see the difference in kind selection Meissner’s book.

28 Mixed-mode assignment  Assume that, b is a real variable whose value is 100.0, while c and d are integers having the values 9 and 10, respectively. a = b*c/d result is 90.0 a = c/d*b a gets 0 value. This phenomenon is known as integer division

29 Application program  show program list_directed_input example !  Compile  Run

30 Application program program list_directed_input integer :: int_1, int_2, int_3 real :: real_1, real_2, real_3 ! Initialize all variables int_1 = -1 int_2 = -2 int_3 = -3 real_1 = -1.0 real_2 = -2.0 real_3 = -3.0 ! Read data read *, int_1,real_1,int_2,real_2,int_3,real_3 ! Print new values print *, int_1,real_1,int_2,real_2,int_3,real_3 end program list_directed_input

31 Program style and design A program must be correct, readable, and understandable. The basic principles for developing a good program are as follows: 1) Programs cannot be considered correct until they have been validated using test data. 2) Programs should be well structured Use a top-down approach when developing a program for a complex problem. Strive for simplicity and clarity 3) Each program unit should be documented Include opening documentation Use comments Use meaningful identifiers Label all output 4) A program should be formatted in a style that enhances its readability

32 Program style and design 5) Programs should be readable and understandable Do not use magic numbers Use comments to describe the purpose of a program and variables 6) Programs should be general and flexible