Arithmetic Operators in C

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
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
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Operations with Positive Fractions
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
Doing math In java.
1 Variables and Arithmetic Operators in JavaScript.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
CompSci 230 S Programming Techniques
CSE 220 – C Programming Expressions.
Topics Designing a Program Input, Processing, and Output
UMBC CMSC 104 – Section 01, Fall 2016
Expressions.
Chapter 7: Expressions and Assignment Statements
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
UMBC CMSC 104 – Section 01, Fall 2016
Lecture 3 Java Operators.
Expressions.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
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.
Assignment and Arithmetic expressions
Variables and Arithmetic Operators in JavaScript
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Arithmetic Operator Operation Example + addition x + y
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
Lecture 3 Expressions Richard Gesick.
Arithmetic Expressions in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Relational and Logical Operators
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Variables and Arithmetic Operators in JavaScript
A First Book of ANSI C Fourth Edition
Arithmetic Operators in C
Arithmetic Expressions & Data Conversions
Introduction to C Topics Compilation Using the gcc Compiler
Alternate Version of STARTING OUT WITH C++ 4th Edition
Relational and Logical Operators
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
CS150 Introduction to Computer Science 1
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
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.
CS150 Introduction to Computer Science 1
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Introduction to C Topics Compilation Using the gcc Compiler
Topics Designing a Program Input, Processing, and Output
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
Incremental Programming
Lecture 3 Expressions, Type Conversion, and string
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Arithmetic Operators in C 2008/10/08: Lecture 9 CMSC 104, Section 0101 John Y. Park [Based on dblock spr’06lecture, heavily modified, reordered, many new slides added]

Arithmetic Operators Topics Arithmetic Operators Assignment Operators Operator Precedence Evaluating Arithmetic Expressions Incremental Programming Reading Section 2.5

Arithmetic Operators in C Binary Operators E.g.: new_value = height + margin; area = length * width; Unary Operators E.g.: new_value = -old_value; negation = !true_value;

Arithmetic Operators in C Name Operator Example Addition + num1 + num2 Subtraction - initial - spent Multiplication * fathoms * 6 Division / sum / count Modulus % m % n

Types and Promotion Can mix types in numerical expressions Hierarchy of types By precision: int -> float By size: short -> long Lower size/precision is promoted to greater size/precision before operation is applied Result is also of promoted type

Types and Promotion E.g.: int num_sticks = 5; double avg_stick_length = 4.5; double total_length; total_length = num_sticks * avg_stick_length; num_sticks would be converted to double-precision, then multiplied by avg_stick_length

Division If both operands of a division expression are integers, you will get an integer answer. The fractional portion is thrown away. Examples : 17 / 5 = 3 4 / 3 = 1 35 / 9 = 3

Division (con’t) Division where at least one operand is a floating point number will produce a floating point answer. Examples : 17.0 / 5 = 3.4 4 / 3.2 = 1.25 35.2 / 9.1 = 3.86813 What happens? The integer operand is temporarily converted to a floating point, then the division is performed.

Division (con’t) Example1 : int my_integer = 5; int my_product; my_product = (my_integer / 2) * 2.0; /* What will following print out? */ printf(“my_product is %d\n”, my_product); /* What about this? */ my_product = (my_integer / 2.0) * 2; printf(“my_product is %d\n”, my_product);

Division By Zero Division by zero is mathematically undefined. If you allow division by zero in a program, it will cause a fatal error. Your program will terminate execution and give an error message. Non-fatal errors do not cause program termination, just produce incorrect results.

Modulus The expression m % n yields the integer remainder after m is divided by n. Modulus is an integer operation -- both operands MUST be integers. Examples : 17 % 5 = 2 6 % 3 = 0 9 % 2 = 1 5 % 8 = 5

Uses for Modulus Used to determine if an integer value is even or odd 5 % 2 = 1 odd 4 % 2 = 0 even If you take the modulus by 2 of an integer, a result of 1 means the number is odd and a result of 0 means the number is even. The Euclid’s GCD Algorithm (done earlier)

Arithmetic Operators Rules of Operator Precedence Operator(s) Precedence & Associativity ( ) Evaluated first. If nested (embedded), innermost first. If on same level, left to right. * / % Evaluated second. If there are several, evaluated left to right. + - Evaluated third. If there are several, evaluated left to right. = Evaluated last, right to left.

a + b * c Would multiply b * c first, then add a to the result. Using Parentheses Use parentheses to change the order in which an expression is evaluated. a + b * c Would multiply b * c first, then add a to the result. If you really want the sum of a and b to be multiplied by c, use parentheses to force the evaluation to be done in the order you want. (a + b) * c Also use parentheses to clarify a complex expression.

Practice With Evaluating Expressions Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expressions: a + b - c + d a * b / c 1 + a * b % c a + d % b - c e = b = d + c / b - a

Good Programming Practice It is best not to take the “big bang” approach to coding. Use an incremental approach by writing your code in incomplete, yet working, pieces. For example, for your projects, Don’t write the whole program at once. Just write enough to display the user prompt on the screen. Get that part working first (compile and run). Next, write the part that gets the value from the user, and then just print it out.

Good Programming Practice (con’t) Get that working (compile and run). Next, change the code so that you use the value in a calculation and print out the answer. Continue this process until you have the final version. Get the final version working. Bottom line: Always have a working version of your program!