Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Introduction to FORTRAN-90 University of Liverpool course.
CPS120: Introduction to Computer Science Lecture 8.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Fall, 2006Introduction to FORTRAN1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman Fall, 2006.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0.
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Week 1 Algorithmization and Programming Languages.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Building Java Programs
ITEC113 Algorithms and Programming Techniques
Revision Lecture
Variables, Expressions, and IO
Java Programming: From Problem Analysis to Program Design, 4e
OPERATORS (1) CSC 111.
Lecture 3 Expressions Richard Gesick.
Lecture II Basic computer and programming concepts
Introduction to C++ Programming
Midterm Review Programming in Fortran
Chapter 2: Basic Elements of Java
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Summary of what we learned yesterday
Building Java Programs
Primitive Types and Expressions
Building Java Programs Chapter 2
Building Java Programs
DATA TYPES AND OPERATIONS
Presentation transcript:

Lecture III Start programming in Fortran Yi Lin Jan 11, 2007

A simple example: Roots finding Finding roots for quadratic aX^2+bX+c=0 Input a, b, c Output root(s): (-b + SQRT(b*b - 4*a*c))/(2*a) (-b - SQRT(b*b - 4*a*c))/(2*a)

Roots finding (cont.) start Initialize a=1, b=-5, c=6 w = (b*b – 4*a*c) R1 = (-b + sqrt(w))/(2*a) R2 = (-b - sqrt(w))/(2*a) End

Roots finding (cont.) PROGRAM ROOTSFINDING INTEGER a, b, c REAL x, y, z, w, r1, r2 a=1 b=-5 c=6 ! To calculate b*b – 4*a*c x = b*b y = a*c z = 4*y w = x-z r1 = (-b +SQRT(w))/(2*a) r2 = (-b – SQRT(w))/(2*a) WRITE(*, *) r1, r2 END PROGRAM Declare variables, Must at the head of block Statements: initialize variables with values statements: calculation statements:Output results

Roots finding (cont.) PROGRAM ROOTSFINDING INTEGER a, b, c REAL x, y, z, w, r1, r2 a=1 b=-5 c=6 ! To calculate b*b – 4*a*c x = b*b y = a*c z = 4*y w = x-z r1 = (-b +SQRT(w))/(2*a) r2 = (-b –SQRT(w))/(2*a) WRITE(*, *) r1, r2 END PROGRAM variables constants operators Keywords in Fortran90

Variables and constants Constants (e.g., 1, -5, 6, 4, 2) A variable (e.g., a, b, c, x, y, z, w) is a unique name which a FORTRAN program applies to a word of memory and uses to refer to it. Naming convention alphanumeric characters (letters, numerals and the underscore character) The first character must not be a letter. No case sensitivity Don’t use keywords in Fortran as variables’ names

Variable data types INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: Data type is important INTEGER::a=1,b=-5,c=6 REAL::r1, r2 IMPLICIT NONE needed to prevent errors. Comment out “IMPLICIT NONE” write(*, *) r1, r3 ! You want to print r1 and r2, but by mistakes you type r3

Declarations PROGRAM RootFinding IMPLICIT NONE INTEGER :: a, b, c REAL :: x,y,z,w REAL :: r1, r2... END PROGRAM RootFinding Declarations tell the compiler To allocate space in memory for a variable What “shape” the memory cell should be (i.e. what type of value is to be placed there) What name we will use to refer to that cell

Declare variables INTEGER a, b, c REAL x, y, z, w A=1 B=-5 C=6 INTEGER:: a=1, b=-5, c=6 REAL:: x, y, z, w same

Variable precision INTEGER(KIND=2)::a OR INTEGER(2)::a KINDBITsValue range 18-2**7 <= a < +2**7 (i.e., [-128,127]) 216-2**15 <= a < +2**15 (i.e., [-32768,-32767]) 4(default)32-2**31 <= a < +2** **63 <= a < +2** byte = 8 bits

Example program test implicit none integer(1)::a=127 integer(2)::b=128 write(*,*) "a=", a, “b=“,b End PROGRAM Output a=127 b=-128

Variable precision (cont.) REAL(KIND=4) CHARACTER KIND=1 only KINDBITs 4(default)32 8(equivalent to DOUBLE PRECISION) *16

Arithmetic Expressions An arithmetic expression is formed using the operations: + (addition), - (subtraction) * (multiplication), / (division) ** (exponentiation) If the operands are integers, the result will be an integer value If the operands are real, the result will be a real value If the operands are of different types, the expression is called mixed mode.

Arithmetic expressions Variable operator variable operator …. E.g., *3 E.g., b**2 – 4*a*c It has a value by itself E.g. The value of expression 5 + 2*3 is 11

