Chapter 2 CREATING DATA FIELDS. SYS-ED/ Computer Education Techniques, Inc.

Similar documents
DECLARING 2 DATA ITEMS AND REPRESENTATION

Chapter 1 INTRODUCTION. SYS-ED/ Computer Education Techniques, Inc.

INTRODUCTION 1 AND REVIEW

DEPARTMENT OF MATHS, MJ COLLEGE

Computer System and programming in C

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

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

UNIT 7A Data Representation: Numbers and Text. Digital Data

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

The type of all data used in a C++ program must be specified

Introduction PL/1 Programming

Data Storage and Query Answering. Data Storage and Disk Structure (4)

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,...

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Characters, Strings, and Floats

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

MACHINE LEVEL REPRESENTATION OF DATA

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

IBM 370 Basic Data Types

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

The C++ Language. Arizona State University 1

Java Notes. 10th ICSE. Saravanan Ganesh

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Programming in C++ 6. Floating point data types

M1 Computers and Data

Final Labs and Tutors

Number System. Introduction. Decimal Numbers

Unit 3. Constants and Expressions

unused unused unused unused unused unused

Programming Using C Homework 4

Creating a procedural computer program using COBOL Level 2 Notes for City & Guilds 7540 Unit 005

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Chapter 4. Operations on Data

CHW 261: Logic Design

Visual C# Instructor s Manual Table of Contents

Introduction to Computer Science-103. Midterm

Chapter 2. Data Representation in Computer Systems

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

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

BASIC ELEMENTS OF A COMPUTER PROGRAM

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Differentiate Between Keywords and Identifiers

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

Computer Organization and Assembly Language. Lab Session 4

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

Number Systems, Scalar Types, and Input and Output

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

EE 109 Unit 19. IEEE 754 Floating Point Representation Floating Point Arithmetic

DEFINING DATA CONSTANTS AND SYMBOLS

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

3 Data Storage 3.1. Foundations of Computer Science Cengage Learning

VARIABLES AND CONSTANTS

LANGUAGE CONSTRUCTS AND CONVENTIONS IN VERILOG

Signed umbers. Sign/Magnitude otation

UNIT- 3 Introduction to C++

COMP2121: Microprocessors and Interfacing. Number Systems

Digital Fundamentals

Variable and Data Type I

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Full file at

Divide: Paper & Pencil

Digital Fundamentals

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

COMP Overview of Tutorial #2

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

Number Systems CHAPTER Positional Number Systems

Chapter 2 Bits, Data Types, and Operations

The type of all data used in a C (or C++) program must be specified

Objectives. Connecting with Computer Science 2

MC1601 Computer Organization

CHAPTER TWO. Data Representation ( M.MORRIS MANO COMPUTER SYSTEM ARCHITECTURE THIRD EDITION ) IN THIS CHAPTER

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

CSC201, SECTION 002, Fall 2000: Homework Assignment #2

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Real Numbers finite subset real numbers floating point numbers Scientific Notation fixed point numbers

CMU MSP 36601: Computer Hardware and Data Representation

Creating a C++ Program

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

Chapter 4: Computer Codes. In this chapter you will learn about:

Chapter 2 Bits, Data Types, and Operations

Basic Elements of C. Staff Incharge: S.Sasirekha

Chapter 2 Bits, Data Types, and Operations

Numeric Variable Storage Pattern

Data Representation and Storage. Some definitions (in C)

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Octal and Hexadecimal Integers

Variable and Data Type I

Number Systems. The Computer Works in Binary, or how I learned to think like a computer. The computer s natural number system is binary not decimal.

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

A Fast Review of C Essentials Part I

Transcription:

Chapter 2 CREATING DATA FIELDS SYS-ED/ Computer Education Techniques, Inc.

Objectives You will learn: Declaring PL/1 variables with scale, base, and mode. The internal representation of numeric, character, and varying character strings. Differences between the number types and which types to use for a particular circumstance. Declaring and using bit strings. Initializing a variable in the Declare statement. Formatting data items for printing or writing to a dataset. Coding the PICTURE specification in a DECLARE. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page i

