You are on page 1of 25

00:38:56

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

Higher level programming language


00:38:29
3.1 Assignment Operations

Assignment operator
In C++, the equal sign (=) is called the assignment operator.

Higher level programming language


00:38:29
3.1 Assignment Operations

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).

Higher level programming language


00:38:29
3.1 Assignment Operations

/ / 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;
}

Higher level programming language


00:38:29
3.1 Assignment Operations

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.

Higher level programming language


00:38:28
3.1 Assignment Operations

Assignment variations:
The variable to the left of the equal sign can also be used to the
right. For example
sum = sum + 10

Higher level programming language


00:38:28
3.1 Assignment Operations

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.

Higher level programming language


00:38:28
3.1 Assignment Operations

Higher level programming language


00:38:28
3.2 Formatting numbers for program output

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;
}

Higher level programming language


00:38:28
3.2 Formatting numbers for program output

The output will be

6
18
124
−−−
148

Higher level programming language


00:38:28
3.2 Formatting numbers for program output

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;
}

Higher level programming language


00:38:27
3.3 Using Mathematical Library Functions

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

where g is the gravitational constant, equal to 32.2 ft/sec2 .

Higher level programming language


00:38:27
3.3 Using Mathematical Library Functions

# 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;
}

Higher level programming language


00:38:27
3.3 Using Mathematical Library Functions

Common C++ functions

Higher level programming language


00:38:27
3.3 Using Mathematical Library Functions

Example 3.5: The polar coordinates of a point consist of the


distance, r, from a specified origin and an angle, θ, with respect to
the x-axis. The points x and y coordinates are related to its polar
coordinates by these formulas:

x = rcosθ
y = rsinθ

Using these formulas, write a C++ program to calculate the x and y


coordinates of a point with the polar coordinates r = 10 and θ = 30
degrees. Verify the results your program produces by calculating
the results manually. After verifying that your program is working
correctly, use it to convert the polar coordinates r = 12.5 and
θ = 67.8 degrees into rectangular coordinates.

Higher level programming language


00:38:26
3.3 Using Mathematical Library Functions

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.

Higher level programming language


00:38:26
3.4 Program input

Cin statement:
The cin is used to enter data in a program while its running, and
then the value is stored in a variable.

Higher level programming language


00:38:26
3.4 Program input

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;
}

Higher level programming language


00:38:26
3.4 Program input

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.

Higher level programming language


00:38:26
3.4 Program input

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;
}

Higher level programming language


00:38:25
3.4 Program input

Example 3.8: Write, compile, and execute a C++ program to


compute and display the value of the second-order polynomial
ax2 + bx + c for any user-entered values of the coefficients a, b, and
c and the variable x. Have your program display a message first to
inform users what the program does, and then display suitable
prompts to alert users to enter the data.

Higher level programming language


00:38:25
3.5 Symbolic Constants

If the number ever has to be changed, the change need be made


only once, where the symbolic name is equated to the actual
numerical value.
Example
const double PI = 3 . 1 4 1 6 ;
const double PLANCK = 6.6256 e−34;
const double DENSITY = 0 . 2 3 8 ;
const i n t MAXNUM = 100;

Higher level programming language


00:38:25
3.5 Symbolic Constants

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 ;
}

Higher level programming language


00:38:48
3.5 Symbolic Constants

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;
}

Higher level programming language

You might also like