Example in Final Fall 2006 PROGRAM EXAM IMPLICIT NONE INTEGER :: A=3, B=8 REAL :: CALC B = CALC(A, B) A = CALC(A, B) WRITE(*,*) A, B END PROGRAM EXAM a)

Slides:



Advertisements
Similar presentations
CHAPTER 5 INPUT/OUT FORMAT. Introduction READ (*,*) x WRITE (*,*) x This is free format: the first * is for I/O device number (* = input is keyboard *
Advertisements

2-1 Chapter 2.  Coding Requirements of IDENTIFICATION DIVISION  Sections of ENVIRONMENT DIVISION  Assigning Files to Devices in ENVIRONMENT DIVISION.
Fortran 4- Reading and Writing from Data Files Chapter 4 of your Fortran book.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Input and output. Input streams n there are two ways of handling I/o n list-directed –done using default settings –PRINT*, num –READ*, num n formatted.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list.
Formatted Output What we know: write(*,*) x print *, x General Form: write (unit, format, options) list unit = * : write to the monitor format = *: no.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.
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.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Generalized I/O. Mystery of stars I/O statements took the form read *, x, y,z print *, a,b,c Such I/O is said to be List-directed or in free format user.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Computer Math CPS120: Data Representation. Representing Data The computer knows the type of data stored in a particular location from the context in which.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
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.
Computer Science 101 Introduction to Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Why Files? ♦ the amount of data read and / or produced is huge ♦ repetitive data is needed for more than one program ♦ data may be entered by other people.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 05 (Part III) Control Statements: Part II.
INPUT / OUTPUT STATEMENTS
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
21 April, 2000 CS1001 Lecture 22 Formatted Input and Output.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
19 - 2/25/2000AME 150L1 Solving Systems of Linear Equations.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
CSCE Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
28 Formatted Output.
Formatted Input and Output
Programming Fundamentals
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
TMF1414 Introduction to Programming
Chapter 2 - Introduction to C Programming
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Subroutine Comp 208 Yi Lin.
IDENTIFIERS CSC 111.
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.
INPUT & OUTPUT scanf & printf.
Chapter 3 The DATA DIVISION.
Midterm Review Programming in Fortran
Review: Compiler Phases:
C++ Data Types Data Type
CHAPTER 17 The Report Writer Module
FILE PROCESSING Opening Files Why Files?
Introduction to C Programming
Week 10 FILE PROCESSING.
OUTPUT DESIGN PRINT K, expression list K FORMAT (specification list)
Presentation transcript:

Example in Final Fall 2006 PROGRAM EXAM IMPLICIT NONE INTEGER :: A=3, B=8 REAL :: CALC B = CALC(A, B) A = CALC(A, B) WRITE(*,*) A, B END PROGRAM EXAM a) 00 b) 30 c) 03 d) 08 e) None of the above REAL FUNCTION CALC(X, Y) IMPLICIT NONE INTEGER :: X, Y IF(X > Y) THEN Y = Y/X CALC = Y ELSE CALC = Y/X END IF END FUNCTION CALC

Example in Midterm Fall 2006 PROGRAM midterm IMPLICIT NONE INTEGER :: a, b a = 15 b = 5 CALL mysub(a, b) WRITE(*,*) b-a END PROGRAM a) b) c). 1 d) e). None of the above SUBROUTINE mysub(b,a) IMPLICIT NONE INTEGER a, b, i, j IF(a > b) THEN a = (a - 10)**2 b = b*b ELSE a = a*b b = b*b END IF if(a == b) THEN DO i=1, 15 DO j=i, 25-i a = a + i*j b = b - a END DO IF(a /= b) THEN a = 2 b = 3 END IF END SUBROUTINE

Formatting input and output Yi Lin

Why formatting? PROGRAM test IMPLICIT NONE REAL::x=1.1, y= WRITE(*,*) x WRITE(*,*) y END PROGRAM Output: Nicer format:

FORMAT statement Syntax write(*, label) list-of-variables label format format-code Semantics Output list-of-variables according to the format- codes specified on the line labeled.

FORMAT statement, example 1 Example REAL::y= write(*, 900) y 900 format (F10.4) F10.4 means that the number y should be printed using fixed point notation with field width 10 and 4 decimal places. # Width=10 (including “.”, white spaces will be filled on the leftmost side) label format-code Decimal digits=4

FORMAT statement, example 2 Example REAL::x=1.0, y= write(*, 900) x, y 900 format (F3.1, F10.4) F3.1 is for x and F10.4 is for y correspondingly. 1.0# (#: white space) Width should be larger enough! List-of-variables

FORMAT statement, example 4 Example REAL::x=1.0, y= write(*, 900) x, y 900 format (F3.1, F9.4) (F3.1,F9.4): (F3.1,F10.4):1.0# (F3.1,F8.4): 1.0******** *: Width=8 is not wide enough to output y. 4 integer digits + 4 decimal digits + 1 for “.” = 9 digits

Common format codes E.g., 900 format (F10.4) The most common format code letters are: F - real numbers, fixed point format I - integer A - text string E - real numbers, exponent notation X - horizontal skip (space) / - vertical skip (newline)

FORMAT statement, Fw.d The format code F (and similarly D, E) has the general form Fw.d where w is an integer constant denoting the field width and d is an integer constant denoting the number of decimal digits. F10.4 If a number or string does not fill up the entire field width, spaces will be added. Usually the text will be adjusted to the right, but the exact rules vary among the different format codes. w=10 (including “.”) d=4 #

FORMAT statement, Exponent notation E: Exponent notation REAL::y= WRITE(*,100) y 100FORMAT(E10.2) w=10 (including “.”) d=2 ##0.11E+04

