Professional Documents
Culture Documents
Chapter 03
Chapter 03
Dale/Weems/Headington
1
Chapter 3 Topics
Constants of Type int and float
Evaluating Arithmetic Expressions
Implicit Type Coercion and Explicit Type
Conversion
Calling a Value-Returning Function
Using Function Arguments
Using C++ Library Functions in Expressions
Calling a Void Function
C++ Manipulators to Format Output
String Operations length, find, substr 2
C++ Data Types
simple structured
pointer reference
3
C++ Simple Data Types
simple types
integral floating
char short int long bool enum float double long double
unsigned
4
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 5
Samples of C++ Data Values
27000.0
-4
2.7E-4 means 2.7 x 10 =
0002.7 =
0.00027
7
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
int main ( )
{
float avgTemp ; // Holds the result of averaging
// FREEZE_PT and BOIL_PT
10
Function main Continued
return 0 ;
}
11
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
12
More C++ Operators
int age;
8
age = 8;
age
age = age + 1;
9
age
13
PREFIX FORM
Increment Operator
int age;
8
age = 8;
age
++age; 9
age
14
POSTFIX FORM
Increment Operator
int age;
8
age = 8;
age
age++; 9
age
15
Decrement Operator
int dogs;
100
dogs = 100;
dogs
dogs--;
99
dogs
16
Which Form to Use
dogs-- ; --dogs ;
17
BUT...
18
What is an Expression in C++?
An expression is a valid arrangement
of variables, constants, and operators.
20
Some C++ Operators
Precedence Operator Description
Higher ( ) Function call
+ Positive
- Negative
* Multiplication
/ Division
% Modulus (remainder)
+ Addition
- Subtraction
Lower = Assignment
21
Precedence
22
Associativity
left to right Associativity means that in an
expression having 2 operators with the same
priority, the left operator is applied first
expression 9 - 5 - 1 means ( 9 - 5 ) - 1
4-1
3
23
Evaluate the Expression
7 * 10 - 5 % 3 * 4 + 9
means (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 24
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
25
Mileage Program
/* This program computes miles per gallon given four amounts
for gallons used, and starting and ending mileage.
26
C++ Code Continued
const float AMT1 = 11.7 ; // Number of gallons for fillup 1
const float AMT2 = 14.3 ; // Number of gallons for fillup 2
const float AMT3 = 12.2 ; // Number of gallons for fillup 3
const float AMT4 = 8.5 ; // Number of gallons for fillup 4
int main( )
{
float mpg ; // Computed miles per gallon
cout << “the mileage per gallon is “ << mpg << endl ;
return 0;
}
28
Assignment Operator Syntax
Variable = Expression
float a;
float b;
a = 8.5;
a 8.5 a ?
b = 9.37;
a = b; b 9.37 b ?
30
What is stored?
float someFloat;
?
someFloat
12.0
someFloat 31
What is stored?
int someInt;
?
someInt
someInt 32
Type Casting is Explicit
Conversion of Type
int(4.8) has value 4
loCost = 12.342;
hiCost = 12.348;
35
Values were rounded to 2
decimal places
12.34
loCost
12.35
hiCost 36
Function Concept in Math
Function definition
f(x) = 5x-3
Parameter of function
Name of function
39
What is in a block?
0 or more statements
here
40
Every C++ function has 2 parts
41
Shortest C++ Program
type of returned value name of function
int main ( )
{
return 0;
}
42
What is in a heading?
type of returned value says no parameters
name of function
int main ( )
43
More About Functions
44
Where are functions?
located in libraries
OR
written by programmers
45
HEADER FILE FUNCTION EXAMPLE VALUE
OF CALL
sqrt ( b * b - 4.0 * a * c )
main function
Square function
Cube function
48
Program with Three Functions
#include <iostream>
int main( )
{
cout << “The square of 27 is “
<< Square(27) << endl; // function call
cout << “The cube of 27 is “
<< Cube(27) << endl; // function call
return 0;
}
49
Rest of Program
50
Function Call
51
Function Call Syntax
int main( )
{
DisplayMessage( 15 ) ; //function call
return 0 ;
}
53
A void function does NOT
return a value
// header and body here
54
Two Kinds of Functions
Value-Returning Void
55
<< is a binary operator
<< is called the output or insertion operator
STATEMENT
cout << “You are “ << age << “ years old\n” ;
56
<iostream> is header file
57
No I/O is built into C++
58
Manipulators
59
Insertion Operator ( << )
60
Output Statements
SYNTAX (revised)
61
Output Statements
SYNTAX
62
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)
63
setprecision(n)
int main ( )
{
float myNumber = 123.4587 ;
return 0 ;
}
65
OUTPUT
Number is 123.459
66
Manipulator setw
67
setw(n)
requires #include <iomanip> and appears in an
expression using insertion operator (<<)
int main ( )
{
int myNumber = 123 ;
int yourNumber = 5 ;
return 0 ;
}
69
OUTPUT
position 12345678901234567890
Mine Yours
123 5
70
What is exact output?
#include <iomanip> // for setw( ) and setprecision( )
#include <iostream>
int main ( )
{
float myNumber = 123.4 ;
float yourNumber = 3.14159 ;
return 0 ;
}
71
OUTPUT
12345678901234567890
Numbers are:
123.4000
3.1416
float x = 312.0 ;
float y = 4.827 ;
75
find Function
function find returns an unsigned integer value
that is the beginning position for the first
occurrence of a particular substring within the
string
76
substr Function
function substr returns a particular substring
of a string
81
C++ Code Continued
const float DISTANCE1 = 1.5; // First map distance
const float DISTANCE2 = 2.3; // Second map distance
const float DISTANCE3 = 5.9; // Third map distance
const float DISTANCE4 = 4.0; // Fourth map distance
int main( )
{
float totMiles; // Total of rounded miles
float miles; // One rounded mileage
82
// Compute miles for each distance on map
83
// Compute miles for other distances on map
84
cout << endl << “Total walking mileage is “
<< totMiles << “ miles.” << endl;
// ***************************************************
{
return float(int(floatValue * 10.0 + 0.5)) / 10.0;
}
85