You are on page 1of 40

00:37:56

Chapter 2
Problem solving using C++
00:37:32
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:37:32
2.1 Introduction to C++

• C++ (pronounced cee plus plus) is a general purpose


programming language.
• It has imperative, object-oriented and generic programming
features, while also providing the facilities for low level memory
manipulation.
• Modular programs are easier to develop, correct, and modify
than programs constructed in some other manner.

Higher level programming language


00:37:32
2.1 Introduction to C++

Higher level programming language


00:37:32
2.1 Introduction to C++

Function as a small machine that transforms the data it receives


into a finished product.

Higher level programming language


00:37:32
2.1 Introduction to C++

Names:
• Functions
• Variables
Identifiers can be made up of any combination of letters, digits, or
underscores ⇒ rule?

Higher level programming language


00:37:32
2.1 Introduction to C++

Rules:
• The first character of the name must be a letter or an
underscore.
• Only letters, digits, or underscores can follow the first letter.
• A function name cannot be one of the keywords listed in
Keywords.
• The maximum number of characters in a function name is 1024.

Higher level programming language


00:37:33
2.1 Introduction to C++

Higher level programming language


00:37:33
2.2 Programming style

Higher level programming language


00:37:33
2.2 Programming style

The first two lines simply designate that ”the program begins here”,
and the last two lines designate the end of the program.

Higher level programming language


00:37:33
2.2 Programming style

The cout object


c o u t << ” H e l l o t h e r e w o rld ! ” ;
The cout (pronounced ”see out”). Its an output object that sends
data it receives to the standard display device.

Higher level programming language


00:37:33
2.2 Programming style

Preprocessor commands
# i n c l u d e <iostream >
The iostream file is part of the standard library that contains,
among other code, two classes: istream and ostream. These two
classes provide data declarations and methods for data input and
output, respectively.
The cout object is created from the ostream class. Therefore, the
iostream header file must be included in all programs using
cout.

Higher level programming language


00:37:33
2.2 Programming style

Namespaces:
• Namspace is a section of source code the compiler accesses
when its looking for prewritten classes or functions.
• Because the iostream header file is contained in a namespace
named std (for the standard library), the compiler automatically
uses iostreams cout object from this namespace whenever cout
is referenced.
• By using namespaces, ones can create their own classes and
functions with the same names the standard library provides
and place them in differently named namespaces.

Higher level programming language


00:37:33
2.2 Programming style

Higher level programming language


00:37:46
2.2 Programming style

Higher level programming language


00:37:46
2.2 Programming style

Example 2.1:

# i n c l u d e <iostream >
u s in g namespace s t d ;
i n t main ( )
{
c o u t << ” H e l l o t h e r e w o rld ! ” ;
return 0;
}

Higher level programming language


00:37:46
2.2 Programming style

Example 2.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 << ” H e l l o t h e r e \ nworld ! ” ;
return 0;
}

Higher level programming language


00:37:47
2.2 Programming style