1 Data Items A data item is either the value of a variable or a constant. Data items can be: Scalars Data aggregates Single items. Collection of data items that can be referred to either collectively or individually. Data aggregates are groups of data items. There are three kinds of data aggregates: arrays structures unions 1.1 Variable A variable has a value or values that might change during execution of a program. It is introduced by a declaration, which declares the name and certain attributes of the variable 1.2 Constant A constant has a value that cannot change. Constants for computational data are referred to by stating the value of the constant or naming the constant in a DECLARE statement String constants, hexadecimal constants, and the picture-specification are enclosed in either single or double quotation marks. If the included quotation marks are the same type as those used to enclose the string, two quotation marks must be entered for each occurrence to be included. For purposes of improving readability, arithmetic, bit, and hexadecimal constants can use the break character ( _ ). CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 1

2 Data Types and Attributes Data used in a PL/1 program can be classified as either computational data or program-control data. Computational data Program-control data Represents values that are used in computations to produce a desired result. Arithmetic and string data constitute computational data. Represents values that are used to control execution of your program. It consists of the following data types: area, entry, label, file, format, pointer, and offset. 2.1 Numeric Data The base of a coded arithmetic data item is either decimal or binary. DECIMAL is the default. The scale of a coded arithmetic data item is either fixed-point or floating-point. The precision of a coded arithmetic data item includes the number of digits and the scaling factor. The mode of an arithmetic data item, coded arithmetic or numeric character, is either real or complex. The SIGNED and UNSIGNED attributes can be used only with FIXED BINARY variables and ORDINAL variables. SIGNED indicates that the variable can assume negative values. UNSIGNED indicates that the variable can assume only nonnegative values. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 2

2.2 Binary Fixed-point Data The data attributes for declaring binary fixed-point variables are BINARY and FIXED. declare Factor binary fixed (20,2); Factor is declared as a variable that can represent binary fixed-point data of 20 data bits; two of which are to the right of the binary point. A binary fixed-point constant consists of one or more bits with an optional binary point, followed immediately by the letter B. Binary fixed-point constants have a precision (p,q), where p is the total number of data bits in the constant, and q is the number of bits to the right of the binary point. Examples: Constant Precision 1011_0B (5,0) 1111_1B (5,0) 101B (3,0) 1011.111B (7,3) 2.3 Decimal Fixed-point Data The data attributes for declaring decimal fixed-point variables are DECIMAL and FIXED. declare A fixed decimal (5,4); This code specifies that A represents decimal fixed-point data of 5 digits; 4 of which are to the right of the decimal point. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 3

declare B fixed (7,0) decimal; declare B fixed decimal(7); These two code fragments specify that B represents integers of 7 digits. A decimal fixed-point constant consists of one or more decimal digits with an optional decimal point. Decimal fixed-point constants have a precision (p,q), where p is the total number of digits in the constant and q is the number of digits specified to the right of the decimal point. Constant Precision 3.1416 (5,4) 455.3 (4,1) 732 (3,0) 1_200_300 (7,0) 003 (3,0) 5280 (4,0).0012 (4,4) /* Calculation Program */ CALC: PROC OPTIONS(MAIN); DCL I1 FIXED BIN(31) INIT(5); DCL I2 FIXED BIN(31) INIT(6); I1 = I2 * 2; PUT LIST(' I1 IS ', I1); END CALC; CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 4

2.4 Binary Floating-point Data The data attributes for declaring binary floating-point variables are: BINARY FLOAT declare S binary float (16); S represents binary floating-point data with a precision of 16 binary digits. A binary floating-point constant is a mantissa followed by an exponent and the letter B. The mantissa is a binary fixed-point constant. Examples: Constant Precision 101101E5B (6) 101.101E2B (6) 11101E-28B (5) 11.01E+42B (4) 2.5 String Data and Attributes Attribute BIT attribute CHARACTER attribute PICTURE attribute WIDECHAR GRAPHIC attribute Description Specifies a bit variable. Specifies a character variable. Character strings can also be declared. Specifies a widechar variable which will hold UTF-16 data. Specifies a graphic variable. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 5

declare User character (15); The statement declares User as a variable that can represent character data with a length of 15: declare Symptoms bit (64); This code fragment declares a bit variable. Data with the CHARACTER attribute can contain any of the 256 characters supported by the character set. The VARYING and VARYINGZ attributes specify that a variable can have a length varying from 0 to the declared maximum length. NONVARYING specifies that a variable always has a length equal to the declared length. The storage allocated for VARYING strings is 2 bytes longer than the declared length. o The leftmost 2 bytes hold the string s current length. The storage allocated for a VARYINGZ character string is 1 byte longer than the declared length. The current length of the string is equal to the number of bytes before the first '00'x in the storage allocated to it. 2.6 X (hex) Character Constant The X character constant is a contiguous sequence of an even number of hex digits enclosed in single or double quotation marks and followed immediately by the letter X. Each pair of hex digits represents one character. The length of an X constant is half the number of hex digits specified. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 6

3 Arithmetic Data Declaration Terminology Keywords Attributes Base Scale Precision Length Vocabulary that Makes Up the PL/1 Language Descriptive properties of data. Radix of a value - DECIMAL or BINARY. Fixed-point or Floating-point. Number of digits in an arithmetic value. Number of characters in alphanumeric data or number of bits in a bit-string. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 7

3.1 Fixed Decimal Declare DCL DOLLARS_AND_CENTS FIXED DECIMAL (15,2); DCL PERCENT FIXED DECIMAL (7,4); DCL NET_PROFIT FIXED DEC (13,0); 3.2 Fixed Binary Declare DCL TABLE_INDEX FIXED BINARY (15,0); DCL VERY_HIGH_COUNTER FIXED BIN (31,0); CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 8

4 Floating Point Declaration DCL identifier FLOAT DECIMAL(p); Precision DCL SPEED FLOAT DECIMAL(6); This declare is used to reserve a floating point number with six significant digits being maintained. Since precision is less than seven digits, short form floating point is used - 4 bytes. DCL VERY_PRECISE_NUMBER FLOAT DEC(16); This declare is used to reserve a floating point number that requires more than 6 significant digits to be maintained. Sixteen digits of precision are reserved for this variable. Long form floating point is used - 8 bytes. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 9

5 Arithmetic Data Types in Review Data Type Use Precision Maximum Value Used for FIXED BINARY (15,0) (31,0) 32,767 2,147,483,647 Counting. Array indexing. Representing integers. FIXED DECIMAL (t,f) where: Up to 15 digits Numbers with fractional parts. FLOAT DECIMAL t=total digits f=fractional digits and t should be an odd 3 sizes: (6) Short (16) Long (33) Extended Data where total accuracy is required. Larger numbers; FIXED BINARY cannot be accommodated. 10**75 Extremely large or small numbers. Most modeling and scientific programming. Cconvenience. PL/1 will maintain the significant digits. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 10

6 Declaration of Strings Examples: DCL NAME CHAR(20); DCL TRUE BIT (1); DCL CITY CHARACTER(16); CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 11

7 Varying Length Strings Format: DCL NAME CHAR(30) {VARYING} {VAR} Strings may have the VARYING attribute. Allows the string to take on different lengths. Length can vary between 0 and maximum specified in DECLARE. PL/1 internally tracks the string length automatically. It uses 2 extra bytes (halfword binary) added to the front of the string's storage to store the length. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 12

8 Initial Attribute The initial attribute is used to give starting values to variables when storage is assigned. Format: Examples: DCL identifier attribute(s) {INITIAL} (value or expr.); {INIT} DCL NAME CHAR(10) DCL TOWN CHAR(20) DCL FALSE BIT(1) DCL ATTR BIT(4) INIT('ALBERT'); [padded with 4 blanks] VAR INIT ('AFTON'); [stores length] INIT ('O'B); INIT ('101101'B); [truncates rightmost 2 bits] DCL DOLLAR_AND_CENTS FIXED DEC(7,2) INIT (4999.99); DCL LARGE_NUMBER FLOAT DEC(16) INIT (1234560000000789); DCL RCD_COUNT FIXED BIN(15,0) INIT (9) or FIXED BIN(15,O) INIT (1001B); CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 13

9 String Data Data Type Stored Length CHARACTER_STRING 1 CHARACTER PER BYTE 0 TO 32767 CHAR's BIT_STRING 8 BITS PER BYTE 0 TO 32767 BITS DCL NAME CHAR(20), MASK BIT(17) String data may have the varying attribute, in which case the actual length at any particular time is the length of the last data item assigned to it. DCL NAME CHAR(20) VARYING; NAME = 'JON'; /*LENGTH IS 3 BYTES */ NAME = 'JONATHON'; /*LENGTH IS 8 BYTES */ Exercise: AVERAGE: PROCEDURE OPTIONS(MAIN); DECLARE (A,B,C,D,E) FIXED(4,1); GET LIST (A,B,C,D,E); MEAN = (A+B+C+D+E) / 5; PUT LIST('AVERAGE IS', MEAN); END AVERAGE; Explicitly Declared Identifiers Implicitly Declared Identifiers CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 14

