CIS22A - Notes for Week 3, 10/7-10/10

Announcements and Reminders


Topics

Terminology

Chapter 3

cin

What does the extraction operator do?
Entering multiple values
white space

Program 3-3 - Page 88-89

Mathematical Expressions

z
9
z + 9
z * 9
sizeof(z)
z + 5 * 3 / 7

What if
int z = 5;

What is the value of each expression?

Operator Precedence and Associativity

parentheses
sizeof
- (unary negation)
* / %
+ - (binary subtraction)
<<  >>

Evaluate these expressions
5 + 2 * 6
12 / 4 - 3
3 + 4 * 6 - 2
12 * 18 % 7 - 1
15 - 2 * 6 + 8 - 2

(5 + 2) * 6
12 / (4 - 3)
3 + 4 * (6 - 2)
12 * (18 % 7) - 1
(15 - 2) * (6 + (8 - 2))

5 * sizeof(1000000)

associativity

right to left or left to right
See Appendix C Page 1289

The pow() function

Program 3-5 - Page 96

Calculate an average

Program 3-6 - Page 98

Type Conversions

Level of Conversions
exact match
trivial conversion
promotions
standard conversions
floating point type to integer type
demotions
integer type to floating point type

When/How does a conversion occur
assignment
binary operation
cast

Overflow and Underflow

Program 3-7 - Page 102
Program 3-8 - Page 103

Cast

C-style cast
"old" C++ style functional notation for a cast

static_cast
forces a  type conversion
sometimes used to document a conversion (that will take place anyway)
useful for something like, average of three ints:

int a = 2, b = 3, c = 5;
float avg = (a + b + c) / static_cast<float>(3);

Advice:  Do not use C-style casts in this class (or even functional notation for a cast).  Use static_cast where appropriate. 


Multiple Assignment and the Assignment operator

int x = 5, z = 99, q, maybe = 9;
int a = b = c = d = 9;  // Why does this work?


Example:  Calculate the harmonic mean of the first 5 prime numbers.

The formula for a harmonic mean is 

    

The first 5 prime numbers are 2, 3, 5, 7, and 11.

Solution


Example:  Calculate the roots of a quadratic equation

A quadratic equation has the form 

    ax2 + bx + c = 0
   
The roots are determined by the formula

   

 Solution


Some More Integer Operators

Compound assignment
+=   -=   *=   /=   %=

Increment and decrement operators
++ prefix and postfix
-- prefix and postfix

Formatted Output

An output stream manipulator changes the behavior of an output stream.
The parameterized manipulators require the <iomanip> header file.

The setw manipulator

has a short memory

Program 3-13 - Page 112

The setprecision manipulator

has a long memory

Program 3-15 - Page 114

The fixed and scientific manipulators

have a long memory

Example
cout << setprecision(2);
float one = 123.4;
float two = 4567.895;
float three = 456;
float four = 2.89;
float five = .895;
cout << one << endl;
cout << two << endl;
cout << three << endl;
cout << four << endl;
cout << five << endl << endl;

cout << fixed;
cout << one << endl;
cout << two << endl;
cout << three << endl;
cout << four << endl;
cout << five << endl;

OUTPUT

1.2e+002
4.6e+003
4.6e+002
2.9
0.89

123.40
4567.90
456.00
2.89
0.89

The showpoint manipulator

has a long memory

Example

cout << setprecision(6);
float one = 123.4;
float two = 4567.895;
float three = 456;
float four = 2.89;
float five = .895;
cout << one << endl;
cout << two << endl;
cout << three << endl;
cout << four << endl;
cout << five << endl << endl;
cout << showpoint;
cout << one << endl;
cout << two << endl;
cout << three << endl;
cout << four << endl;
cout << five << endl;


OUTPUT

123.4
4567.9
456
2.89
0.895

123.400
4567.90
456.000
2.89000
0.895000

The left and right manipulators

have a long memory

Example
int x = 500;
float y = 2.54;
string z = "zebra";

cout << x << y << z << endl;

cout << setw(8) << x << setw(8) << y << setw(8) << z << endl;
cout << left;
cout << setw(8) << x << setw(8) << y << setw(8) << z << endl;

cout << right;
cout << setw(8) << x << setw(8) << y << setw(8) << z << endl;

OUTPUT

5002.54zebra
     500    2.54   zebra
500     2.54    zebra
     500    2.54   zebra


string input

Example
string name;
int age;

cout << "Enter your name ";
cin >> name;

cout << "Enter your age ";
cin >> age;

cout << name << age << endl;

OUTPUT

Enter your name Joe Bentley
Enter your age Joe-858993460

What happened?

The getline (string) function

reads from an input stream into a string

Prototype
istream& getline (istream& is, string& str);

Example

string name;
int age;

cout << "Enter your name ";
getline(cin,name);

cout << "Enter your age ";
cin >> age;

cout << name << age << endl;

OUTPUT

Enter your name Joe Bentley
Enter your age 37
Joe Bentley37

char input

The overloaded get function

reads char input from an input stream

Prototypes
int istream::get();
istream& istream::get (char& c);

Example

char a, b;
int c;
cin.get(a);
b = cin.get();
c = cin.get();
cout << a << ' ' << b << ' ' << c << endl;

INPUT
ABC

OUTPUT
A B 67

The ignore function

Extract and discard characters

Prototype
istream& ignore (streamsize n = 1, int delim = EOF);

Program 3-23 - Page 125

Example

string name;
int age;

cout << "Enter your name ";
cin >> name;
cin.ignore(80,'\n');
cout << "Enter your age ";
cin >> age;

cout << name << age << endl;

OUTPUT

Enter your name Joe Bentley
Enter your age 37
Joe37


Math Library Functions

  • requires #include <cmath>
Program 3-24 - Page 128

The rand() function

  • returns a random int between 0 and RAND_MAX
  • requires #include <cstdlib>
Program 3-25 - Page 129



Videos

Basic Arithmetic

Read me
cin
istream object
extraction operator
right shift

operator precedence
operator associativity

pow()

conversion
exact match
trivial conversion
promotions
demotions
integer type
floating point type

assignment
assignment operator
binary operator, binary operation
cast
static_cast (operator)

output stream manipulator
<iomanip>
setw
setprecision
fixed
scientific
showpoint
left
right

input buffer

compound assignment operators
+=
-=
*=
/=
%=

increment operator
++
decrement operator
--
prefix
postfix

output stream manipulator
<iomanip>
setw
setprecision
fixed
scientific
showpoint
left
right

input buffer

getline
prototype
get
overloaded
ignore
delimiter
default argument

math library
abs
cos
exp
fmod
log
log10
sin
sqrt
tan
<cmath>

rand()
<cstdlib>
srand
seed a random number generator
time
<ctime>