Review Question - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Review Question

Description:

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 number to fit? – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 20
Provided by: bois78
Learn more at: http://cs.boisestate.edu
Category:

less

Transcript and Presenter's Notes

Title: Review Question


1
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
    number to fit?
  • Old Odometer problem.

2
Arithmetic Expressions
  • 02/04/11

3
Programs do Calculations
  • Example in cs117/ch2/circle.cpp

4
Arithmetic Operators
  • Algebra
  • , - unary
  • x,
  • , - binary
  • xn
  • C
  • , - unary
  • , /,
  • , - binary
  • no exponent

5
Precedence
  • 1. Parenthesis
  • 2. Unary , -
  • 3. , /,
  • 4. Binary , -

6
Values of the Following?
  • 5020
  • 20.0 6.0/2.0 3.0
  • 20.0 6.0/(2.0 1.0)
  • 5 -a 14 ? ,assume a -2

7
Associativity
  • Left to right
  • x y z
  • Right to left
  • --z
  • x y 0

8
No Implied Multiplication
  • x 2(y z) //Error
  • x 2(y z) //Ok

9
Assignment Operator
  • variable expression
  • expression on right evaluated
  • result stored in variable
  • int x, y
  • x 9(74)
  • y x 20

10
Example
11
int operations
  • int i 5
  • int j 3
  • int k
  • k i / j //Result of divide truncated
  • k i j
  • Result int when operands are int

12
Careful with Integer Fractions
13
Fraction -- Whats Wrong?
14
Implicit Type Conversion
  • Assume variables
  • int k 5, m 4, n
  • double x 1.5, y 2.1, z
  • Type automatically converted
  • n x
  • z m

15
Implicit Type Conversion
  • Assume variables
  • int k 5, m 4, n
  • double x 1.5, y 2.1, z
  • z k /m //Result of op. Same as Operands
  • y m 1.5 // mixed types

16
Explicit Type Conversion
17
Explicit Type Conversion
int kids, families double average kids
21 families 8 average (double)kids/families

18
Next
  • Additional Operations
  • Functions
  • Read Chapter 3 for Wednesday

19
Exercises for Study
  • p. 55, 1 ,3, 4
Write a Comment
User Comments (0)
About PowerShow.com