ME1107 Computing Y Yan.

Similar documents
Lecture 2 FORTRAN Basics. Lubna Ahmed

FORTRAN Basis. PROGRAM LAYOUT PROGRAM program name IMPLICIT NONE [declaration statements] [executable statements] END PROGRAM [program name]

Numerical Modelling in Fortran: day 2. Paul Tackley, 2017

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

C Programming

Our Strategy for Learning Fortran 90

CS-201 Introduction to Programming with Java

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

Introduction to Engineering gii

City University School of Engineering and Mathematical Sciences Common part 1 COMPUTING. Fortran Lecture 1.1

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

2. Numbers In, Numbers Out

Program Structure and Format

Computer Programming in MATLAB

Dr Richard Greenaway

Introduction to Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

1001ICT Introduction To Programming Lecture Notes

2. Numbers In, Numbers Out

Data Types and Basic Calculation

Introduction to Functions. Functions. Library Functions. Intrinsic Functions. Library Functions. Chapter 6 Fall 2015, CSUS. Chapter 6.

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

2 Making Decisions. Store the value 3 in memory location y

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Introduction to FORTRAN

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

PIV Programming. Today s Contents: 1. Matlab Programming 2. An example of PIV in Matlab code 3. EDPIV 4. PIV plugin for ImageJ 5.

ANSI C Programming Simple Programs

VBScript: Math Functions

VARIABLES Storing numbers:

Table 2 1. F90/95 Data Types and Pointer Attributes. Data Option. (Default Precision) Selected-Int-Kind

Section 2.2 Your First Program in Java: Printing a Line of Text

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Fortran. (FORmula TRANslator) History

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Chapter 2. Outline. Simple C++ Programs

Intrinsic Functions Outline

AN INTRODUCTION TO MATLAB

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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

Visual C# Instructor s Manual Table of Contents

1.1 Numbers system :-

LAB 1 General MATLAB Information 1

Fortran 90 Two Commonly Used Statements

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

Methods CSC 121 Fall 2016 Howard Rosenthal

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

Outline. Data and Operations. Data Types. Integral Types

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

A First Program - Greeting.cpp

The Fortran Basics. Handout Two February 10, 2006

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

C Functions. 5.2 Program Modules in C

Matlab as a calculator

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

10 Using the PCFL Editor In this chapter

PROBLEM SOLVING WITH FORTRAN 90

LECTURE 0: Introduction and Background

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Pace University. Fundamental Concepts of CS121 1

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Multiple Choice Questions ( 1 mark)

MATLAB Constants, Variables & Expression. 9/12/2015 By: Nafees Ahmed

Mentor Graphics Predefined Packages

Introduction to MATLAB Programming

IDL Primer - Week 1 John Rausch

C++ Support Classes (Data and Variables)

THE FUNDAMENTAL DATA TYPES

Goals for This Lecture:

ELEMENTARY MATLAB PROGRAMMING

Full file at

CS110: PROGRAMMING LANGUAGE I

MATLAB INTRODUCTION. Matlab can be used interactively as a super hand calculator, or, more powerfully, run using scripts (i.e., programs).

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Basic types and definitions. Chapter 3 of Thompson

Methods CSC 121 Fall 2014 Howard Rosenthal

DEPARTMENT OF MATHS, MJ COLLEGE

Computers Programming Course 5. Iulian Năstac

Full file at

Methods CSC 121 Spring 2017 Howard Rosenthal

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Built-in Types of Data

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

A very brief Matlab introduction

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

Introduction to C# Applications

Methods: A Deeper Look

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Chapter 1. Numeric Artifacts. 1.1 Introduction

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

AMS 27L LAB #1 Winter 2009

8/16/12. Computer Organization. Architecture. Computer Organization. Computer Basics

Transcription:

ME1107 Computing 1 2008-2009 Y Yan http://www.staff.city.ac.uk/~ensyy

About Fortran Fortran Formula Translation High level computer language Basic, Fortran, C, C++, Java, C#, (Matlab) What do we learn? basic programming skills self-learning skills References Text book (Fortran 77 for Engineers) Fortran95 Manual (course website) 2

Creating Executable Code Source code ( text ) File1.f95 object code ( binary ) File1.obj Executable code File2.f95 File2.obj Prog1.exe compiling linking libraries Source code: text file you write Executable code: binary code 3

