Arithmetic Calculations

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation.
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite.
LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,
CSci 1130 Intro to Programming in Java
Chapter Three Arithmetic Expressions and Assignment Statements
Pointers and Arrays Chapter 12
Computer Programming w/ Eng. Applications
Types and Arithmetic Operators
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Data Conversion, and Input
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
OPERATORS.
CPS120: Introduction to Computer Science Operations Lecture 9.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Variables, Operators, and Expressions
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Tokens in C Keywords Identifiers Constants
TMF1414 Introduction to Programming
Relational Operations
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Nahla Abuel-ola / WIT.
Primitive and Reference Data Values
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators in C
Arithmetic Expressions in C
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Arithmetic Operators in C
Arithmetic Expressions & Data Conversions
C Operators, Operands, Expressions & Statements
Lectures on Numerical Methods
Introduction to Programming
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 3 Operators and Expressions
Data Types and Expressions
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Operator King Saud University
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Arithmetic Calculations Chapter 7 Arithmetic Calculations Basic arithmetic operators: + addition - subtraction * multiplication / division % remainder (or modulus). Same precedence and associativity as * and / Examples: 17 % 3 = 2 (operands must be integers) 100 / 6 = 16 Division of two integers! (The answer is not 16.666667 or 17!) The Computer Continuum

More examples with % and / Chapter 7 More examples with % and / Given num is an integer. Decide if it is even or odd? if (num % 2) printf(“Odd”); else printf(“Even”); Or, we could do the following: if ((num % 2) == 0) Given that a and b are integers. Let a=10, b=3. What is a / b? Answer: 3 (Result of integer division) What is a % b? Answer: 1 (Remainder of division) The Computer Continuum

Example 1 Given the following declarations and assignments: Chapter 7 Example 1 Given the following declarations and assignments: int a, b, c, d, e; Let a=10, b=20, c=15, d=8, and e=40 What is result of the following expression? (a + b / (c – 5)) / ((d + 7) / (e - 37) % 3) Answer: (a + b / 10) / ((d + 7) / (e - 37) % 3) (a + b / 10) / (15 / (e - 37) % 3) (a + b / 10) / (15 / 3 % 3) (a + 2) / (15 / 3 % 3) 12 / (15 / 3 % 3) 12 / (5 % 3) 12 / 2 = 6 The Computer Continuum

Example 2 Given the following declarations and assignments: Chapter 7 Example 2 Given the following declarations and assignments: double a, b, c, d, e; Let a=10.0, b=20.0, c=15.0, d=8.0, and e=40.0 What is result of the following expression? (a + b / (c – 5.0)) / ((d + 7.0) / (e – 37.0) / 3.0) Answer: (a + b / 10.0) / ((d + 7.0) / (e – 37.0) / 3.0) (a + b / 10.0) / (15.0 / (e – 37.0) / 3.0) (a + b / 10.0) / (15.0 / 3.0 / 3.0) (a + 2.0) / (15.0 / 3.0 / 3.0) 12.0 / (15.0 / 3.0 / 3.0) 12.0 / (5.0 / 3.0) 12.0 / 1.666667 = 7.2 The Computer Continuum

Example 3 Given the following algebraic expression: Chapter 7 Example 3 Given the following algebraic expression: Write the corresponding C expression: (a*a*a + b*b*b) / (c*c – d*d) The Computer Continuum

Increment and Decrement Operators Chapter 7 Increment and Decrement Operators Increment and decrement are unary operators In C, for integer variables, such as count, we can write: (Post-increment): count++ instead of count=count+1 (Pre-increment): ++count instead of count=count+1 (Post-decrement): count-- instead of count=count-1 (Pre-decrement): --count instead of count=count-1 Example: int count=3 printf(“%d”, count++); /* prints 3! */ printf(“%d”, count); /* prints 4 */ The Computer Continuum

Comparison of Prefix and Postfix Increments Chapter 7 Comparison of Prefix and Postfix Increments The Computer Continuum

Compound Assignment Operators Chapter 7 Compound Assignment Operators Compound assignment operators: += assignment sum -= assignment difference *= assignment product /= assignment division %= assignment remainder Examples: a += b; /* a = a + b; */ a *= b; /* a = a * b; */ Suppose a=10 and b=12. Then, a *= ( b %= 7); /* a = 50, b = 5*/ The Computer Continuum

Chapter 7 Some Rules Rules for assigning a type to arithmetic expressions that involve integers and doubles: If one or more operators in an arithmetic expression are of type double, the result of the expression is also of type double If all operands in an arithmetic expression are of type integer, the result of the expression is also of type integer The type of the entire statement and the type of the value stored in the variable to the left of the assignment operator are the same as the type of the variable on the left Examples: double first=4.7; int second=27; second = first + second; /* first + second is 31.7! */ However, second=31 since 31.7 is converted (and truncated) to integer!! The Computer Continuum

Examples of Mixed Expressions Chapter 7 Examples of Mixed Expressions double x; double b=12.5; int a=7; x = a / 3 + b; What is the value of x? Evaluate a / 3 = 7 / 3 = 2 Evaluate 2 + b = 14.5 So, x = 14.5 The Computer Continuum

Explicit Type Conversions Chapter 7 Explicit Type Conversions Explicit type conversions are done by casting: The form of a cast operation is (Type) Expression double first = 4.7; int second = 27; (int)(first + second) is 31 first = (int)first + second; /* first is 31.0 */ first = (int)first % second; /* first is 4.0 */ first = second % (int)second; /* first is 3.0 */ The Computer Continuum

Mathematical Library Functions Chapter 7 Mathematical Library Functions Declarations are found in <math.h> and <stdlib.h> ceil(x) ceil(4.2)=5, ceil(-5.7)=-5 floor(x) floor(4.2)=4, floor(-5.7)=-6 abs(x) abs(-12)=12, abs(-12.7)=12 fabs(x) fabs(-12)=12.0, fabs(-12.8)=12.8 sqrt(x) sqrt(2)=1.414214 pow(x, y) pow(2, 3)=8 cos(x) cos(60*3.141593/180)=0.5 sin(x) sin(30*3.141593/180)=0.5 tan(x) tan(45*3.141593/180)=1.0 exp(x) exp(2.1)=8.16617 log(x) log(2)=0.693147 log10(x) log10(1000)=3.0 The Computer Continuum

Example 3 (modified) Given the following algebraic expression: Chapter 7 Example 3 (modified) Given the following algebraic expression: Write the corresponding C expression using pow(). (pow(a,3)+pow(b,3)) / (pow(c,2)–pow(d,2)) The Computer Continuum

Arithmetic Errors and Inaccuracies Chapter 7 Arithmetic Errors and Inaccuracies Division by zero (generally results in a run-time error) Arithmetic overflow When two numeric values are added or multiplied, result of operation may be in excess of max value that can be represented by the number of bits allocated for the type of target variable Arithmetic underflow Arithmetic operation results in a value that is less than the smallest value that can be stored for that data type. The computer stores a value of zero instead. Representational inaccuracies Precision limitations of floating-point data types 4.0/3.0=1.33333 (round-off errors) The Computer Continuum