Programming and Problem Solving with C++, 2/e

Download Report

Transcript Programming and Problem Solving with C++, 2/e

Chapter 3
Numeric Types,
Expressions, and Output
Dale/Weems/Headington
1
C++ Data Types
simple
integral
enum
structured
floating
array struct union class
char short int long bool
float double long double address
pointer
reference
2
C++ Simple Data Types
simple types
integral
char
short
int
long
floating
bool
enum
float
double
long double
unsigned
3
Standard Data Types in C++
• Integral Types
– represent whole numbers and their negatives
– declared as int, short, or long
• Floating Types
– represent real numbers with a decimal point
– declared as float, or double
• Character Type
– represents single characters
– declared as char
4
Samples of C++ Data Values
int sample values
4578
-4578
0
float sample values
95.274
9521E-3
95.
-95E-1
char sample values
‘B’ ‘d’
‘4’
.265
95.213E2
‘?’
‘*’
5
More About Floating Point
Values
• floating point numbers have an integer part and a
fractional part, with a decimal point in between.
Either the integer part or the fractional part, but not
both, may be missing
EXAMPLES
18.4
500.
.8
-127.358
• alternatively, floating point values can have an
exponent, as in scientific notation--the number
preceding the letter E doesn’t need to include a
decimal point
EXAMPLES
1.84E1
5E2
8E-1
-.127358E36
Division Operator
• the result of the division operator depends on the
type of its operands
• if one or both operands has a floating point type, the
result is a floating point type. Otherwise, the result
is an integer type
• Examples
11 / 4
has value 2
11.0 / 4.0 has value 2.75
11 / 4.0
has value 2.75
7
Modulus Operator
• the modulus operator % can only be used
with integer type operands and always has an
integer type result
• its result is the integer type remainder of an
integer division
EXAMPLE
11 % 4 has value 3 because
R=?
4 ) 11
8
More C++ Operators
int age;
age = 8;
8
age
age = age + 1;
9
age
9
Increment Operator
int age;
8
age = 8;
age
age++;
9
age
10
Decrement Operator
int dogs;
100
dogs = 100;
dogs
dogs--;
99
dogs
11
What is an Expression in C++?
• An expression is a valid arrangement
of variables, constants, and operators.
• in C++ each expression can be
evaluated to compute a value of a
given type
• the value of the expression
9.3 * 4.5 is 41.85
12
Operators can be
binary
involving 2 operands
2+3
unary
involving 1 operand
-3
ternary
involving 3 operands
later
13
Some C++ Operators
Precedence
Higher
Lower
Operator
( )
+
*
/
%
+
=
Description
Function call
Positive
Negative
Multiplication
Division
Modulus (remainder)
Addition
Subtraction
Assignment
14
Precedence
• higher Precedence determines which
operator is applied first in an
expression having several operators
15
Associativity
• left to right Associativity means that in an
expression having 2 operators with the same
priority, the left operator is applied first
• in C++ the binary operators
* , / , % , + , - are all left associative
• expression 9 - 5 - 1 means ( 9 - 5 ) - 1
4-1
3
16
Evaluate the Expression
means
7 * 10 - 5 % 3 * 4 + 9
(7 * 10) - 5 % 3 * 4 + 9
70 - 5 % 3 * 4 + 9
70 - (5 % 3) * 4 + 9
70 - 2 * 4 + 9
70 - ( 2 * 4 ) + 9
70 - 8 + 9
( 70 - 8 ) + 9
62 + 9
71
17
Parentheses
• parentheses can be used to change the
usual order
• parts in ( ) are evaluated first
• evaluate (7 * (10 - 5) % 3) * 4 + 9
(7 * 5 % 3 ) * 4 + 9
( 35 % 3 ) * 4 + 9
2 * 4 + 9
8 + 9
17
18
Mileage Program
/* This program computes miles per gallon given four amounts
for gallons used, and starting and ending mileage.
Constants:
The gallon amounts for four fillups.
The starting mileage.
The ending mileage.
Output (screen)
The calculated miles per gallon.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
#include <iostream>
using namespace std;
19
C++ Code Continued
const
const
const
const
float
float
float
float
AMT1 = 11.7 ;
AMT2 = 14.3 ;
AMT3 = 12.2 ;
AMT4 = 8.5 ;
// Number of gallons for fillup 1
// Number of gallons for fillup 2
// Number of gallons for fillup 3
// Number of gallons for fillup 4
const float START_MILES = 67308.0 ;
const float END_MILES = 68750.5 ;
int main( )
{
float mpg ;
// Starting mileage
// Ending mileage
// Computed miles per gallon
mpg = (END_MILES - START_MILES) /
(AMT1 + AMT2 + AMT3 + AMT4) ;
20
Main returns an int value to
the operating system
cout << “For the gallon amounts “ << endl ;
cout << AMT1 << ‘ ‘ << AMT2 << ‘ ‘
<< AMT3 << ‘ ‘ << AMT4 << endl ;
cout << “and a starting mileage of “
<< START_MILES << endl ;
cout << “and an ending mileage of “
<< END_MILES << endl ;
cout << “the mileage per gallon is “ << mpg << endl ;
return 0;
}
21
Assignment Operator Syntax
Variable = Expression
• first, Expression on right is evaluated
• then the resulting value is stored in the
memory location of Variable on left
NOTE: An automatic type coercion occurs after
evaluation but before the value is stored if the
types differ for Expression and Variable
22
What value is stored?
float a;
float b;
a = 8.5;
b = 9.37;
a = b;
a
8.5
a
?
b
9.37
b
?
23
What is stored?
float someFloat;
?
someFloat
someFloat = 12;
// causes implicit type conversion
12.0
someFloat
24
What is stored?
int someInt;
?
someInt
someInt = 4.8;
// causes implicit type conversion
4
someInt
25
Type Casting is Explicit
Conversion of Type
int(4.8)
has value
4
float(5)
has value
5.0
float(7/4)
has value
1.0
float(7) / float(4)
has value
1.75
26
Some Expressions
int age;
EXAMPLE
age = 8
- age
5+8
5/8
6.0 / 5.0
float ( 4 / 8 )
float ( 4 ) / 8
cout << “How old are you?”
cin >> age
cout << age
VALUE
8
-8
13
0
1.2
0.0
0.5
cout
cin
cout
27
What values are stored?
float loCost;
float hiCost;
loCost = 12.342;
hiCost = 12.348;
loCost = float (int (loCost * 100.0 + 0.5) ) / 100.0;
hiCost = float (int (hiCost * 100.0 + 0.5) ) / 100.0;
28
Values were rounded to 2
decimal places
12.34
loCost
12.35
hiCost
29
Insertion Operator ( << )
• the insertion operator << takes 2 operands
• the left operand is a stream expression,
such as cout
• the right operand is an expression of simple
type, or a string, or a manipulator
30
Output Statements
SYNTAX (revised)
cout << ExpressionOrManipulator
<< ExpressionOrManipulator . . . ;
31
Output Statements
SYNTAX
cout << Expression
<< Expression . . . ;
These examples yield the same output.
cout << “The answer is “ ;
cout << 3 * 4 ;
cout << “The answer is “ << 3 * 4 ;
32
Using Manipulators
Fixed and Showpoint
• use the following statement to specify that
(for output sent to the cout stream) decimal
format (not scientific notation) be used, and
that a decimal point be included (even for
floating values with 0 as fractional part)
cout << fixed << showpoint ;
33
setprecision(n)
• requires #include <iomanip> and appears in
an expression using insertion operator (<<)
• if fixed has already been specified, argument
n determines the number of places displayed
after the decimal point for floating point
values
• remains in effect until explicitly changed by
another call to setprecision
34
What is exact output?
#include <iomanip>
#include <iostream>
// for setw( ) and setprecision( )
using namespace std;
int main ( )
{
float myNumber = 123.4587 ;
cout << fixed << showpoint ;
// use decimal format
// print decimal points
cout << “Number is ” << setprecision ( 3 )
<< myNumber << endl ;
return 0 ;
}
35
OUTPUT
Number is 123.459
value is rounded if necessary to be displayed
with exactly 3 places after the decimal point
36