Fortran Programming Environment Silverfrost Plato, Integrated Development Environment (IDE), is used for Fortran programming Create a folder for Fortran programs Start Fortran IDE Start -> programs -> F. Departmental software -> B. Engineering & mathematics -> Plato IDE Open a new file: Free Format Fortran File 4

Exercise 1: Your first Fortran program!! Your name, Group! Date!! Hello World Program Prog1 Print *, Hello World Print *, your name, group Print *, email address Print *, Personal tutor s name End Prog1 Compile: to create.obj Build : to create.exe Start: to run.exe DOS window Program output 5

Data types In Fortran there are 5 intrinsic (i.e. built-in) data types: Integer: whole numbers 1 bit 0/1 1 byte 8 bits For n bits, the integer number range is (-2 n-1, 2 n-1-1). 2-byte integer: -32768 to 32767 Real: numbers have a decimal point Finite machine precision Real 4 byte (seven significant digits and be within the magnitude range of 10-38 to 10 +38 ) Double 8 byte Complex: real and imaginary parts Character: strings of characters 1 character 1 byte ASCII characters All letters and symbols on the keyboard mapped with numbers 0-127 (e.g. a 97, A 65) Logical:.TRUE. or.false. The first three are the numeric types. The last two are non-numeric types. 6

Variable types Declaration of variables are not compulsory in the Fortran language Without declarations I, J, K, L, M, N, O are integer type (Don t use O, it will be confused with 0.) the rest are real type Declaration of variables are compulsory in coursework IMPLICITY NONE (must be included in program) which means that all variables must be declared 7

Declaration of Variables Integer integer i, j, k integer :: n = 0 Real real a, b, c real x, y, z real :: sum = 0 Complex Char logical! initialised! initialised FORTRAN is not case sensitive. (e.g. I is same as i ) 8

Output statement The syntax of output statement: WRITE (output_device, format[, options]) list PRINT format, list (standard output device, usually the screen) Print *, Hello World Print *, your name Print *, Input A, B, C Print *, Roots are, ROOT1, ROOT2 Write(*,*) Roots are, ROOT1, ROOT2 Text is put between or Variable in the write list: values in variables are printed (not variable names). 9

The syntax of input statement: Input statement READ (input_device, format[, options]) list_of_variables READ format, list_of_variables (standard input device, usually the keyboard) Print *, Input A, B, C Read *, A, B, C! A prompt before input Read(*,*) A, B, C 10

Numerical operators: +, -, *, /, ** Operators and intrinsic functions Logical operators: > (.gt.) >= (.ge.) < (.lt.) <= (.le.) == (.eq.) /= (.ne.) Intrinsic functions: Full list is on page 35 of Fortran95 Manual Some examples: COS( X ), SIN( X ), TAN( X ) trigonometric functions (arguments are in radians) EXP( X ), LOG( X ), LOG10( X ) exponential and logarithmic functions SQRT( X ) square root ABS( X ) absolute value (integer, real or complex) 11

Type Conversion When a binary operator has operands of different type, the weaker (usually integer) type is converted to the stronger (usually real) type and the result is of the stronger type. e.g. 3 / 10.0 to 3.0 /10.0 = 0.3 If an integer is divided by an integer then the result must be an integer and is obtained by truncation towards zero. 3 / 10 = 0 If you intend a constant to be a real number, use a decimal point! 12

Line discipline The usual layout of statements is one-per-line, interspersed with blank lines for clarity. There may be more than one statement per line, separated by a semicolon; e.g. A = 1; B = 10; C = 100 This is only recommended for simple initialisation. Each statement may run onto one or more continuation lines if there is an ampersand (&) at the end of the line to be continued. e.g. DEGREES = RADIANS * PI & / 180.0 13

Exercise 2: Run the quadratic equation solver 14

Exercise 2: Run the quadratic equation solver Program structure Comment lines Implicit None Declare variables Output: print Input: read Calculations: = one variable on the left side variables on the right side much have values IF statement 15

Exercise 3. Write a program for this flowchart start Input deg Calculate rad deg and rad are two variables, which are the names given to two memory locations for storing data. Output rad end 16