More Fun with Expressions

Tom Kelliher, CS17

Feb. 28, 1996

An Example Utilizing Integer Division and Remainder

int change;
int quarters;
int dimes;
int nickels;
int pennies;

cout << "Amount of change to be given: ";
cin >> change;
cout << "Making change for " << change << " cents.\n";

quarters = change / 25;
change %= 25;

dimes = change / 10;
change %= 10;

nickels = change / 5;
change %= 5;

pennies = change;

cout << "\nChange:\n";
cout << "Quarters: " << quarters << endl;
cout << "Dimes: " << dimes << endl;
cout << "Nickels: " << nickels << endl;
cout << "Pennies: " << pennies << endl;

The Type of an Expression

We know the types of:

9 / 5
9.0 / 5
What about:
int a = 5, b = 7, c = 11;
double x = 1.0, y = 9.0, z = 13.0;

a = b + c + x - a;
x = (a + c) / b;
a = (x + b) / c;

We must determine:

  1. Order in which operators are evaluated
  2. Types and values of sub-expressions, from smallest to largest
  3. Type and value of right-hand expression
  4. If an implicit cast is required by the assignment

The rules (for what we've learned):

  1. If one of the operands has type double, the type of the expression is double
  2. If all of the operands have type int, the type of the expression has type int
  3. The type of an arithmetic assignment operator is the type of the left operand. The type and value of the right operand will be cast to that of the left before the assignment is performed

Remember:

a = (b + c) / x;
has three (sub-)expressions.

What are the types, order of evaluation, value?

Consider:

x = (a = (b + c) / x) + 5;
What are the types, order of evaluation, value?

Is this the same?

x = a = (b + c) / x + 5;

Casting

Type conversion

x = 5 / 9;   // can be fixed:   5.0 / 9.0
a = 5;
b = 9;
x = a / b;   // can this be fixed?
x = (double) a / (double) b;   // yes!

Unary operator --- same precedence as ++

Forced integer operations:

a = (int) x / (int) y;
a = (int) x / y;   // integer division?

The Math Library

C++'s core is missing I/O --- included in iostream library

It's also missing math functions --- found in math library

Attributes of a function

Mathematically, what is a function?

``Silly'' example:

int add(int a, int b)
{
   return a + b;
}

int i, j, k, l;

i = j + k;      // same result
i = add(j, k);

i = j + k + l;          // same result
i = add(add(j, k), l);

Properties:

In using library functions, explicitly cast arguments

Some Math Library Functions

Examples:

x = sqrt(y);
x = sqrt(a);            // design/syntax error
x = sqrt((double) a);   // fixed
a = sqrt((double) b);   // implicit cast in assignment
x = pow(y, 3.0);        // x is assigned y cubed

Function call --- () --- is a primary operator, highest precedence seen so far:

  1. () (left)
  2. Unary +, -, ++, -- (right)
  3. *, /, % (left)
  4. Binary +, - (left)
  5. <<, >> (left)
  6. Assignment =, +=, -=, *=, /=, %= (right)

Questions of Style

  1. Use parentheses to change order of evaluation, improve readability
  2. Time-wise, functions are expensive. Avoid calling when possible:
    x = pow(y, 2);   // slow
    x = y * y;       // much faster
    
  3. Avoid the side-effects induced by ++,, --
    ++a;             // ok
    a = ++b - c--;   // huh???
    
  4. Simplify complex expressions, then simplify them some more.

Lab Exercise

Using the math library, write a program to compute the length of the hypotenuse of a right triangle. The inputs to your program will be the lengths of the other two sides. Assume that all quantities are of type double.


Thomas P. Kelliher
Tue Feb 27 16:16:55 EST 1996
Tom Kelliher