Simple Expressions > > * 8 --> / > /4.2 --> 2.0 rather than 2, since the result must be of REAL type. -5**2 --> /4 --> 3 13/4 --> 3 rather than Since the operands are of INTEGER type, so is the result. The computer will truncate the mathematical result to make it an integer. 3/5 --> 0 rather than 0.6.

Evaluating Complex Expressions Evaluate operators in order of precedence. First evaluate operators of higher precedence 3 * 4 – 5  * 5  23 For operators of the same precedence, use associativity. Exponentiation is right associative, all others are left associative Expressions within parentheses are evaluated first

Arithmetic operators: precedence 2+3*4 = ? = 5 * 4 = 20 Or = 2+12 = 14 (2+3)*4 = 20 operatorsPrecedence ()1 **2 *, /3 +, -4

Arithmetic operators: associativity operatorsassociativity ()Left to right **Right to left *, /Left to right +, -Left to right associativity resolves the order of operations when two operators of the same precedence compete for three operands: 2**3**4 = 2**(3**4) = 2**81, 72/12/ 3 = (72/12)/3 = 6/3 = 2 30/5*3 = (30/5)*3 = 18 if *,/ associativity is from right to left 30/5*3 = 30/(5*3) = 2

Examples 2 * 4 * 5 / 3 ** 2 --> 2 * 4 * 5 / [3 ** 2] --> 2 * 4 * 5 / 9 --> [2 * 4] * 5 / 9 --> 8 * 5 / 9 --> [8 * 5] / 9 --> 40 / 9 --> 4 The result is 4 rather than since the operands are all integers.

Examples ( / 100) ** 3 --> (1 + [250 / 100]) ** 3 --> (1 + 2) ** 3 --> ([1 + 2]) ** 3 --> ** 3 --> [3 ** 3] --> > 127 Parentheses are evaluated first

Examples * 3.0 / ( 6.0* *44.0) ** > [2.0 * 3.0] / (6.0* *44.0) ** > / (6.0* *55.0) ** > / ([6.0*6.0] + 5.0*44.0) ** > / ( *44.0) ** > / ( [5.0*44.0]) ** > / ( ) ** > / ([ ]) ** > / ** > / [256.0 ** 0.25] --> / > [6.0 / 4.0] --> > 2.5

Mixed Mode Expressions If one operand of an arithmetic operator is INTEGER and the other is REAL the INTEGER value is converted to REAL the operation is performed the result is REAL  3.5 1/2.0  /8  **2.0  **(1/2)  1.0 (since 1/2  0)

Example of Mixed Mode 25.0 ** 1 / 2 * 3.5 ** (1 / 3)  25.0 ** 1 / 2 * 3.5 ** (1 / 3)  25.0 ** 1 / 2 * 3.5 ** ([1 / 3])  [25.0 ** 1] / 2 * 3.5 ** 0  25.0 / 2 * [3.5 ** 0]  25.0 / 2 * 1.0  [25.0 / 2] * 1.0  12.5 * 1.0  12.5

Statements Assignment statement: Variable = expression e.g., b = -5 (never the other way around, show an example) General statement: INTEGER a, b, c write (*,*) ‘Hello world!’

Assignment Statement The assignment statement has syntax: variable = expression Semantics 1. Evaluate the expression 2. If the type of result is the same as the type of the variable store the result in the variable 3. Otherwise convert the value to the type of the variable and then store it – If the value is REAL and the variable is INTEGER remove the decimal part (truncate) – If the value is INTEGER and the variable is REAL convert the value to a decimal 4. The original value of the variable is destroyed

Assignment statements examples REAL::X=3.5 X=2 * 2.4 ! X=4.8 INTEGER X=3 X=2 * 2.4 ! X=4 X=2 * 2.6 ! X=5

Roots finding revisited PROGRAM ROOTSFINDING INTEGER::a=1, b=-5, c=6 REAL x, r1, r2 ! To calculate b*b – 4*a*c x = b**2- 4*a*c r1 = (-b +SQRT(x))/(2*a) r2 = (-b – SQRT(x))/(2*a) WRITE(*, *) r1, r2 END PROGRAM Declare variables, Must at the head of block statements: calculation statements:Output results

Example in a previous final PROGRAM Q1 IMPLICIT NONE INTEGER :: I, J, K REAL :: A, B, C A = B = 22/7 C = 22.0/7.0 I = MOD(78,5) J = 45/3*3+1-3*4 K = C WRITE(*,*) I,J,K,A,B,C END PROGRAM Q1 a) b) c) d)

Example in midterm05 What does the following program output? PROGRAM midterm REAL A,B,C INTEGER I,J,K A = 3.5 I = A J = 5.25 K = I*2 B = A*I C = J/3 WRITE (*,*) A,B,C,I,J,K END PROGRAM midterm A B C D E. None of the above