7-1 Chapter 7.  Basic Arithmetic Verbs  Options Available with Arithmetic Verbs  COMPUTE Statement  Signed Numbers in Arithmetic Operations  Intrinsic.

Slides:



Advertisements
Similar presentations
Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
Advertisements

Lecture 2 Introduction to C Programming
7-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Data types and variables
JavaScript, Third Edition
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Section 3.6 BUILT-IN FUNCTIONS involving numbers & strings.
Numerical Expressions
Objectives You should be able to describe: Data Types
Simple Data Type Representation and conversion of numbers
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert.
Chapter 2.2 Scientific Notation. Expresses numbers in two parts: A number between 1 and 10 Ten raised to a power Examples: 2.32 x x
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
7-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
Chapter 2: Using Data.
Lecture 31 Numeric Edited Alphabetic (A) AlphaNumeric (X) Numeric (9, V, S) Numeric Edited (9, Z, comma, decimal point, minus sign) –Z = zero suppressed.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Some Basic Math Concepts for Statistics
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Arithmetic and Edited Pictures. Arithmetic Verb Template  Most COBOL arithmetic verbs conform to the template above. For example; ADD Takings TO CashTotal.
Module 6 Arithmetic. Arithmetic Verb Template  Most COBOL arithmetic verbs conform to the template above. For example; ADD Takings TO CashTotal. ADD.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
7-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been.
Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions Chapter 7.
Doing math In java.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Any Questions! 2 Agenda Moving data Arithmetic Verbs Compute statement.
Chapter 1 Representing Data in a Computer. 1.1 Binary and Hexadecimal Numbers.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
William Stallings Computer Organization and Architecture 8th Edition
Topics Designing a Program Input, Processing, and Output
Data Types, Variables & Arithmetic
Arithmetic operations & assignment statement
Multiple variables can be created in one declaration
Arithmetic Operator Operation Example + addition x + y
Variables and Arithmetic Operations
Lecture 3 Expressions Richard Gesick.
A First Book of ANSI C Fourth Edition
Digital Logic & Design Lecture 02.
C Operators, Operands, Expressions & Statements
Section 5.3 The Rational Numbers
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Data Types and Expressions
Decision Making Using the IF and EVALUATE Statements
Data Types and Expressions
Presentation transcript:

7-1 Chapter 7

 Basic Arithmetic Verbs  Options Available with Arithmetic Verbs  COMPUTE Statement  Signed Numbers in Arithmetic Operations  Intrinsic Functions 7-2

 ADD, SUBTRACT, MULTIPLY, DIVIDE  All require fields operated on to ◦ Have numeric PICTURE clauses ◦ Contain numeric data when statements executed 7-3

identifier-1 ADD… TO identifier-2... literal-1  identifier-1 or literal-1 added to identifier-2  Result stored in identifier Format 1

Assume X, Y and Z are numeric fields X = 5, Y = 3 and Z = 7 ADD StatementResult Add X To YY = 8 Add X, 9 To YY = 17 Add X, 6, Y To ZZ = 21  Identifiers preceding TO are unchanged  Value of identifier after TO ◦ Used in ADD operation ◦ Original value replaced with ADD result 7-5

identifier-1 ADD … GIVING identifier-2... literal-1  Identifiers and literals preceding GIVING added together  Result stored in identifier Format 2

Assume X, Y and Z are numeric fields X = 5, Y = 3 and Z = 7 ADD StatementResult Add X, Y Giving ZZ = 8 Add X, 10 Giving YY = 15 Add X, 4, Y Giving ZZ = 12  Identifiers preceding GIVING are unchanged  Value of identifier after GIVING ◦ Original value replaced with ADD result ◦ May be report-item with edit symbols 7-7

 Several ADD operations can be done in single statement  Assume X, Y and Z are numeric fields X = 5, Y = 3 and Z = 7 ADD StatementResult Add X To Y, ZY = 8, Z = 12 Add X, 6 Giving Y, ZY = 11, Z =

 Use ADD … TO when original contents of result operand ◦ Need to be included in operation ◦ But are not needed after operation  Use ADD … GIVING when ◦ Original contents of all operands except result field are to be retained ◦ ADD HOURS-WORKED TO WEEKLY-HOURS ◦ Vs. Giving 7-9