Line comments:
• Comments are explanatory remarks made in a program.
• The compiler ignores all comments.
• A line comment begins with two slashes (//) and continues to
the end of the line.

/ / T h is produces th e d i s p l a y
c o u t << ” H e l l o t h e r e w o rld ! ” ;

Higher level programming language


00:37:50
2.2 Programming style

Block comments:
• Block comments begin with the symbols /* and end with the
symbols */.
• The compiler ignores all comments.

/ ∗ T h is i s a b l o c k comment t h a t
spans
three l i n e s ∗/

Higher level programming language


00:37:49
2.2 Programming style

Example 2.3:

# i n c l u d e <iostream >
u s in g namespace s t d ;
i n t main ( ) {
cout <<”15.0 p l u s 2 .0 equals ”<< (15.0+2.0) < < e n d l
<<”15.0 minus 2 .0 equals ” << ( 1 5 . 0 − 2.0)<< e n d l
<<”15.0 tim e s 2 .0 equals ” << ( 1 5 . 0 ∗ 2.0)<< e n d l
<<”15.0 d i v i d e d by 2 .0 equals ” < <(15.0/2.0) < < e n d l ;
return 0;
}

Higher level programming language


00:37:49
2.3 Data types

Higher level programming language


00:37:49
2.3 Data types

Signed and unsigned data types

Higher level programming language


00:37:49
2.3 Data types

Floating-Point Data Types

Higher level programming language


00:37:34
2.3 Data types

Determining storage size: ⇒ using operator sizeof().


Example 2.4:

# i n c l u d e <iostream >
u s in g namespace s t d ;
i n t main ( ) {
c o u t << ” \ nData Type Bytes ”
<< ” \ n−−−−−−−−− −−−−−”
<< ” \ n i n t ” << s i z e o f ( i n t )
<< ” \ nchar ” << s i z e o f ( char )
<< ” \ nbool ” << s i z e o f ( b o o l )
<< ’ \ n ’ ;
return 0;
}

Higher level programming language


00:37:34
2.4 Arithmetic operations

Higher level programming language


00:37:34
2.4 Arithmetic operations

Operator precedence and association:

8 + 5 ∗ 7%2 ∗ 4 = ???

Higher level programming language


00:37:34
2.4 Arithmetic operations

Operator precedence and association:

8 + 5 ∗ 7%2 ∗ 4 =
8 + 35%2 ∗ 4 =
8+1∗4 =
8+4 = 12

Higher level programming language


00:37:34
2.4 Arithmetic operations

Operator precedence and association:

10.0 + 15/2 + 4.3 = ???


10.0 + 15.0/2 + 4.3 = ???
4.0 ∗ 4%2 = ???

Higher level programming language


00:37:34
2.5 Variables and declaration statements

Declaration Statements:

dataType variableName ;

Higher level programming language


00:37:34
2.5 Variables and declaration statements

Example 2.5:

# i n c l u d e <iostream >
u s in g namespace s t d ;
i n t main ( ) {
double grade1 ; / / d e c l a r e grade1
double grade2 ; / / d e c l a r e grade2
double t o t a l ; / / d e c l a r e t o t a l
double average ; / / d e c l a r e average
grade1 = 8 5 . 5 ;
grade2 = 9 7 . 0 ;
t o t a l = grade1 + grade2 ;
average = t o t a l / 2 . 0 ; / / d i v i d e th e t o t a l by 2 .0
c o u t << ” The average grade i s ” << average << e n d l ;
return 0;
}

Higher level programming language


00:37:34
2.5 Variables and declaration statements

Multiple declarations:

double grade1 ;
double grade2 ;
double total ;
double average ;
Single declaration:

double grade1 , grade2 , t o t a l , average ;

Higher level programming language


00:37:34
2.5 Variables and declaration statements

Example 2.6: A highway-patrol speed detection radar emits a


beam of microwaves at a frequency designated as fe . The beam is
reflected off an approaching car, and the radar unit picks up and
analyzes the reflected beam, fr . The reflected beams frequency is
shifted slightly from fe to fr because of the cars motion. The
relationship between the speed of the car, v, in miles per hour
(mph), and the two microwave frequencies is
 
8 fr − fe
v = (6.685 × 10
fr + fe

where the emitted waves have a frequency fe = 2 × 1010 sec−1 .


Using the given formula to calculate and display the speed
corresponding to a received frequency of
fe = 2.000004 × 1010 sec−1 .

Higher level programming language


00:37:34
2.5 Variables and declaration statements

# i n c l u d e <iostream >
u s in g namespace s t d ;

i n t main ( ) {
double speed , fe , f r ;
f e = 2e10 ;
f r = 2.0000004 e10 ;
speed = 6.685 e8 ∗ ( f r − f e ) / ( f r + f e ) ;
c o u t <<”The speed i s ”<<speed<<”m i l e s / hour”<<e n d l ;
return 0;
}

Higher level programming language


00:37:34
2.5 Variables and declaration statements

Example 2.7: Design, write, compile, and execute a program that


determines the work performed by a piston engine providing a force
of 1000N over a distance of 15 centimeters. The following formula
is used to determine the work, W , performed:

W = Fd

• F is the force provided by the piston in Newtons.


• d is the distance the piston moves in meters.
Manually check the values computed by your program. After
verifying that your program is working correctly, modify it to
determine the work performed by six pistons, each providing a
force of 1500N over a distance of 20 centimeters.

Higher level programming language


00:37:35
2.5 Variables and declaration statements

# i n c l u d e <iostream >
u s in g namespace s t d ;

i n t main ( ) {
double speed , fe , f r ;
f e = 2e10 ;
f r = 2.0000004 e10 ;
speed = 6.685 e8 ∗ ( f r − f e ) / ( f r + f e ) ;
c o u t <<”The speed i s ”<<speed<<”m i l e s / hour”<<e n d l ;
return 0;
}

Higher level programming language


00:37:35
2.5 Variables and declaration statements

Example 2.8: The stress placed on the fixed end of a symmetrical


steel I-beam can be determined by this formula:
Ldc
S=
I
• S is the stress.
• L is the weight, in lbs, of the load placed on the beam.
• I is the beam’s rectangular moment of inertia in units of in4 .
• d is the distance, in inches, the load is placed from the fixed end
of the beam (technically referred to as the ”moment arm”).
• c is one-half the height, in inches, of a symmetrical beam
(c = h2 ).

Higher level programming language


00:37:35
2.5 Variables and declaration statements

Higher level programming language


00:37:35
2.5 Variables and declaration statements

• Determine the units of stress, S, by calculating the units


resulting from the right side of the formula.
• For a steel beam having a rectangular moment of inertia of 21.4
in4 and a height of 6 inches, determine the stress for a load of
700 lbs placed 8 feet from the fixed end.
• Check the values computed by your program by hand.

Higher level programming language


00:37:35
2.5 Variables and declaration statements

Solution:

Ldc lbs × in × in lbs


S= = 4
= 2
I in in

Higher level programming language


00:37:38
2.5 Variables and declaration statements

# i n c l u d e <iostream >
u s in g namespace s t d ;

i n t main ( ) {
f l o a t bst , bwe , bdt , bhi , bmm;

bmm = 21.4;
bhi = 6;
bwe = 700.0;
bdt = 8.0;

b s t = bwe∗ b d t ∗12∗ b h i / 2 /bmm;

return 0;

}
Higher level programming language

You might also like