Professional Documents
Culture Documents
Chapter 3
Assignment, Formatting, and
Interactive Input
00:38:29
Content
• Introduction to C++
• Programming Style
• Data Types
• Arithmetic Operations
• Variables and Declaration Statements
• A Case Study: Radar Speed Traps
• Common Programming Errors
Assignment operator
In C++, the equal sign (=) is called the assignment operator.
Example 3.1:
Example: The volume of a cylinder is determined by the formula
volume = πr2 h, where r is the radius of the cylinder, h is the height,
and π is the constant 3.1416 (accurate to four decimal places).
/ / t h i s program c a l c u l a t e s th e volume o f a c y l i n d e r ,
/ / g iv e n i t s r a d i u s and h e i g h t
# i n c l u d e <iostream >
u s in g namespace s t d ;
i n t main ( ) {
double r a d i u s , h e i g h t , volume ;
radius = 2.5;
height = 16.0;
volume = 3.1416 ∗ r a d i u s ∗ r a d i u s ∗ h e i g h t ;
c o u t << ” The volume o f th e c y l i n d e r i s ”
<< volume << e n d l ;
return 0;
}
Coercion:
The value of the expression to the right of the assignment operator
is converted to the data type of the variable to the left of the
assignment operator.
For example, if temp is an integer variable, the assignment
temp = 25.89 causes the integer value 25 to be stored in the integer
variable temp.
Assignment variations:
The variable to the left of the equal sign can also be used to the
right. For example
sum = sum + 10
Counting:
The counting statement, which is an assignment statement similar
to the accumulating statement, has the following form:
v a r i a b l e = v a r i a b l e + fixedNumber ;
v a r i a b l e = v a r i a b l e − fixedNumber ;
The expression variable = variable + 1 can be replaced by the
expression variable++ or the expression ++variable. The
expression variable = variable - 1 can be replaced by the
expression variable– or the expression –variable. This is called the
increment and decrement operators.
Example 3.2:
# i n c l u d e <iostream >
u s in g namespace s t d ;
i n t main ( ) {
c o u t << 6 << e n d l
<< 18 << e n d l
<< 124 << e n d l
<< ”−−−\n ”
<< (6+18+124) << e n d l ;
return 0;
}
6
18
124
−−−
148
Example 3.3:
# i n c l u d e <iostream >
# i n c l u d e <iomanip>
u s in g namespace s t d ;
i n t main ( ) {
c o u t << setw ( 3 ) << 6 << e n d l
<< setw ( 3 ) << 18 << e n d l
<< setw ( 3 ) << 124 << e n d l
<< ”−−−\n ”
<< (6+18+124) << e n d l ;
return 0;
}
Example 3.4: The time it takes a ball to hit the ground after it has
been dropped from an 800-foot tower is:
p
T ime = 2 × distance/g
# i n c l u d e <iostream >
# i n c l u d e <cmath>
u s in g namespace s t d ;
i n t main ( ) {
i n t height ;
double tim e ;
h e i g h t = 800;
tim e = s q r t ( 2 ∗ h e i g h t / 3 2 . 2 ) ;
c o u t << ” I t w i l l ta k e ” << tim e
<< ” seconds t o f a l l ”
<< h e i g h t << ” f e e t . \ n ” ;
return 0;
}
x = rcosθ
y = rsinθ
Casts:
The operator used to force converting a value to another type is the
cast operator. For example, the following expression
int (a ∗ b)
converts the value of the expression a ∗ b to an integer value.
Cin statement:
The cin is used to enter data in a program while its running, and
then the value is stored in a variable.
Example 3.6:
# i n c l u d e <iostream >
u s in g namespace s t d ;
i n t main ( ) {
double num1 , num2 , p r o d u c t ;
c o u t << ” Please ty p e i n a number : ” ; c i n >> num1 ;
c o u t << ” Please ty p e i n a n o th e r number : ” ;
c i n >> num2 ;
p r o d u c t = num1 ∗ num2 ;
c o u t << num1 << ” tim e s ” << num2 << ” i s ”
<< p r o d u c t << e n d l ;
return 0;
}
The cin statement can be used to enter and store as many values
as there are extraction operators and variables to hold the entered
data. For example, the statement
c i n >> num1 >> num2 ;
results in two values being read from the keyboard and assigned to
the variables num1 and num2.
Example 3.7:
# i n c l u d e <iostream >
u s in g namespace s t d ;
i n t main ( ) {
i n t num1 , num2 , num3 ;
double average ;
c o u t << ” E n te r t h r e e i n t e g e r numbers : ” ;
c i n >> num1 >> num2 >> num3 ;
average = ( num1 + num2 + num3 ) / 3 . 0 ;
c o u t << ” The average o f th e numbers i s ” <<
average << e n d l ;
return 0;
}
Example 3.9:
# i n c l u d e <iostream >
# i n c l u d e <iomanip>
# i n c l u d e <cmath>
u s in g namespace s t d ;
i n t main ( ) {
c o n s t double PI = 3 . 1 4 1 6 ;
c o n s t double DENSITY = 0 . 2 8 4 ;
double r a d i u s , h e i g h t , w e ig h t ;
cout <<”E n te r th e r a d i u s o f th e c y l i n d e r ( in c h e s ) : ” ;
c i n >> r a d i u s ;
cout <<”E n te r th e h e i g h t o f th e c y l i n d e r ( in c h e s ) : ” ;
c i n >> h e i g h t ;
w e ig h t = DENSITY ∗ PI ∗ pow ( r a d i u s , 2 ) ∗ h e i g h t ;
}
c o u t << s e t i o s f l a g s ( i o s : : f i x e d )
<< s e t i o s f l a g s ( i o s : : showpoint )
<< s e t p r e c i s i o n ( 4 )
<< ” The c y l i n d e r weighs ” << w e ig h t << ” pounds ”
<< e n d l ;
return 0;
}