identifier-1 SUBTRACT … FROM identifier-2... literal-1  identifier-1 or literal-1 subtracted from identifier-2  Result stored in identifier Format 1

Assume A, B and C are numeric fields A = 6, B = 2 and C = 18 SUBTRACT Statement Result Subtract A From C C = 12 Subtract B, 5 From C C = 11 Subtract B From A, CA = 4, C =

identifier-1 identifier-2 SUBTRACT … FROM literal-1 literal-2 GIVING identifier-3...  identifier-1 or literal-1 subtracted from identifier-2 or literal-2  Result stored in identifier Format 2

Assume A, B and C are numeric fields A = 6, B = 2 and C = 18 SUBTRACT StatementResult Subtract B From A Giving C C = 4 Subtract A From 15 Giving C C = 9 Subtract A, 4 From C Giving B B =

identifier-1 MULTIPLY BY identifier-2... literal-1  identifier-1 or literal-1 multiplied by identifier-2  Result stored in identifier Format 1

Assume Q, R and S are numeric fields Q = 4, R = 7 and S = 5 MULTIPLY StatementResult Multiply Q By R R = 28 Multiply 10 By S S = 50 Multiply 2 By R, S R = 14, S =

identifier-1identifier-2 MULTIPLY BY literal-1literal-2 GIVING identifier-3...  identifier-1 or literal-1 multiplied by identifier-2 or literal-2  Result stored in identifier Format 2

Assume Q, R and S are numeric fields Q = 4, R = 7 and S = 5 MULTIPLY StatementResult Multiply Q By R Giving SS = 28 Multiply Q By 3 Giving S S = 12 Multiply 6 By Q Giving R, S R = 24 S =

 Only two operands can be multiplied using the MULTIPLY statement  To obtain product of 3 operands requires two instructions To find Price x Qty x Discount Multiply Price By Qty Giving WS-Amt Multiply Discount By WS-Amt 7- 18

identifier-1 DIVIDE INTO identifier-2... literal-1  identifier-1 or literal-1 divided into identifier-2  Result stored in identifier Format 1

Assume X, Y and Z are numeric fields X = 2, Y = 12 and Z = 8 DIVIDE StatementResult Divide X Into Y Y = 6 Divide 3 Into Y Y = 4 Divide 2 Into Y, Z Y = 6, Z =

identifier-1 INTO identifier-2 DIVIDE literal-1 BY literal-2 GIVING identifier-3...  identifier-1 or literal-1 divided into or by identifier-2 or literal-2  Result stored in identifier Format 2

Assume X, Y and Z are numeric fields X = 2, Y = 12 and Z = 8 DIVIDE StatementResult Divide X Into Y Giving ZZ = 6 Divide Y By X Giving Z Z = 6 Divide 16 By Z Giving X, Y X = 2 Y =

 Optional clause with DIVIDE used to store remainder of division  Assume Q and R have PICTUREs of 99 Divide 70 By 15 Giving Q Remainder R  Stores quotient 4 in Q and integer remainder 10 in R  What can this be used for???  Same as MOD 7- 23

 Arithmetic result truncated if room to store all decimal positions not available ◦ Result of 3.89 stored in field with PIC 9V9 as 3 ^ 8  Include ROUNDED to round result to PICTURE specification ◦ Result of 3.89 stored as 3.9 if ROUNDED option used ◦ Its actually adding.5 then truncating 7- 24

01 Amt1Pic 9V99Value Amt2Pic 999. Arithmetic statement Result Value Stored Multiply.3 By Amt1.675 Amt1 = 0 ^ 67 Multiply.3 By Amt1 Rounded.675 Amt1 = 0 ^ 68 Divide 150 By 9 Giving Amt2 Rounded16.66… Amt2 = Examples

 Occurs when result value too large to be stored in result field  Result of this ADD statement is 1,075 Add 350 To 725 Giving Num  If Num has PICTURE of 999, only 3 digits can be stored  High-order digits truncated so 075 stored in Num 7- 26

 Any arithmetic statement may include one or both size error clauses  ON SIZE ERROR statement(s) ◦ Specifies one or more statements to be executed if overflow (size error) occurs  NOT ON SIZE ERROR statement(s) ◦ Specifies one or more statements to be executed if overflow (size error) does not occur 7- 27