FORMAT statement, example 5 For integers only the field width is specified, so the syntax is Iw. Similarly, character strings can be specified as Aw but the field width is often dropped. INTEGER::a=1000 WRITE(*,100) “a=“, a 100FORMAT(A5,I6) WRITE(*,200) “a=“,a 200 FORMAT(A,I4) WRITE(*,300) “a=“,a 300FORMAT(A,I3) ###a=##1000 A5I6 a=*** AI3 a=1000 AI4

FORMAT statement, horizontal skip nX: horizontally skip n spaces INTEGER::a=1000 WRITE(*,100) “a=“, a 100FORMAT(A, 4X, I4) If n is omitted, n=1 100 FORMAT(A,X,I4) a=#1000 AI4X a=####1000 AI44X

FORMAT statement, vertical skip n/: vertically skip n lines INTEGER::a=1000 WRITE(*,100) “a=“, a 100FORMAT(A, 2/, I4) If n is omitted, n=1 a= # 1000 A I4 2/

FORMAT statement, repeating nIw: = repeat Iw n times INEGER::a=1, b=10, c=100 WRITE(*,100) a,b,c 100 FORMAT(3I4) ###1##10#100 I4 And similarly nFw.d

FORMAT statement, repeating (cont.) n(format-codes): = repeat format-codes n times FORMAT(3(I4,F10.4)) Is equivalent to FORMAT(I4,F10.4,I4,F10.4,I4,F10.4)

FORMAT statement, Simplified form Format strings in read/write statements write (*,”(A, F8.3)”) “The answer is x = “, x is equivalent to write (*,990) “The answer is x = “, x 990 format (A, F8.3) Sometimes text strings are given in the format statements, e.g. the following version is also equivalent: write (*,999) x 999 format (“The answer is x = “, F8.3)

FORMAT statement, READ All of these format-codes are also applied to READ statement. INTEGER::a,b READ(*,*) a,b From the keyboard, we just need to type 1,2 (or 1#2) ! a=1, b=2 But with formatting input, we must be careful about the number of spaces input.

FORMAT statement, READ Example INTEGER::a,b READ(*,100) a,b 100 FORMAT(2I3) ! eqv. To FORMAT(I3,I3) If inputting “1,2”, the computer will take the first 3 characters “1,2” and assign them to a.  Runtime error! “1,2” not an integer If input “##1, 2”, a=##1. But “,2###” will be assigned to b.  Runtime error!

FORMAT statement, READ Correct inputs for (2I3), e.g., “##1##2”a=##1, b=##2 “1##2##”a=1##, b=2## “#1##2#”a=#1#, b=#2# What if “#1#22222”? a=#1#, b=222

FORMAT statement, READ Correct inputs for READ(*, “(F5.1)”) x? “##3.4”  x=3.4 “ ”  x=123.4 “12345”  x= (take the leftmost 5 digits first, then assign the last digit as decimal part and the leftmost 4 digits as integer part)

Format Read Example INTEGER::a, b,c READ(*,100) a,b,c 100 FORMAT(I3,x,I2,2x,I4) Input: A=123 B=56 C=9

File input/output READ(*,*)/WRITE(*,*) only reads from/writes to standard input(e.g., keyboard)/output(screen). Files: a data storage unit in hard disks. E.g., HelloWorld.f90 E.g., studentRecords.txt E.g., experimentalData.txt We need to read data from existing files and/or write data to files.

File input/output Three steps to use a file 1. Open a file 2. Input/output using READ and WRITE – READ: read data from the opened file – WRITE: write data to the opened file 3. Close the file ( A file that has not been closed can usually not be read. )

File input/output, OPEN To open a file, syntax: OPEN ([olist] ) where, olist is a list of keyword clauses separated by “,”: keyword "=" value {"," keyword "=" value} Example: OPEN(UNIT=10, FILE=“expData.txt”)

File input/output, OPEN Important keywords: [UNIT=] u! u is a unique number to identify the file FILE= fname! File name Some other keywords (not required in course materials) STATUS, ERR, ISOTAT, ACCESS, FORM, RECL, POSITION

File input/output, OPEN [UNIT=]u OPEN(10, “expData.txt”) OPEN(UNIT=10, “expData.txt”) Unit number (i.e., u) is an integer from 1-99, with some reserved: 5: standard input READ(*,*) == READ(5,*) 6: standard output WRITE(*,*) == WRITE(6,*)

FILE input/output, READ/WRITE READ READ(unit, label) list-of-variables labelformat(format-codes) or READ(unit, *) … WRITE WRITE(unit, label) list-of-variables labelformat(format-codes) or WRITE(unit, *) …

FILE input/output, CLOSE A file that has not been closed can usually not be read. Syntax: CLOSE ([UNIT=]u) For example: CLOSE (10) CLOSE (UNIT=10)

FILE input/output, Example ! Input 10 integers from keyboard and write them to file “inputData.txt” PROGRAM fileTest IMPLICIT NONE INTEGER::count, a OPEN(UNIT=10,FILE=“inputData.txt”) ! Open file “inputData.txt” DO count=1,10 WRITE(*,*) “Input an integer number from keyboard:” READ(*,*) a WRITE(10,100) “a=“, a ! Write to “inputData.txt” END DO CLOSE(10);! Close file “inputData.txt” 100 FORMAT(A2, I8) END PROGRAM a=######51 a=#######6 … inputData.txt

Example in Midterm Fall 2006 program mid implicit none integer :: i character*1 :: a(4) read(*,7) (a(i), i = 1,4) 7 format (4(A1,X)) write(*,8) (a(i), i = 4,1,-1) 8 format (4A1) end program 1. There will be an error message because the input string is too long 2. pmoc 3. iumc 4. cmui 5. None of the above.