Arithmetic Operators - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Arithmetic Operators

Description:

Fall 1999: Peter Olsen. Lecture 13: 4. Modulus ... both operands of a division expression are integers, you get an integer answer. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 18
Provided by: peterolsen
Category:

less

Transcript and Presenter's Notes

Title: Arithmetic Operators


1
Arithmetic Operators
  • Arithmetic Operations with
  • Variables

2
Arithmetic Operators
  • Name Operator Example
  • Addition num1 num2
  • Subtraction - initial - spent
  • Multiplication fathoms 6
  • Division / sum / count
  • Modulus m n

3
Arithmetic OperatorsInfix Notation
  • Operator is between the operands
  • y a b (c d)
  • Resembles standard mathematical notation
  • Requires parentheses to show order.

4
Modulus
  • The expression m n yields the 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

5
Modulus
  • 11 4 11 2 4 3 ? 11 4 3
  • 31 27 ?
  • 16 9 ?
  • 32 5 ?
  • 64 21 ?
  • 128 31 ?

6
Uses of Modulus
  • Even and Odd
  • 5 2 1 odd 4 2 0 even
  • Take the modulus by 2 of any integer, a result of
    1 means the number was odd, a result of 0 means
    the number was even.
  • The Euclidean GCD algorithm

7
Integer Division
  • If both operands of a division expression are
    integers, you get an integer answer. The
    fractional portion is thrown away, not rounded!
  • Examples 17 / 5 3
  • 4 / 3 1
  • 35 / 9 8
  • 3 / 4 0
    (!)

8
Mixed Division
  • Division where one operand is a floating point
    number will produce a floating point result.
  • Integer is promoted to a float
  • Division where one operand is a double will
    produce a double result.
  • Integer is promoted to a double.
  • General rule promotion to highest type

9
Division By Zero
  • Division by zero is mathematically undefined.
  • Division by zero will cause a fatal error. ---
    your program will terminate immediately and give
    an error message.
  • Often caused by forgetting to initialize
    variables (or using zero result of integer
    division).

10
Non-Fatal Errors
  • Non-fatal errors will not cause program
    termination, but they may produce incorrect
    results.

11
Precedence and Associativity
  • Many ways to interpret arithmetic statements
  • 5 7 3 2 41 ? 76 ? 65 ? 100
  • Infix notation is inherently ambiguous, need
    rules of precedence and associativity

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

13
Evaluation of expressions
  • a b c Would multiply a b first, then
    add c to the result.
  • If we really want the sum of b and c to be
    multiplied by a, use parenthesis to force the
    evaluation to be done in the order we want.
  • a ( b c )

14
Practice With Evaluating Expressions
  • Given integer variables a, b, c, d, 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 d c / b - a

15
Good Programming Practices
  • You will be given a handout of suggested C coding
    standards and Indentation Styles
  • You will be expected to conform to these
    standards for all programming projects in this
    class.
  • Pick one of the two indentation styles shown and
    use it consistently.

16
A Sample Project
  • Project We are going to write a program
    in class that finds the volume and surface
    area of a box.
  • Write the Algorithm
  • Use the algorithm as a guide for writing the
    code.

17
An algorithm for writing code
  • Write the algorithm
  • Write the code using emacs
  • Try to compile the code
  • While there are still syntax errors
  • Fix errors
  • Try to compile the code
  • Test the program
  • Fix any semantic errors
  • Compile the code
  • Test the program
Write a Comment
User Comments (0)
About PowerShow.com