Add X To Y Giving Z On Size Error Display ' Result too large' Not On Size Error Perform Calc-Para End-Add  If sum of X and Y too large to store in Z, Display statement executed  If Z large enough for result, Calc-Para is performed  When using one or both clauses, use scope terminator to end arithmetic operation ◦ END-ADD, END-SUBTRACT ◦ END-MULTIPLY, END-DIVIDE 7- 28

 Ensure receiving field has PICTURE large enough to store result  Addition - define resultant field one position larger than largest field added  Subtraction - define resultant field as large as number being subtracted from ◦ Assumes positive numbers ◦ Assumes smaller subtracted from larger number 7- 29

 Multiplication - define resultant field equal to sum of lengths of operands begin multiplied  Division - define resultant field equal to sum of number of digits in divisor and dividend 7- 30

 General arithmetic statement using symbols in place of arithmetic verbs SymbolVerb +ADD -SUBTRACT *MULTIPLY /DIVIDE **exponentiation 7- 31

arithmetic-exp-1 COMPUTE identifier-1 … = literal-1 identifier-2  Identifier to left of equal sign set to value of arithmetic-expression, literal or identifier on right of equal sign Format

Assume X, Y and Z are numeric fields X = 9, Y = 4 and Z = 12 COMPUTE StatementResult Compute Z = X * YZ = 36 Compute X = Z - Y + 2 X = 10 Compute X = YX = 4 Compute Z = Y ** 2Z =

 Arithmetic expression may include any combination of symbols +, -, *, / or **  Order of operations 1. ( ) override rules 1-3, all operations in ( ) performed first 2. ** all exponentiation performed first 3. * or / in order or appearance left to right 4. + or - in order or appearance left to right Same rules apply here as standard math 7- 34

Assume X, Y and Z are numeric fields X = 6, Y = 18 and Z = 5 COMPUTE StatementResult Compute Z = Y / X + 3 Z = 6 Compute Z = Y / (X + 3) Z = 2 Compute Y = Z + X * 10Y = 65 Compute Y = Z * X / 10Y =

 COMPUTE can include same optional clauses used with other arithmetic verbs  ROUNDED follows result field (identifier preceding equal sign)  If ON SIZE ERROR or NOT ON SIZE ERROR clauses used, include scope terminator END- COMPUTE 7- 36

 Use S in PIC clause of result field if ◦ Numbers used in calculation may be negative ◦ Calculation may produce negative results  PIC clause without S assumed to be unsigned ◦ If negative result stored in unsigned field, sign not retained  We discussed this already 7- 37

 Built-in procedures to perform particular task like ◦ Find square root of number ◦ Convert letters to uppercase ◦ Get current date  Looks like our compiler handles these because of Lab 2 with the NUMVAL function 7- 38

Find square root of X and place result in Y Compute Y = Function Sqrt(X)  Value of X passed to function called Sqrt  Code in function finds square root of X  Result returned by Sqrt assigned to Y Example

Convert More-Data to uppercase Move Function Upper-Case (More-Data) To Up-More-Data  If More-Data = "Yes", function Upper-Case returns value "YES"  Value "YES" moved to Up-More-Data Example

 Output of function - result returned after function performs its task  Function returning alphanumeric result used in statements using alphanumeric data-items  Function returning numeric result can be used only in arithmetic expressions, of course  Page 283 – 288 has a list of several categorized by type 7- 41

 ADD, SUBTRACT, MULTIPLY, and DIVIDE verbs ◦ format without GIVING  Receiving field is part of arithmetic  May not be report-item ◦ with GIVING format  Receiving field is not part of arithmetic  May be report-item 7- 42

 COMPUTE used for any combination of arithmetic operations  Order of evaluation of operators 1.** 2.* or / in sequence left to right 3.+ or - in sequence left to right 4.( ) override normal hierarchy rules 7- 43

 ROUNDED can follow receiving field in any arithmetic verb  ON SIZE ERROR, NOT ON SIZE ERROR ◦ Can be used with any arithmetic verb ◦ Include scope terminator (e.g., END-ADD) 7- 44

 Intrinsic functions added as COBOL extensions in 1989 ◦ Calendar ◦ Numerical analysis ◦ Statistical ◦ Trigonometric ◦ Financial ◦ Character and String 7- 45