Review Question

19
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.

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? Old Odometer problem. 02/04/11. Arithmetic Expressions. Programs do Calculations. Example in cs117/ch2/circle.cpp. C++ - PowerPoint PPT Presentation

Transcript of Review Question

Page 1: Review Question

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.

Page 2: Review Question

Arithmetic Expressions

02/04/11

Page 3: Review Question

Programs do Calculations

• Example in cs117/ch2/circle.cpp

Page 4: Review Question

Arithmetic Operators

• Algebra+, - unary

x, ÷

+, - binary

xn

• C+++, - unary

*, /, %

+, - binary

no exponent

Page 5: Review Question

Precedence

1. Parenthesis

2. Unary +, -

3. *, /, %

4. Binary +, -

Page 6: Review Question

Values of the Following?

50%20 =

20.0 – 6.0/2.0 + 3.0 =

20.0 – 6.0/(2.0 + 1.0) =

5 + -a * 14= ? ,assume a = -2

Page 7: Review Question

Associativity

• Left to rightx + y + z

• Right to left--z

x = y = 0;

Page 8: Review Question

No Implied Multiplication

x = 2(y + z); //Error

x = 2*(y + z); //Ok

Page 9: Review Question

Assignment Operator

= • variable = expression• expression on right evaluated• result stored in variable

int x, y;

x = 9*(7+4);

y = x + 20;

Page 10: Review Question

Example

Page 11: Review Question

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

Page 12: Review Question

Careful with Integer Fractions

Page 13: Review Question

Fraction -- What’s Wrong?

Page 14: Review Question

Implicit Type Conversion

• Assume variablesint k = 5, m = 4, n ;

double x = 1.5, y = 2.1, z;

• Type automatically convertedn = x;z = m;

Page 15: Review Question

Implicit Type Conversion

• Assume variablesint 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

Page 16: Review Question

Explicit Type Conversion

Page 17: Review Question

Explicit Type Conversion

int kids, families;double average;

kids = 21;families = 8;average = (double)kids/families;

Page 18: Review Question

Next

• Additional Operations– Functions

• Read Chapter 3 for Wednesday

Page 19: Review Question

Exercises for Study

• p. 55, #1 ,3, 4