10 Declaring Identifiers with the Bit Attribute DECLARE YES BIT(1), NO BIT(1), MORE_TRANSACTIONS BIT(1), SKILLS BIT(16); YES = '1'B; NO = '0'B; MORE_TRANSACTIONS = YES; SKILLS = '1100111000111100'B; SKILLS = (8)'0B' (8)'1'B; Pad on right with zeros, if bit-string is less than the declared length. Truncate on right, if bit-string is greater than the declared length. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 15

11 Initial Attribute The initial attribute causes an identifier to be set/initialized to a specified value before execution of other PL/1 statements in a program. DCL TITLE CHAR(10) INITIAL('SIT GEORGE'); DCL DELETE CHAR(1) INIT(' '); DCL SUM FIXED(5) INIT(0); DCL PI FIXED(6,5) INIT(3.14159); /* NOTE ABBREVIATION */ DCL (A,B,C) FIXED(3) INIT(0); DCL ON BIT(1) INIT('1'B); DCL OFF BIT(1) INIT('0'B); CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 16

12 PICTURE Attribute The PICTURE attribute specifies the properties of a character data item by associating a picture character with each position of the data item. 12.1 Picture Character A picture character specifies a category of characters that can occupy that position. A numeric picture specification specifies arithmetic attributes of numeric character data in much the same way that they are specified by the appearance of a constant. 12.2 Numeric Character Numeric character data has an arithmetic value, but is stored in character form. Numeric character data is converted to coded arithmetic before arithmetic operations are performed. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 17

12.3 Picture Characters Character Description Additional Comment 9 Any decimal digit. V Virtual decimal point. Causes decimal point alignment.. Decimal point insert. Not necessarily related to decimal point alignment. - Negative. Usually used on output pictures. 9 Positive. Usually used on output pictures. S Sign--positive or negative. Used for input and output. X A Any alphanumeric character. Any alphabetic character. $ Insert dollar sign. 0 Insert asterisk., Insert comma. B Insert blank character. Usually used on output pictures. CR Credit symbol. cr for negative fields, bb for positive fields. DB Debit symbol. db for negative fields, bb for positive fields. Z Zero suppress. Input: blanks, interpreted as zeros, output: leading zeros replaced by blanks. / Insert slash. CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 18

Examples: Source Data Picture Specification Resulting Data 00000 '99999' 'ZZZZ9' 00.00 '99V.99' 'ZZV.ZZ' 'ZZV.99' 'ZZ.V99' 0123.45 '9999V.99' 'ZZZZV.99' 'Z,ZZZV.99BBB' 070784 '99/99/99' 'Z9/99/99' 'ZZ/ZZ/ZZ(5)B' 001200.75 '$ZZZ,ZZZV.99' '$$$$,$$$V.99cr' -123.45 '999V.99' 's999v.99' '999V.99BDB' '999V.99CR' '999V.99S' '(6)A' 00000 bbbb0 00.00 bbbbb bb.00 bbb00 0123.45 b123.45 bb123.45bbb 07/07/84 b7/07/84 b7/b7/84bbbbb $bb1,200.75 bb$1,200.75bb 123.45-123.45 123.45bDB 123.45CR 123.45- ABCbbb CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 19

13 Declare Statement Formats Examples: Format 1: DCL AMT_PURCH FIXED DEC(7,2) INIT(0); DCL AMT_COST FIXED DEC(7,2) INIT(0); DCL AMT_RECEIVED FIXED DEC(7,2) INIT(0); Format 2: DCL AMT_PURCH FIXED DEC(7,2) INIT(0), AMT_OF COST FIXED DEC(7,2) INIT(0), AMT_RECEIVED FIXED DEC(7,2) INIT(0); Format 3 (factoring): DCL (AMT_PURCH, AMT_COST, AMT_RECEIVED) FIXED DEC(7,2) INIT(0); Format 4 (factoring): DCL (AMT_PURCH INIT(300.00), AMT_COST INIT(250.00), AMT_RECEIVED INIT(0) FIXED DEC(7,2); CETi / COMPUTER EDUCATION TECHNIQUES, INC. (PL/1 Prg - 9.12) Ch 2: Page 20