FORTRAN PROGRAMMING BASICS (2) MET 50. Programming Basics A few extra things from last week… 1. For a program written in Fortran 90, use the extension.

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

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
1 Chapter 2 Basic Elements of Fortran Programming.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
MORE ON… MAKING YOUR OUTPUT LOOK NICE! MET 50. Pretty output Recall the more sophisticated PRINT statement: PRINT nn, GRAV nn FORMAT (stuff) We use the.
MAKING YOUR OUTPUT LOOK NICE! MET 50. The “IF” statement Suppose we have the statements: REAL, parameter :: GRAV = 9.81 PRINT*, ‘Value of GRAV is:’,GRAV.
Chapter 2 Basic Elements of Fortan
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
CS 201 Functions Debzani Deb.
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
FORTRAN PROGRAMMING BASICS MET 50. Programming Basics The basic layout of all programs is as follows (p.33) PROGRAM name = heading (i.e., title) Specifications.
Copyright © Cengage Learning. All rights reserved. Real Numbers and Their Basic Properties 1.
Computer Science 101 Introduction to Programming.
Visual Basic Chapter 1 Mr. Wangler.
Any questions on today’s homework? (Sections 1.6/1.7) Reminder: You should be doing this homework without using a calculator, because calculators can’t.
Python Programming Fundamentals
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Introduction to Python
Fall, 2006Introduction to FORTRAN1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman Fall, 2006.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 9, 2015)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Computer Science 101 Introduction to Programming.
Documentation and Comments. What’s a comment? A comment is a simple form of documentation. Documentation is text that you the programmer write to explain.
Input, Output, and Processing
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
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.
Lecture 41 Arithmetic Clauses The ROUNDED clause –Place ROUNDED after the variable that holds the result ADD A TO B ROUNDED. ADD A TO B GIVING C ROUNDED.
Section 2.1 Solving Equations Using Properties of Equality.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
CSCE 102 – Chapter 11 (Performing Calculations) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
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.
Any questions on today’s homework? (Sections 1.6/1.7) Reminder: You should be doing this homework without using a calculator, because calculators can’t.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
The Hashemite University Computer Engineering Department
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CSCE Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.
Starter What does the following code do?
Visual Basic.NET Windows Programming
Section I: Distributive Property Section II: Order of Operations
Topics Designing a Program Input, Processing, and Output
User-Written Functions
Chapter 2 Introduction to C++ Programming
User input We’ve seen how to use the standard output buffer
Introduction to Programming
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Arithmetic Operator Operation Example + addition x + y
More Selections BIS1523 – Lecture 9.
Unit 2 Programming.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
MSIS 655 Advanced Business Applications Programming
Introduction to Programming Using Python PART 2
Core Objects, Variables, Input, and Output
Introduction to Programming with Python
Topics Designing a Program Input, Processing, and Output
Week 1 Real Numbers and Their Properties
Topics Designing a Program Input, Processing, and Output
listing. Where is the variable
Presentation transcript:

FORTRAN PROGRAMMING BASICS (2) MET 50

Programming Basics A few extra things from last week… 1. For a program written in Fortran 90, use the extension “.f90”  This tells the compiler: “Hey – this is a Fortran 90 code”.  *.f might be interpreted at Fortran 77. 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 2. When writing a number like: E6, always write this as:  (real number) E (integer)  So 6.371E6 is OK  But 6.371E1.5 is not OK. 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 3. Real versus Integer: it is dangerous to mix real and integer variables in Fortran Example: REAL :: A=10.0, B=4.0, C INTEGER :: K=4, L C = A/B ! Produces C = 10./4. = 2.5 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics REAL :: A=10.0, B=4.0, C, D INTEGER :: K=10, L=4, KL C = A/B ! Produces C = 10./4. = 2.5 KL = K/L! Produces KL = 10/4 = 2 ! Rounded down to nearest integer D = A/L ! A/L = 10.0/4 = 2.5 ! A/L is treated as REAL ! D is REAL and has value 2.5 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics Example: REAL :: A=2.0, B=4.0, C INTEGER :: K=4, L C = A**(3/2) ! 3/2 = 1 (INTEGER!) ! C = A**1 = A = 2.0 ! but (2)**(3/2) = SQRT(2**3) =  8 So…you get an error… Should be: C=A**(3./2.) 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 4. Order of operations in Fortran: a. Stuff inside parentheses is done first. b. Inside parentheses, the order is: c. Exponentiation (A**2) – right  left  A**2**3 is computed as  A**(2**3)=A**8 d. Multiplication & division: right  left e. Addition & subtraction: right  left 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics Example: X = SQRT(B**2 – 4.0*A*C) 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics Page 25, Q 10. ((2 + 3)**2) / (8 - (2 + 1)) =((5)**2) / (8 - (3)) = (5**2) / (5) = (25) / (5) = 5 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics But…caution about parentheses… ((2 + 3)**2) / (8 – (2 + 1)) = (5**2) / (7) = (25) / (7) 55 Parentheses matter!!! 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics And… ((2 + 3)**2) / (8 – (2 + 1) Will not run! Why?? Parentheses really matter!!! 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 4. PRINT*, READ* statements: We have met the PRINT* statement. a. PRINT*, VAR prints the value of “VAR” b. PRINT*, VAR, TAR prints the values of “VAR” and “TAR” c. Quantities are printed to the screen (only)…”hardcopy”? 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics Results may be printed “ugly”, such as: We can make things a bit nicer, as in: PRINT*, ‘value of VAR is’, VAR, ‘value of TAR is’, TAR Would  value of VAR is value of TAR is /8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics We can make things better still using the WRITE command (Chapter 5)(or sooner!) READ* statement is the simplest way to input data to a program. In lab-02, you ran “add2.f” to add 2 numbers. The code prompted you for two numbers. How? Using the READ* statement. 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics Example: REAL :: A, B, C READ*, A, B C=A*B PRINT*, A, B, C As this code runs, it will stop – waiting for YOU to enter values of A and B at the “READ” command. Codes in this class will run FAST (since they are very small), so if the code stops, it is either expecting input – or it’s broken  9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics To help you see what is going on in your code, it is good practice to add some PRINT statements: PRINT*, ‘enter values for A and B’ READ*, A, B Or: PRINT*, ‘enter first number’ READ*, A PRINT*, ‘enter second number’ READ*, A 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics This is a style thing! There is a more powerful READ statement – Cht. 5 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 5. Comment statements: Comment statements are vital! Use to explain what this section of code does. ! top of code what the program does! ! Read in parameter values ! Main computation ! Write results 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 5. Next lecture? “selective execution” Fortran equivalent to: “IF it’s Sunday, sleep in. ELSE, set alarm.” 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2

Programming Basics 6. Next lab? Practice finding problems with REAL and INTEGER numbers mixed. READ* and PRINT* statements 9/8/ MET 50, FALL 2011, CHAPTER 2 PART 2