OUTPUT DESIGN PRINT K, expression list K FORMAT (specification list)

Slides:



Advertisements
Similar presentations
The "if structure" is used to execute statement(s) only if the given condition is satisfied.
Advertisements

Input and Output READ WRITE OPEN. FORMAT statement Format statements allow you to control how data are read or written. Some simple examples: Int=2; real=
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
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.
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.
1 Chapter 2: Data Types & Operations Part 1 ICS101: Computer Programming Al-Hashim, Amin G.
Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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(,,,..., );
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)
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
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.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
INPUT / OUTPUT STATEMENTS
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
21 April, 2000 CS1001 Lecture 22 Formatted Input and Output.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
19 - 2/25/2000AME 150L1 Solving Systems of Linear Equations.
 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.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CSCE Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 4 – C Program Control
Chapter Topics The Basics of a C++ Program Data Types
Introduction to C++ Programming
Line Continuation, Output Formatting, and Decision Structures
Building Java Programs
Wel come.
Chapter 2, Part I Introduction to C Programming
Basic Elements of C++.
Primitive Data, Variables, Loops (Maybe)
Introduction to Scripting
Chapter 4: Making Decisions.
Basic Elements of C++ Chapter 2.
Chapter 2 – Getting Started
Line Continuation, Output Formatting, and Decision Structures
Introduction to Python
Introduction to C++ Programming
Building Java Programs
Building Java Programs
The University of Texas – Pan American
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
elementary programming
Building Java Programs
Building Java Programs
Building Java Programs
FILE PROCESSING Opening Files Why Files?
Building Java Programs
Building Java Programs
Building Java Programs
Repetition Structures
Building Java Programs
DATA TYPES AND OPERATIONS
Administrative things
Building Java Programs
REPETITION Why Repetition?
Presentation transcript:

OUTPUT DESIGN PRINT K, expression list K FORMAT (specification list) The following are some of the carriage control characters used to control the vertical spacing:   ' ' : single spacing (start printing at the next line) ' 0 ' : double spacing (skip one line then start printing) ' - ' : triple spacing (skip 2 lines then start printing) ' 1 ' : new page (move to the top of the next page before printing) ' + ' : no vertical spacing (start printing at the beginning of the current line irrespective of what was printed before)

* I Specification ( Iw ) for integer numbers Example : Assume K = -244 and M = 12 . What will be printed after the execution of the following print statements ?   PRINT 10, K 10 FORMAT(' ', I4) PRINT 20, K, M 20 FORMAT(' ', I5, I6) PRINT 30, K PRINT 40, M 30 FORMAT(' ', I3) 40 FORMAT('0', I2) PRINT 50, K + M 50 FORMAT(' ', I5) - 2 4 - 2 4 1 2 * 1 2 - 2 3

Error Message Type Mismatch PRINT 60, M + 1.0 60 FORMAT(' ', I3) PRINT 70, K PRINT 70, M 70 FORMAT(' ', I4) PRINT 80, K PRINT 90, M 80 FORMAT(' ', I4) 90 FORMAT('+', I8) PRINT 99, N 99 FORMAT(' ', I3)   Error Message Type Mismatch - 2 4 1 2 - 2 4 1 2 ?

* F Specification ( Fw.d ) for real numbers Error Message Example : Assume X = -366.126, Y = 6.0 and Z = 20.97. What will be printed after the execution of the following print statements ? PRINT 10, X 10 FORMAT(' ', F11.5)   PRINT 20, X FORMAT(' ', F8.2) PRINT 30, X 30 FORMAT(' ', F7.3)  PRINT 40, Z 40 FORMAT('+', I5) . - 3 6 1 2 6 . - 3 6 1 3 . * Error Message Type Mismatch

X Specification ( nX ) to insert blanks   Example : The following program: REAL A, B A = -3.62 B = 12.5 PRINT 5, A, B 5 FORMAT(' ', F5.2, F4.1) END Prints the following output If we modify the FORMAT statement using X Specification as follows: 5 FORMAT(' ', F5.2, 3X, F4.1) The output - 3 . 6 2 1 2 . 5 - 3 . 6 2 1 2 . 5

The X specification can be used as a carriage control character. The following pairs of format statements print the same output   10 FORMAT(' ', I2) is equivalent to 10 FORMAT(1X, I2) - - - - - - - - - 20 FORMAT(' ', 2X, F4.1) 20     FORMAT(3X, F4.1)

Exercises REAL X INTEGER J, K, N What will be printed by the following program? REAL X X = 123.8367 PRINT 10, X, X, X 10 FORMAT(' ', F7.1, 2X, F6.2, F9.5) END 1 2 3 . 8 1 2 3 . 8 4 1 2 3 . 8 6 7 What will be printed by the following program? INTEGER J, K, N K = 123 J = 456 N = 789 PRINT 10, N PRINT 11, J PRINT 12, K FORMAT(' ', 6X, I3) FORMAT('+', 3X, I3) 12 FORMAT('+', I3) END 1 2 3 4 5 6 7 8 9

Literal Specification string in a format statement   Example : What will be printed by the following program? REAL AVG AVG = 65.2 PRINT 5, AVG 5 FORMAT(' ' ,'THE AVERAGE IS ', F4.1) END The output: THE AVERAGE IS 6 5 . 2

A Specification ( Aw ) for character expressions Example1 : What will be printed by the following program? CHARACTER TEXT*5 TEXT = 'KFUPM' PRINT 55, TEXT, TEXT, TEXT 55 FORMAT(' ', A, 3X, A3, 3X, A9) END w may be omitted. If w is omitted, the number of characters will be determined by the length of the character string if the string has more characters than the w positions: print the left- most w characters if the string has fewer characters than the w positions: print the string right-justified K F U P M K F U K F U P M

Example2 : What will be printed by the following program? CHARACTER TEXT*7 TEXT = 'KFUPM' PRINT 66, TEXT, TEXT, TEXT 66 FORMAT(' ', A, 3X, A3, 3X, A9) END K F U P M K F U K F U P M Example3 : What will be printed by the following program? CHARACTER TEXT*4 TEXT = 'KFUPM' PRINT 77, TEXT, TEXT, TEXT 77 FORMAT(' ', A, 3X, A3, 3X, A9) END K F U P K F U K F U P

L Specification ( Lw ) for logical expressions Example1 : PRINT 10, .TRUE. 10 FORMAT(' ', L4)   END T Example2 : PRINT 20, .FALSE. , .TRUE. 20 FORMAT (' ', L2 , 3X , L3)   END F T

* * Example: LOGICAL M REAL X, Y CHARACTER TEXT*8 K = 346 X = 56.352 INTEGER K LOGICAL M REAL X, Y CHARACTER TEXT*8 K = 346 X = 56.352 Y = -24.628 M = .NOT. .TRUE. TEXT = 'FORTRAN' PRINT 10, X, Y PRINT 20, Y, K PRINT 30, TEXT, TEXT PRINT 40, K PRINT 50, M , .TRUE. , 'FINAL' 10 FORMAT(' ', 2X, 'X=', F7.4, 3X, 'Y=', F5.1) 20 FORMAT(' ', 3X, 'Y=', F6.2, 2X, 'K=', I3) 30 FORMAT('0', 9X, A, 1X, A2) 40 FORMAT('+', 4X, 'K=', I2) 50 FORMAT(4X, 'M=', L4 , 2X , L3 , 1X, A) END X = 5 6 · 3 5 2 0 Y = - 2 4 · 6 Y = - 2 4 · 6 3 K = 3 4 6 K = * * F O R T R A N F O M = F T F I N A L