Chapter 03
Chapter 03
Tenth Edition
Chapter 3
Expressions and
Interactivity
3-2
Topics 1of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-3
Topics 2 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-4
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-6
•Examples:
area = 2 * PI * radius;
cout << "border is: " << (2*(l+w));
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-9
Order of Operations 1 of 2
In an expression with > 1 operator, evaluate it in
this order:
First: ( ) expressions in parentheses
Then: - (unary negation) in order, left to right
Then: * / % in order, left to right
Finally: + - in order, left to right
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-10
Order of Operations 2 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-11
Associativity of Operators
Algebraic Expressions
•Multiplication requires an operator
Area = lw is written as Area = l * w;
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-14
Type Coercion
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-16
Coercion Rules 1 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-17
Coercion Rules 2 of 2
Important Notes:
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-18
Type Casting
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-19
char ch = 'C';
cout << ch << " is stored as "
<< static_cast<int>(ch);
gallons = static_cast<int>(area/500);
average = static_cast<double>(sum)/count;
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-20
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-21
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-22
Overflow Example
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-23
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-24
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-25
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-26
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-27
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-28
Combined Assignment
x += 5; means x = x + 5;
x -= 5; means x = x – 5;
x *= 5; means x = x * 5;
x /= 5; means x = x / 5;
x %= 5; means x = x % 5;
The right hand side is evaluated before the
combined assignment operation is done.
x *= a + b; means x = x * (a + b);
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-30
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-31
Stream Manipulators 1 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-32
Stream Manipulators 2 of 2
•Some affect values until changed again
–fixed: Use decimal notation (not E-notation)
for floating-point values.
–setprecision(x):
▪When used with fixed, print floating-point
value using x digits after the decimal.
▪Without fixed, print floating-point value
using x significant digits.
▪Rounding is used if x is smaller than the
number of significant digits
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-33
Stream Manipulators
•Some additional manipulators:
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-34
Manipulator Examples
Code Displays
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-35
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-36
String Input
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-37
Character Input
Reading in a character:
char ch;
cin >> ch; // Reads in any non-blank char
cin.get(ch); // Reads in any char
ch=cin.get();// Reads in any char
cin.ignore();// Skips over next char in
// the input buffer
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-38
cin.ignore()
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-40
string equals;
equals.assign(80,'=');
. . .
cout << equals << endl;
cout << "Total: " << total << endl;
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-41
String Operators
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-42
Using C-Strings
•A C-string is stored as an array of characters
•The programmer must indicate the maximum
number of characters at definition
const int SIZE = 5;
char temp[SIZE] = "Hot";
•NULL character (\0) is placed after final
character to mark the end of the string
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-44
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-49
•rand()
–Returns a random number between 0 and the largest
int the computer holds
–Will yield the same sequence of numbers each time the
program is run
•srand(x)
–Initializes the random number generator with
unsigned int x. x is the “seed value”.
–This should be called at most once in a program
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-50
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-51
Copyright
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved