[PDF]

mf
~{||}
cd
p
ep
g
k ei
un
mn
mp
o
o
z
pg yf
n
re
t xi
f
of
wh
m op
vg op
k
mi
k ou
pt sq
io
p
ng
ri q
k
mf
n op
g
lf
ki j
i hi
g ef
UV
XW
Y
bT a
TX
]
RX
` _^
\ Z[
SR
ST
STR Q
1
A
EL
KJ
F
3M
0N.
O
,
H AB
I
<
8, 1
89
8G
9
9
8, E
P
?
A
A
ABC
? <=>
@
3:;
D
-897
65,4
/0.
21
,
3
-,*+
)
)(
'
$
& %
$ "#
! €
€
€
€
€
€
€
€
"
+ +
<
<
€
€
€
; +
* A <
!
* (
"
=> !
?@ !
<
* €
€
€
"
€
€
21
€ € € € €
"
€
%
&
( ' '
) * €
€
€
€
€
"
"
"
€ € € € € € ;
"
€
(
) 3 # - ; * * *
! 1 * , $
€ € € € € € "
€
# ! "
€ € € € € $€ 3 # +
"€ " "
€ €
€ €
€
) *
9
€ € € € € € € $ "
€
) 3 # C B
* , * D ! E $$ $ $ $ $ $
€$
€ € €
€
- * +
! * * , 1
€ € F
€
: - )
) G 1
HI
* , 1 HI € € €
"
0 - .
! * */
3
€ € € € €
"
F
- . )
+ * *
,
G 1
1 " $ $
€ € € € €
$ €
, 0 + .
)
* 12 1
& , Foundations
€ € €
- ( & , Contents
Introduction
€ € € €
$ "
) 3 #
* 2
€
)
)
:
*
,
; - . %
* €
"
, )
"
) * , * * ! +
€
€
% € € € € € € € €
€ € € € € € €€ €
"
Contents
67
8
67
9
45
€
€
€
€
€
€
€
"
€$
""
€
""
€
€
€
€
€
- € $
.
! * "€
€
€
€
€
€$
€
"
€
"
+
) 3 # :
* ?
1
& **
"
€
€
€
€
(
*
€
H ( (
* * & * #
" " " " $" " " " " "
"
€
+
# ; : % # % %
* ? ?
, * !
* * * * ! ? !
, * *
*
1
" $ $ " € " " 3
) * €$
" **
"
€
€$
€
€
€
) *
€
€
€
3 #
€ € € €
$ €
€
€
€
) *
€
€
€
€
€
3 #
"
€
0
€$
(
€ € $€ € € €
"
€
" " %
€
+
% € € € € € € € € € € € € € € €
Compound Types
€
"
€ " "
€ € $€ € € € € € € €
€
"
) 3 : * ? ?
?
1
! )* ? 1 ,
, *
*
& * *
1
1 !
*
!
* € € € € €$ € € € € € €$
" " " "
%
$ &
€
"
3 # : % F - F F ' '
* *
* * ?
!
*
1 , *
! &
! * * * * $ $ €
€
$ " € €
€
:
#
:
* ? ! * 1
1 * * 1 &
&
! * * , ,
* €
) *
$ $
169
€ € *
€
- % *
? * A € € €
€ €
Functions
# 1
€ €$
" € € €
* ;
*
, *
* , * € € €
" €
3
4
231
"
67
8
67
67
8
67
9
45
9
45
A C++ Operators
291
Index
294
€$
€
3
€ €
"
)
*
67
8
67
67
8
67
9
45
9
45
$
*
0 B
H * * ; ! * , *
1 H ! 1
;
+
* @ - * 1 1 1
H *
A *
* ) *
H , ,
1 & * * ) *
*
! ? * 1
1
* 1
*
1
H
H 1
H C
*
1
H
- H
1
, !
(
1
*
!
!
! :
) * H 2
A ! *
A
1
A A
- %
! * ? , ?
1
?
* *
*
A H
1
1
H * , , A
HI ,
<
*
*
! !
* ?>
A * A 1 *
A *
* !
! +
!
* *
1
*
1
*%
*
H
H
1 H
? 1 1
IH 1 ! H$
*
C
!
3
! H
-
*
" " " A
8
.9
H :
;
* H
= <
=
"
!
*
! 6 -
) 6 1 !
,
* !
! , IH 7 * : !"
!
* H 7 1 , 1 1
1
3
+ !
2
1
IH
*
4 H * /5 H *
% 0
! *
!
*
2
+,
-
)* (
!
!'
1
$ &
$0 !
%
.
# $ /
!
$ 0
!
! !
! "
" !
Introduction
H
H
)
* )
1 * IH
*
1
A , H $# !
" "
!
" !
#
Chapter 1
!! *
)
IH ! *
* *
1.1 Why learn programming?
6
5
5
74
67
8
7
4
* *
A
1
267 − 1
1
22000
7
*
!
, A
0
!
%
:
, 6 6
*
H
* H * 1
1
,
1 * * 7
7
, 3
* A * $
? * Maple
H
!
* ? * * !
A
!
*
H , )
*
*
A
0
, H
B A
@
!
1
H
,
!
7
0
*
A
!
A
8
&
H H
A
1
*
!
! 1
*
, ,
1 1
6
0
* 0
*
,
! ?
:
1
*
* 1
IH
< -
)
H " H € 0 :
A
, *
H * *
1
* 6
A
1
- 7
*
;
1
)
A
+
;
,
&
+
1
*
2
" * H 1
* *
* 2 A
, &
H
*
,
!
IH !
* H *
*
:
2
.
#
!
*
6
,
(
H 7 H * !
* 1
1
#
#
#
1
A *
, & ! ,
* ! 1
, 1
** ,
1
< -
H
,
* * H
:
*
!
1 9
! ! * +
H"
F
*
A
* :
*
0
!
A
267 − 1 = 761838257287 193707721
267 − 1
H !
*
B ! 1 *
21000
6
1 ! 1
B *
# H 0
! A *
#
,
.
0
1
147573952589676412927
*
Problem skills.
267 − 1
- *
D E A
!
*
*
1 , * A ! * ,
IH
* C*
* Tool skills.
1
*
H
&
1
! , )
<*
1 )
7 1
1 !
* *
, !
* 6
, *
n = 19
,
, * 1 H *
* 1 1 A ,
; ! F
* *
147573952589676412927
,
!
1
0
*
$ H
F
A
!
*
*
H
!
, +
€
267 − 1
6
*
* 7
*
267 − 1
1 H
* 1 * * A (
1 *
H F
*
& !
0
1
* A
* 267 − 1
761838257287 x 193707721
-----------------------761838257287
6856544315583
2285514771861
5332867801009
267 − 1 =
1
231 − 1 = 2147483647
2n − 1
1
* *
A
! 1
1 Mersenne’s Conjecture.
n = 2, 3, 5, 7, 13, 17, 19, 31, 67, 127, 257
n < 257
A
A ! A
1
HI 1 * A
A 1 H
&
A
*
- )
* IH
*
** H * * ,
1 A , !
-
!
*
)
IH
*
*
,
C
" - *
* *
1
IH
! ! !
*
*
HI 1 * 1
! 5332867801009
5332867801009
1523676514574
761838257287
--------------------147573952589676412927
Maple
ifactor(2^67-1);
n=7
(761838257287)(193707721)
€
5
6
5
74
67
8
4
5
7
6
6
8
www.mersenne.org
.
+
! * 1
1
A 1
+
H
H
1
< !
*
3
!
H
3
!
1 *
*
* IH
* !
1 , 3
1 * ; H
*
) *
1 ** * !
!
!
*
+
!
* A ,
, !
%
: ! ! ( !
1
1 *
€
*
B
H
, & c
; A
:
< A "
A
1 :
H , : , IH
*
D
! C : ! !
E !
A +
* ) /? ? )
n = 67
*
*
The art of computer programming.
* -
" H !
"
n = 61, 89
(
6
*
* !
1
, H A 7
*
!
! 1 !
< ! ,
! *
? *
,
- ! ,
*
!
, ! , 1
A #
. # < 0
*& * , <
IH
- 1
H ?
*&
A
*
1
1
?
*
*
Programming Skills.
$
H "
!
,
,
1
!
" "
% !
, *
1 1 IH
, >
*
,
C*
A
H
0
* +
! !
A * 1
1 1 * 1 H *
!
1
* !
* * , *
*
1
! 6
1 *
&
7
Maple
n = 257
107
n = 43, 112, 609
!
3 1
A
? !
!
1
#
" #
"
!
" !
!
!
.
* E
H
*
*
H
* 1
1
!
1
*!
!
6 #
!
!
.
1
+
.
*
* * €
1 H $ 1 !
.
.
€
*
* 1 ! * ! * 6 A ! * 1 ,
- 1
1 !
! A 7 * 1
!
*
1
, .
& .+
6 .+
+
C ! 1
1 ! 7
.
< +7
0
1
*
!
,
&
H
D1
1
*
- !
$ !
$ !
/
$
$ 0
"
"
!
*
! * 1 1
*
A
:
H 1 * A *
, *
A )
2n − 1
$ 2
&
1 ! * *
:
+!
,
! A
H *
1 1
H
, n
//www-cs-faculty.stanford.edu/~knuth/taocp.html
http:
5
6
5
74
67
8
4
5
7
6
6
8
%
"
!
&
6
? ,
7
!
1
(
B
!
* H
(
*
F ! H
!
H ! A
*
! - ?
A * < *&
@ IH
A
+
E
+
!
, ! +
* +
1
H
1
<
A
D
+ 1
,
&
!
*
*
!
+
D
A
1 *
* - * ! E
!
*
*
*
B A
, 1
H
, A * * 1
A
H <
* G
*
E
2
&
* 1
1
1
, - *
? * *
* &
A
1 * 1
, * ! H 6 , ? , 7 1
1
!
IH *
*
, * +
H !
!
A
* !
D* ? !
A
* *
1 C
H H 2 ,
! * * *&
*
%
*
H )
HI *
!
, %
H
* *
; 1
,
*
A !
%
;
! * 1
! /
1
,
*
1
*
!
!
;
*
HI
*
!
" H
*
2
" <
$ !
0 ! " ! " !
2 " $
0
2
"
!
/
!
Sourcecode
/
! !
2 *
0
* "
# 0
"
!
0
$ 0
!
/
.
Compiler
, # ! $
}
+
H
< /
- 1 6 .
9
3
+,*
-
)(
.
0
0 %
" 0 " $ 2 0
3 ! " 3 $! !
4 $
// computation
int b = a * a;
b = b * b;
$
!
* 7
1 )
!
* ! C
H *
* !
1 *
! H ) *
; *
! ,
int main()
{
// input
std::cout << "Compute a^8 for a =? ";
int a;
std::cin >> a;
*
1
1 , A
1 **
* <
%
* * 1
H
*
D E 1 H #include <iostream>
1 !
+
* H !
* H
1
1
A 1 H *
H ! * * % *
*
H // Output b * b, i.e., a^8
std::cout << a << "^8 = "
<< b * b << "\n";
return 0;
) ? )
-
* )* !
IH , * * H
* !
*
4 Figure 1:
- :
/ A
! "
" ,
* 1
* * *
* ,
* 1
*
!
, , *
, C // Program: power8.C
// Raise a number to the power eight.
#
#
!
, H
*
1
A
1
C
1.2.2 Compiler
*
: , *
H
* * * , , * A
1 1 !
* *
/
,
*
!
-
1
1.2.1 Editor
HI H ; & !
! *
* &
*
* %
! /
% *
? 1.2 How to run a program
MZ ÿÿ@
U‰å]é×È
U‰åƒì ƒäð¸
‰Eô‹Eôè'Ë è‚×ÇD
0C Ç $ÀeC è.ë
Eü‰D$ Ç $ eC èKàD$ 0
Ç $ÀeC è ë
Eø‰D$ Ç $ eC è$à ÇD$ <0
Ç $ÀeC èàê ‰Â‹Eü‰D$ ‰ $è
ÇD$ H0C
‰ $è¿ê ‰Â‹Eø‰D$ ‰ $èn
ÇD$ N0C
‰ $è ê ‰Â‹EøEü‰D$ ‰ $èJ
ÇD$ S0C
‰ $èzê ÇD$ óB ‰ $èZúà U‰åƒì } ÿÿ
u ƒ} u Ç $ `C èÑ
} ÿÿ u ƒ}
u Ç $ `C èF
ÉÃU‰åƒì ÇD$ ÿÿ Ç $
è¨ÿÿÿÉÃU‰åƒì ÇD$ ÿÿ Ç $
ÿÿÉÃ
U‰åSƒì ‹] ‹M Ç
˚JC ‹@ôŠ p9C ˆ h
‹ R S RèÖ Æƒ
ƒÄ ‹]ü‰ì]à ´&
¼'
U‰åSƒì ‹] ‹M Ç
˚JC ‹ @ôŠ p9C ˆ h
‹ P S RèÏÕ Æƒ
ƒÄ ‹]ü‰ì]à ´&
¼'
U‰åƒì ‹U Ç ˚JC
RèûY ƒÄ ‰ì]à t& U‰åƒì ‹U Ç ˚JC RèÛY
ƒÄ ‰ì]à t& U‰åSƒì ‹] Ç
˚JC SèºY ƒÄ ‰] ‹]ü‰ì]éyX
´&
U‰å‹E ƒÀ ]à t& U‰åWVSƒì<ŠM ˆMÇÇEàp @
ÇEäP C Eô‰EèÇEì @ ‰eð Uȃì RèWÉ
ƒÄ }Ç • âÿ
‰UÀÿMÀ eÀ
}Ç „
Executable
"
5
5
6
5
74
67
8
4
5
7
6
7
5
%
*
* &
*
?
* *
!
*
*
* &
! # *
H
* *
- ! !
! H A
1
, IH
- G
A 1
! 1 * * &
€ & *
H * * 1
-
*
D H 1
*
E ? 0 ) * )
1 +
* < * A H )
* H
€
* ! C * & , 1
? 1
! *
@ * H
* 1 A
! $ 32
32
*
*
** 1
* ! 6 D
, * 1 E 7 !
*
! A
* B <
- ,
*
A
HI
* *%
!
! H *
* *
, * *
& *
*
! * B $
1 * -
HI ( H A * H H & *
, , * )*
* * H *
* H *
* !
*
! 1
!
* 1 !
1 1
H ! +
{0, 1}
?
A
*
G
,
* !
A
H
H
H *
6
H €
7
1
!
*
( *
H 0
4
F H ) : H
/
H ,
9
*
' , 1
*
* A
H
*
/
"
!
* H , H *
* H &
,
* HI
!
* D E H
&
* *
A ! )*
" $ +
6 (7 D
H E
D*
1 * E
)*
1
@
*
A
!
/ *
# A
!
G
> *
!
H
* >
*
H % *
&
H *
* D * * E 1 A ! 1 H
)
*
H ?
,
Main memory.
*
H
* ** * ! 1
! *
A **
1 B +
*
? H G
, *
1
,
! !
A * !
* 1 H
* 1 *
!
** *
*
1 A ! A
!
!
+
H
*
1
1
* *
H
1
&
)
? *
,
H 1
H
*
C 7
H
%
6
H
* *
, H
A *
*
!
: H
*
IH H , * , H * * - * ! * !
) & !
! !
+!
* A ; *
H 1 * *
*
1 ! ? * 6
A
* - 1
*
*
! * H 7
! *
* *
* -
* !
1
*
! % !
* !
*
* , , A *
* * * ! *
!
<
, *
*
1
H :
$
%
*
, 7 *
! *
" 6
- ? & 1
,
C
H *
!
*
IH
* ! * H < )* *
* *
C D1
* ! E
1.2.3 Computer
*
1
-
@
*
*
A
!
*
* A
*
! 1
)
)*
,
1 *
1
* * A
*!
IH
*
!
Processor.
1.2.4 Operating system
1.2.5 Platform
*
**
*
? ,
*
*&
*
!
1
5
5
6
5
5
74
67
8
7
4
5
6
7
!
1
* !
,
"
! * A
, * !
* A
H * 1
*
*
? 0
H
!
A
1
* ) * *
+
*
*
!
*
* *
*
7
6
**
!
0
A
H *
1
? ,
*
< *
+
! * H !
! *
! *
*
* 1 & * * * !
/ H , C A
*
? * * & A *
*
1 * 1 * H A #
H
H
,
1
* /
-
) * * A !
? *
! 1
* * % IH
* ? !
*
* & H * H
1
,
6
A *
7
*
!
*
1
*
* ? H
!
* *
*
* *
A
!
* 1
! 1 ) H
1
, 7 *
- !
**
C ! *
6
* :
*
, H * *
A
-
*
A
*
7
* H * -
, * !
! ,
6 H !
) *
- , % !
! ! , ,* H * !
* @?!
!
* +
*
H H
!
! .
A A
! , !
*
1 !
* A A
, 1
A 1
H
, H C
! *
*
)
H H
! H
)
A
*
*
* @
*
D 1 ,
C E *
* * *
/ *
* * 1 * 1 * F
H 1 ,
* ?
!
1.2.6 Details
1
!
, H
,
*
*
,
%
IH * *
! !
** * ! H A , * 1
!
* *
+
A , *
! IH
1 ? * *
!
*
, * * !
* & ! 1
,
H ,
1
A
!
?
* *
* 1
/
!
!
,
, H
!
,
!
#
1
!
*
H *
H @ *
*
:
1
H ! *
* !
! A
*
,
!
?
* *
) , H
H
*
?
!
! ,
A & H *
H
!
? *
? , * &
1
! ,
* ! 1
:
?
, *
, *
+
*
*
!
H
1
!
1
* 1
H *
!
* * * *
! ,
1
! IH
6 A €
IH
1
!
* *
7 * 1 * H "
!
,
€
, *
€
$
5
5
6
5
74
67
8
4
5
7
6
7
5
€
2 +
0
<
Program 1:
!
{
*
A
A
6
,
H
1
7
*
H
, ! H ! ' ' <
* IH
!
* H *
*
!
?
A
1
H
A
*
&
+ ! '
'
, , +
A
' !
' 1
1 !
, }
"
!
=
=<
! 2 $
.
* !
* ! *
Foundations
} { | } // Program : power8 .C
// Raise a number to the eighth power .
# include < iostream >
int main ()
{
// input
std :: cout < < " Compute a ^8 for a =? ";
int a;
std :: cin > > a;
// computation
int b = a * a ; // b = a ^2
b = b * b;
// b = a ^4
// output b * b , i .e . , a ^8
std :: cout < < a < < " ^8 = " < < b * b < < " .\ n";
return 0;
.
+ -
)* (
:
$
0
!
.
3
8
0
&
!
2
%
Chapter 2
"
!
{|
2
0
0
! !
2
.
# 0 #
$# !
#
$ " # 0 $ !
1
2.1 A first C++ program
a8
a
€
€
5
69
5
7
6
8
7
4
,
2.1.2 Comments and layout
1
,
H 1
H H
*
&
,
&
1
)
* H & (
*
* * €
! * % * & * * A
H ) 1
*
! ! * * !
* C
& ? H
& *
* * !
, ! * * & *
&
! ! ! *
1
1
! *
* -
A
1
, !
!
? 1
,
-
** !
D ! !
E H *
A
*
H *
B
!
/
/
(
1
A ,
IH
,
* ,
! ' ' !
Unspecified and implementation defined behavior.
!
)
IH *
b
+
b
&
E H
- ,
* * ) ' ) * '
! * ! *
* *
* !
& * !
D
* & 1 *
a8
*
+
0
$
!
*
, -
-
H * &
*
!
*
*
H 6
' ' 1
A
7
1 ! )
*
' ' 1
1 H A
) )
F * * *
H
,
# *
H
* * ! , ' ' 1
=" €
!
*
;
H ,
*
C
*
A
!
) * HI 1
+( + -
€ ! *
$$ !
'
'
*
,
1
* 1 1 6 7 B
0 * *
* -
1 1 * ! *
* - B ?
*
1
$ - , ** ! B
H ,
* A
* !
< * 1
HI H !
* * ) * * * 1
, 1 , * * * * * F
H * = *
* A ! 1
)
a
*
2.1.1 Syntax and semantics.
1!
* * A
1 1
, ,
H *
H ' ' * (
€
* +
! !
* &
!
*
?
b * b
H H * H
= IH ,
- , 1 1
* = , -
, A
* ! * * A
*
+
+
H
H
,
&
1
H a
,
1 * *
*
*
* 0 * !
*
* H a
-
IH 0
!
iostream
+
power8.C
*
//
*
1
)
1
H H
,
H
,
A
1
H
@
*
* H
! H €
A
1
%
!
- &
1
) ,
! ' '
*
&
!
)
! * H -
*
*
/ B
* * !
Compute a ^8 for a =? 2
2^8 = 256.
, * H 1
*
a * a
*
!
H
power8.C
H
,
- 6D !
! H
$ / ! A
'
E
H 7 ' @ ! *
*
A 1 * ! ! ' '
, * ! , ? !
% **
'
'
)
* * 1
int
/ + A * * *
<
b * b
! !
,
*
6
*
* H
A
7
C
*
b
$
*
H
*
a4
&
)
*
*
)*
+!
! 1
* &
H ?
*
1
*
H * &
!
* *
%
! A * * C 1 ,
1 , *
* B C
* IH
1
, * & H !
! !
IH
A 1 ! 1
, H *
Compute a ^8 for a =?
€
€
5
69
5
7
6
8
7
4
5
4
79
*
!
*
+
!
* ! * H
* ! !
*
!
!
! *
- 1
* / 2.1.3 Include directives
- " !
* " 1
" !
! * * , * / ' '
! # A
'
main
!
6
! - * ; ! *
*
1 H
,
1
, *
A
1
1 7
* 1 ,
1
, %
H
*
main
()
*
* 1 ,
! *
)*
, !
, 1 * H
A 6 *
7 B
-
A B
1
* !
#
* H H
! * D
*
, E
main
!
*
* 1
* 1
*
H
1
*
-
!
!
*
, , '
'
<
2 +
0
!
"
!
main
!
*
)
*
,
+
&
, *
, C
* *
- ! ) * !
{ H *
E
D
%
* & * /
* !
*
*
1 ! C
H
)
! * *
1
* ,
!
! *
%
& C &
* HI
€
1
HI A 1 ,
A ,
HI *
IH *
A , $ #
!
A 1 7 - *
H * A 6
, !
% ? !
1 '' * 1
1 *
- !
H *
** ! !
!
1 *
H !
! *
% * > HI , 1
A
!
! , !
A !
> *
1
? +
B
* *
H !
, 1 ! ,
* ,
!
* 1
6 !
1 7 *
,
! +
*
H - * *
! * , ! < H A ,
* 1
- 1
! ?
#include
H 7
,
)
* H
0
main
! 6
H
A
* **
'
1
1
*
A
1 !
1
! 1
1
1
A
H H A
1 !
*
*
power8.C
H
1
H
* 2
* ,
1
* A
* *
1
, !
2
*
, A
* !
1
1 *
1 H ! * 1
!
H # include < iostream >
int main (){ std :: cout < <" Compute a ^8 for a =? ";
int a; std :: cin > >a ; int b= a*a; b=b *b; std :: cout < <
a < <" ^8 = " <<b *b < <" .\ n"; return 0;}
"
# include < iostream >
include
,
Program 2:
1
* 1
H A
, *
!
, A * * *
B
power8.C
H
*
, H
* 1
A
*
! ! *
1 !
H * * 1
*
+
*
IH *
1
H *
,
% 1 1
!
! * * 1
! !
,
!
6 *
*
, 7 ! * )*
*
1
H ) 1
!
+
*
!
1
H *
!
**
*
!
,
H
' '
H % $
? *
* $
* * ! * !
,
*%
, ! * *
!
) * //
!
, !
*
H * H ' '
H ,
1
! !
!
*
*
, +
? // Program : power8 .C
// Raise a number to the eighth power .
#include
power8.C
iostream
std
std::cin
2.1.4 The main function
int main () { return 0; }
main
return 0;
main
return 0
int
power8.C
"
€
€
5
69
5
7
6
8
4
5
4
7
79
*
1
A
* 12
2
( !
2 - . #
#
" $
)
_
* 2
1
22
H H
**
A !
&
H
H
! <
!
,
* A H
1 !
, )
, 1 H
, ! 1 , * ( D
E
€ A *
-
+
!
' '
* HI 1 * a
H
2
22
2
1 !
/ ,
1
! * 1
!
*
* *
1
* ! *
!
* &
6
!
* ,
7 +
' ' 6
*
H 7
1 2
1
'
'
22
2
9
%
- *
*
' , ' ) ! *
& IH
!
, )
*
) H '
' * 0
1 *
, %
* 12
% 1 2 * !
* HI A
H , ! 1
6 1 std::cin
identifier
Z
IH
2.1.8 Variables
*
& H
, 12
* A
1 * 1 * 7
2.1.7 Literals
H H
!
*
!
int a;
+
,
* 1
2
* %
*
A
* 12 )
!
H
1
# $ 0
& A
D
* * H E
'
' H
!
H H "
a
!
,
*
*
1
,
!
*
,
* 1
A ,
1 # !
* *
H 1
A
* , A
H 2
1 A
* & ,
! !
* ;
& *
0 , ,
1 < A A H
( !
*
* ,
* /
*
*
H
&
,
!
* @
F
6 )
H , 3
* 0
' 7 '
H
+ *
*
a
* 12
) <
1
,
, 1
*
1
1
!
C *
! Definition 1
a z
A
,
&
* 1
*
(
* / -
,
+
* * /
*
H ,
!
! !
* * / ! * 1
* ' '
* 1 :
, A
int
& !
, 2
/
*
1
a
1
, * !
1 HI &
* !6
1 )
!
, H 1 *
!
+ ( ! 7
1
A
! ;
,
,
H !
' *
' * !
1 ! * # $
!
6
*
1
? 0
*
* H *
*
* 1 C
*
, !
€
1 H 1
H
%
0
!
,
H
< A
,
,D
1
E
*
! ,
!
, F
)
F H * ! $
!
D 7
!
* * E * int
* *
*
%
power8.C
( , +, )
H
&
H
A
A
,
int
1
*
int
2.1.6 Types and functionality
-
int
int
!
, %
1
*
*
, !
!
( , +, )
A
A -
,
1
B * H A $
! H !
H 6
* ! *
power8.C 0
-
7
,
1
0
1
* A
1 ,
*
! -
+
* !
*
H * *
* ) & , - !
* !
,
! 1
H
* * * 1
,
1
2.1.5 Values and effects
int a;
a
a
2
2.1.9 Identifiers and names
int
std::cout
2.1.10 Objects
€
€
5
69
5
7
6
8
7
4
5
4
79
Evaluation of composite expressions.
*
B
1
) H
*
, H
A
,
)
6
7
1
H 7
1
,
, *
6
2
& *
* 6 )
* ,
, * / ,
,
,
+!
, - 1 * , 1
!
+! )
* * , ) H $ !
, H
:
* 12
!
, H
B
*
&
!
*
!
, ,
)
H
) H
H
* /
)
-
1
!
6
H 7
# &
- IH
$ !
+
* * H * , B * * * , , !
*
/ &
*
H **
?
6 1
!
!
)
!
* * ! 7 !
*
!
*
H
H
H
)
*
A
0
0
!
, !
)
* 12 H 1
! H , !
, , !
)
H
* A
1
*
, , !
,
- % , * (
)
€
1
* H
)
, ? ,
H F
)
D
H
E
! ,
*
<
1 ,
%*
* / * *
& * * H 1 )
<
H ) & , * H
H 2
1 & ) A
H , ) A
)
* H
1 !
H ! * / ,
! *
* * %*
* *
' H ' * H
1
1 , )
C !
*
, H
*
+
* !
/
!
HI %
*
1 )
!
1
1
*
1
) )
, #
* 12
* H * 12 , ,
HI 1
, , ,
;
, * ,
,
1
!
% **
,
, ,
1
c
)
)
-
)
%
:
A
'
'
,D
1
E
*
!
,
1
*
* *
!
12
<* , * 12
7 6 *
H
H ! *
$ / ! H ,
! , ) )
)
*
C
+
- ,
, H ! , & , !
! ,
1
* 12 %
, H
,
1
%
, )
+
G
1 H
! *
! *
* C
*
! < H * 1
* A
!
* *
power8.C
€
,
* /
!
, , ?1
)
! 6 )*
- 7 B
, , ,
! !
*
1
)
,
& !
)
1
* ) &
H ,
* /
1
int
H
HI * (
)
*
H
* /
,
* /
!
*
)
1
, , power8.C
+
1
* * ,
)
*
H
, +
A , H 1 1
* 1 ,
,
- H
A ! 1 1 b
b
H * 1
)
?
/
,
! H
b = b * b
A
,
,
b * b
,
$
)
!
H
a
7
;
,
HI
*
,
**
!
)
()
* / - )
!
)
)
!
)
)
*
H
,
,
a * a
H b
* 1
b * b
A
$ C
()
IH !
int
b
1
!
, "
H 1 !
)
/ < * C
H
! 1 * A
a
a * a
#
, * /
H !
*
* ,
b * b
,
!
)
, * /
H - ' 1
' A
* G 1 ! * / a
power8.C
!
b
H *
* /
@
*
,
* /
a * a
#
a * a
2.1.11 Expressions
2.1.12 Lvalues and rvalues
b
b
std::cout << a
0
2.1.13 Operators
*
€$
5
69
5
7
6
8
4
5
4
7
79
std::cerr
std::cout
!
HI
A
!
, * *
, !
*
-
,
!
.
+
, !
) * !
H
& *
*
, !
H
+
)
*
std::cin >> x >> y
1
* )
* ,
,
* !
! IH
*
1
!
**
H *
!
-
A
! & +
1
* H
* !
*
! -
H
A H
F
)
?
+ H
,
1
A *
* & 1
H
'
'
,
1
!
, +
* *
? A
* 1
* A
&
cin
!
int
H
!
power8.C
!
,
!
*
Output Operator.
, .
)
*
, ,
- ? <<
%
,
!
, !
*
>>
*
H !
!
* 1
H +
!
)
*
!
* power8.C
-
!
!
*
! * * H H A *
,
% ! H H *
1
f(x, y)
B
+ A
* *
! ,
.
* ! , *
*
A
H ,
*(a,a)
std::cin
)
-
, ! 1 3 !
* !
*
: ! H 1
&
! A
*
&
! !
* !
=
&
' ' std::cin >> a
b
F / 1 * 6 H !
1
H 7
+
*
*
H
H
H ? * 0 H
* =
:=
)
"
!
H
H
b = b * b
b
*
,
H H .
*
H
=
+
,
* /
*
+
&
1 ! &
* H
,
!
& *
& +
, , *
#
)
,
!
!
H A
1
!
+
a * a
=
$
power8.C
C
! *
H #
D
E
! , !
( !
*
, )
)
+
power8.C
,
1
Multiplication operator.
Assignment operator.
Input Operator.
a
=
Operator specifics.
. !
. H ! * ) *
F H
,
! *
!
*
,
!
H
!
+
,
* * G !
* ! *
, * H 1 F ,
IH H
!
*
C
*
!
!
,
%
*
* ! !
* 1 ,
*
, A
!
, * H +
!
* / * ! )
* / ! * *
) , * /
H * , !
-
*
) , ! , !
.
* ,
1
C - -
* (
*
)
! ! * *
/ H * ! 7 ! , * * * / * & ! H , C
* / ) ! ( * !
) * *
G € * ,
)
) , *
/
*
, *
* / H
,
1 !
* & 6
?
IH
*
, !
&
)
>>
int
a
std::cout << a
<<
€
5
69
5
7
6
8
4
5
4
7
79
power8.C
0
0
return 0;
,
B
(
*
!
!
,
,
!
,
1
H !
H 7
7
7
H
*
* %
!
* B *
* *
1 ! * ! +
'
' ! * 1
1
A
H
1
:
* A
!
B 1
* H
*
1
(
*
H * A * !
,
1
!
, , ,
1
H )
% ) * 6
H , 1 !
7
std::cout
+
*
* H
, *
* B
* 1
,
*
*
, H
A
,
*
, power8.C
std::cin
*
* .
* & , %
* 1
* ; /
* * H !
1 , )
A B
!
) )
H
* *
*
, !
! ) ?
&
,
- * ;
A ''
# 1 H
* !
, H * , 1 , )
A
std::cerr
* * , * %
1 * ,
! ) ! IH /
*
, 1 ,
HI - +
*
* * /
, ! *
* H
!
!
, + , * / 1 * !
Declaration statement.
Return statement.
0
return
!
2.1.14 Statements
, H ! , )
7 < !
1
* /
H '
'
*
* H )
) a + (b + c)
/
*
7
- ! )
! , ) !
)
**
* ! * H !
! )
1 A ! ? 6
7 ! *
& - ;
* 1 ! 1
* 1
6 !
std::cout
1
* 1
1
*
A
!
%
<<
*
\n
1
* /
-
(std::cout << a) << "^8 = "
-
%
)
* H
D E ! (
%
)* std::cout << a
!6
1
?
?
((( std :: cout < < a ) < < " ^8 = " ) < < b * b ) < < " .\ n"
!
*
!
% !
, ) * B
* * /
+
!
* *
*
B H
*
H ,
1
>>
, * 6 * HI 7 H &
,
+
! *
* **
A * !
! *
!
* IH *
** %
1
*
* A ,
!
!
+
C
1 H
H A 1
A
A
!
,
!
*
1 * *
A !
(a + b) + c
<<
a+b+c
H )
*
F
A
*
+
)
%
* - )
+
! ,
IH )
1
H A
!
, C !
!
H 1 !
*
-
H ! Expression statement.
b = b * b;
std :: cout < < a < < " ^8 = " < < b * b < < " .\ n"
int a;
int b = a * a;
<<
main
5
69
5
7
6
8
4
5
4
7
79
A
6
*
power8_exact.C
158
Compute a ^8 for a =? 15
15^8 = 2562890625.
A
&
H , !
integer.h
< 7
1
+
2 / +
0
!
"
!
<
{{
"
0
0 /
2 !
!
2
0
$ " " 0
/
9
{
{|
,
1
,
, )
// Program : power8_exact.C
// Raise a number to the eighth power ,
// using integers of arbitrary size
# include < iostream >
# include " integer .h"
int main ()
{
// input
std :: cout < < " Compute a ^8 for a =? ";
ifm :: integer a;
std :: cin > > a;
} { | } { 3 2
$
% $ ! ! 0 !!
" ! $
$ !
"
,
1
*
**
&
!
*
HI *!
, 7
* 1 , H
!
A
!
' <'
int
ifm::integer
* /
,
*
* 1 1 ! 1
!
& ) - H
* ! , *
$ 6 * ? !
*
1
A !
H B
-
?
!
*
H 1
1
158
* 6
, 1 H *
*
*
7
*
, !
<
Program 3:
!
,
*
!
2.1.15 The first program revisited
*
!
!
!
, IH
IH $
2
"
A→B
;
Compute a ^8 for a =? 15
15^8 = -1732076671.
!
!
B
H
+
!
C * A * )
*
1
&
!
F ) A
power8.C
!
<
Figure 2:
!
* *
)*
H
'
'
*
/
!
,
,
* 12
1
* H
+!
* ,1
H * !
, ) * 1
*
1 1
, H
) F
€
- & *
* * * ,
) * H *
'
'
int
ifm::integer
// computation
ifm :: integer b = a * a ; // b = a ^2
b = b * b;
// b = a ^4
// output b * b , i .e . , a ^8
std :: cout < < a < < " ^8 = " < < b * b < < " .\ n";
return 0;
}
"
5
69
5
7
6
8
4
5
4
7
79
std
cin
1
7 6
!
!
1
!
C X
*
!
2
+
<
" 0
!
"
*
H
+
+
* 1
! ,
H ) &
!
A
A
std::
power8.C
*
,
!
!
1
, ! H * * , 2 A A
6
6
) )*
* * !
7 , 1
H A
1 HI A
! !
7
;
!
* 6
A * 1
* 7
!
H *
A
)
*
!
return 0;
*
B * *
* 1
, *
*
!
1
%
!
*
- & 1 )*
!
*
,
!
*
main
+
* Program 4:
std
H
!
B
using namespace
*
using
* , *
%
)
-
* 1 6 !
! * *
7 6 ! * 7 !
*
Using directives.
using
!
1
*
*
!
The main function.
_
H
!
A
* * 2
1
7 *
A
*
1 , ! c = a + /* don ’t subtract ! */ b;
*
*/
*
!
*
H A
!
,
/*
-
# include < iostream >
using namespace std ;
{| } { | } { {
*/
? !
'
'
*
*
D
? HI
Program : power8 . C
Raise a number to the power 8.
0
{{
H
,
*
A
!
*
H HI
1 *
*
1 * <
!
!
, *
* A !
*
,
! !
* *
1 H *
/*
+
* *
1
( Commenting.
&
)
,
! * 6 C
,
* !
7 &
H 1 1
A ,
1
*
A
*
*
F
2.1.16 Details
6
! *
H * D *
*
& B E "
!
- &
E
int
+
#
&
*/
power8.C
1
H ,
!
* , *
1 1
*
!
D & H E 1 ! 2 ifm::integer
int
& ' '
H
- 1 ! *
* &
, ,
IH ! ! 1
!
1
/*
H
*
Identifiers starting with an underscore.
* Define variables where needed!
-
C *
A
!
,
1
)
*
&
1
*/
1
1
*
*
/*
B
!
)
H , 1
, !
*
Compute a ^8 for a =? 1234567
1234567^8 = 5396563761318393964062660689603780554533710504641.
main
ifm::integer
// Program : power8 .C
// Raise a number to the eighth power .
int main ()
{
// input
cout < < " Compute x ^8 for x =? ";
int a;
cin > > a;
// computation
int b = a * a ; // b = a ^2
b = b * b;
// b = a ^4
// output b * b , i .e . , a ^8
cout < < a < < " ^8 = " < < b * b < < " .\ n ";
return 0;
}
X::
cout
5
69
5
7
6
8
7
4
5
4
79
%
? !
int
"
6 3" !
7 #
6 "3 7 #
a, b, c
6 "
3 76 " 3" 7 1
=
a
!
=<
power20.C
" H
1
# include < iostream > int main []{ int a ; int b; int c; std :: cin > > a ;
cin > > b;c = a * b ; std :: cout < < c *c; return 0;}
6 $
! # 0 #
/ !
$ 2 $
4 #
6 /
3 5
7
1
/ using
4
"3 7 1 !
#
/
!
/ $ ! $ 2 /
5
6 3 7
0
1
,
!
*
1
,
(
- -
(
$0 -
#(
-
"(
(-
1
!
4
# (-
(-
"( (
- -
(-
(-
#
=
=<
"
0 #
4
3 6
€7
$0 , * , 1 H !
A
! !
* 1
< 1 ** H , )
1 * *
H
(
- -
(
H
*
H
-
!
! * ! C
*
! ) ! H H
*
1
!
#
- #
0 "
( - # $
! 2 (
=<
=
/
!
6
3 7 (
8
int
1=a
(a=b)*(b=5)
#
0" €
7
0
-
!
!
1
A
,
A
b
x_i
T#
!
multhree.C
abc
2
*
7
17
a=(b=5)
(1
a20
!
*
+
* 7
H
* *
< 6
C * & H * ,
* A int
_tmp
!
"
- ) * A
C ! !
1 *
,
-
a
1*(2*3)
(a=5)*(b=7)
* ,
D
**
* A E
1 * * !
A
A ! H !
* !
, 1 % 1
D *
E HI
identifier
A99_
Exercise 8
!
Exercise 6
Note:
4
1
!
H %
Exercise 5
2
" 1
* )
H ! 1
H
* *
H * H H * H !
'
H '
B * /
1
*
Exercise 3
Exercise 7
4
" !
"
! 2 " !
H
* +
power8.C
!
7
* !
<
1
*
, , IH * * H ! * H !
1
+
!
H !
* 1
Exercise 2
power8.C
Exercise 4
B
&
!
2.1.17 Goals
H
&
1
B
)
*
€7
std
"
, *
*
C
*
, *
*
C
*
7
1
1
* , *
) * B * !
'
'
6
, 1
7
3
6
using
2 $
0 2 $
"
3
€7
6
std::
Exercise 1
B
)
, )
, ,
B
3 7
6
cout
7
!
*
3 7
&
6
power8.C
std
,
, 7
3
6
Operational.
"3 7
6
Dispositional.
cout
! /
!
* H )
* ,
H , ?
IH
&
IH
1
cout
cout
!
&
,
1
*
F
)
H 0
H 0
1
A
A
* * G ,
2.1.18 Exercises
4x__
x12b
(a=1)
(a*3)=(b+5)
$
5
69
5
7
6
8
4
5
4
7
79
.
3
(
0
!
"
" #
0
!
2 #
$ u
./ power8_exact < power8 . dat
n
power8.dat
power8_slow.C
time ./ power8_fast < power8 . dat
`(n ) < λ(n ) + ν(n ) − 1
$
!
t
1
/
!
! !
2
!
t
0
2
i
/
#
- 0
!
2
a
i, 1
n
an
n
#
#
#
.
#
!
, * 1
# 0
0" 0
3 #
! !
: 0
3
! 0 1 $
! "
a0
2
!
! (
!
# :
* *
6
7
$
0
4
$
$ " / ! #
!
$
5
69
5
7
8
4
5
4
7
6
€
79
'
F
) &
)
)
*
-
17
#
0
# ! #
-
(
# !
/
#
!
!
$
4 "
$ "
) &
! 7
* *
*
) 1
* !
, )
, 0
* H *
) )
*
!
)
IH
1
!
*
`(n)
1 ! 0 #
" #
2 ! $ ! !
a
2
!
# 2
0 7
*
`(n)
`(n)
!
#
! 0
a t = an
power8.C
" !
#
$
7
an
t
$0
" n
a
2
ν(20) = 2
2
"
%
1 1
*
B
1
H 1
*
n
n
λ(20) = 4
!
`(n)
t
λ(n)
#
(
#
/
2
2
+! $ ! #
)
# λ(n)
!
`(n)
!
0
!
2
)
2 2 )
2 !
# !
$ ! .
0
2 % " #
) !
" $ " !
0 0
#
" t
#
! !
$ (
/
" $ ! !
! #
# 0 2
.
! "
#
" ! 0 "
# 1 " 0
#
" /
1
j, k < i
10100
!
!
$
1234567
" $
0
power8.dat
0 !
"
$ #
!
int
Exercise 10
!
0
" 0
0
0
$ 0 ! 0 1 " ( " $
$
0 !
$ "
0 0
$ 1
10, 000
5
4 #
0 2 - ! $ " "
!
0 0 0
!
10, 000
-
$(
! # 0
! " $
$0
2
power8_fast.C
ifm::integer
power8_slow.C
7
3
0 $! " - $ " $
0
(
#
0 power8_exact.C
2 !
2
2
! " /
!
$ #
ifm::integer
"
!
power8_fast.C
0
! - " 0
. $ " !"
( 3 $ " !
#
! "
#
" # / 1 power8.C
" Exercise 9
3 $
"
!
2.1.19 Challenges
power8_fast.C
power8_slow.C
`(n)
ai = a j * a k
a8
a1 = a 0 * a 0
a2 = a 1 * a 1
a3 = a 2 * a 2
1
λ(n) + ν(n) − 1,
ν(n)
5
20
* , ,
*
*
:
* , *
)
* , * * H *
H
,
! !
, )
, +
) H ,
* , 1
* * !
*
1 * , 1
6
* ( € H
€ 1
7 *
)
1
!
1
0
9
, @
!
-
!
" 0
& * #
1 !
- , *
! * !
C
*
!
* !
1 H
0
!
*
C
1
#
0
H
H
!
* ,
? 1
# !
H
$ )
*
)
H
H
!
H
!
-
,
1
!
) ( &
+ +, -
'
'
"
3
:
!
celsius
* , H
H
2
* &
H int
* !
* H * , A
C ! " C 2
int
*
,
, % * 1 H
+
' , ' ?
9 * celsius / 5 + 32
, * ! 1
* ) %
IH
' ' 0 !
)
* , 1
' ' * (
*
- * €
A , * IH )
*
,
$
.
$
!
!
$ " 9 5 32
$
+
Arithmetic Evaluation Rule 1:
, - 2
#
/
!
$ ! $ !
2 0 0 32
H
!
−9
$
)
;
+
A
!
9 5
/
)
'
'
Literals of type int.
int
#
%
3 + (4 5)
(3 + 4) + 5
!
celsius
// Computation and output
std :: cout < < celsius < < " degrees Celsius are "
< < 9 * celsius / 5 + 32 < < " degrees Fahrenheit .\ n";
return 0;
-
int main ()
{
// Input
std :: cout < < " Temperature in degrees Celsius =? ";
int celsius ;
std :: cin > > celsius ;
}
# include < iostream >
, * IH
, * , IH ) * * ,
, " *
* *
? // Program : fahrenheit.C
// Convert temperatures from Celsius to Fahrenheit.
15 degrees Celsius are 59 degrees Fahrenheit.
HI
+ 32.
2 <
#
5
!
"
!
Program 5:
F
HI
#
F
*
,
&
1
!
9
#
=
unsigned int
+!
} { | } { int
!
" 0 2 3 9 "
*
-
)
! * ) = !
!
*
,
)*
2.2 Integers
9 * celsius / 5 + 32
-9
9
2.2.1 Associativity and precedence of operators
3+4+5
3+4 5
3 + (4 + 5)
+
*
-
/
5
69
5
7
6
8
4
9
7
8
8
67
*
(9 * (celsius + 5)) * 32
(1, 2, 5, 3, 6, 4, 7)
9
H
"
!
2 #
#
1 H )
& H
1 # *
* , C
- *
! H
) * ! H
1
}
1 * !
? H
* +
9 * celsius
/
2
9 * celsius / 5 + 32
((9 * celsius) / 5) + 32
!
/
*
((9 * celsius ) / 5) + 32
celsius
! ? , ! 1 * )
1 !
A
&
* !
) * H
*
H , A
-
*
,
H * H *
* 1 '
'
1 * , 1 A
F
)
-5
C
*
*
C
*
!
H
!
*
#
&
1
!
!
H
*
)
9
-
F
(( − 3) − 4),
* Figure 3:
(
*
/
, 1
* HI
1
A
A 6
1
) *
H 7
1 6 1
1
# *
1 7 ) 9 * celsius / 5
,
(-3) - 4
* )
1 ) * ( *
! ,
C * * F /
C
1
* ? 1 ) 1
, **
, !
0
-3 - 4
, A
* H
!
, ,
1
H
! , , )
, , ; A #
F )
H
H
* 1 *
H *
H
< A
1 , ! H
& 6 *
1
6 *
1 *
7 ! )
; H
)
) H 7
* +
* * -
H
,
C
*
!
" $ 1
*
*
!
!
-
A
1
-
*
*
*
?
, * *
*
-
;
!
+
? )
* !
*
, ; H * ) ) H
1 ! ? 1
, !
- *
+
, F *
H !
) )
*
! * ) 1
, !
!
/
1
!
?1
)
*
' IH
' ! # " ? 1
)
9 * celsius / 5
?
H
9 * (celsius + 5) * 32
,
, H
'
3 - 4
!
*
)
−7
*
1
!
,
!
* ) )
-3 - 4
* -
;
6
)
*
7
Arithmetic Evaluation Rule 3:
,
Identifying the operators in an expression.
* H
!
, 1
*
(9 * celsius) / 5
*
)
)
H #
Arithmetic Evaluation Rule 2:
H
+
*
* C D
? ,
E
)
<
*
)
1
!
32
C
C *
* *
*
!
*
* , ,
, **
C +
F * % H !
)
H
HI
HI
*
*
*
, '
'
-
+
-(3 - 4)
1
(9 * celsius / 5) + 32
9 * celsius / 5
2.2.2 Expression trees
5
9 * celsius / 5 + 32
32
9 * celsius
(9 * celsius) / 5
((9 * celsius) / 5) + 32
2.2.3 Evaluating expressions
celsius
"
5
69
5
7
6
8
4
9
7
8
8
67
1
)
The division operator.
9 * celsius / 5 + 32
9 / 5 * celsius + 32
* +
?)
*
*
-
!
2
!
5
int
!
/
* ? ?
* *
? *
? * €
€
€
€
€
€
!
€
!
€
!
€
!
€
"
"
"
"
! €
!
!
Prec.
2
, 6
7
Arity
. $
!
#
, 1
* /
*
H
*
Operator
++
-++
-+
*
/
%
+
=
*=
/=
%=
+=
-=
1 C
Description
" / ! !
!
59
2.2.4 Arithmetic operators on the type int
(
H #
59
* , 32
"
!
$ !
/
!
2 2
!
(
1 !
1
2 - 5 !
" 1 ! / !
(
!
1 # int
Table 1:
+
!
, <
H )
*
€
'
€
'
€
C
1 *
) 7 6
+
*
,
1
?1
)
,
1 ,
H 1
)
!
A 1 ,
H
15
HI
* /
'
27 + 32
€
27
−→4
'
−→6
5
135 / 5
celsius
,
−→3
'
9 * 15
*
135
*
C
-
−→5
!
,
+
C * C
IH * ! *
)
B
H H 1
, )
,
* !
&
1 C
, '
*
' -
* /
* * ,
,
H
1
1 ?
−→7 59
(1, 2, 3, 4, 5, 6, 7)
) *
" €(
6
,
C
*
1
*
9
*
*
, 1 *
* , 1
; 7
* * %
* C *
H * )
! *
) , 1
H * *
&
−→
2
*
!
−→1
H
+
fahrenheit.C
*
-
1 - 1 1 * H !
1
H *
6
HI * , +
* ,1
" *
* , !
- 7
* 1
,
9 * celsius / 5 + 32
1
*
* %*
*
?@ !
0 ) *
* ; ! *
, !
C * H
H
celsius
Assoc.
5
69
5
7
6
8
7
4
9
8
8
67
++a
=
C
G
, € IH
*
, 7
# !
/
!
* -
* 1
!
, *
1
1
-
*
1 )
- H /
!
* 12 !
/
!
1 / * ! 6
*
H F *
, 7
A ,
/ !
!
?
*
-
/
*
* * !
* A
* , * , *
* /
!
?
,
6
*
*
7
!
!
/
!
-
!
/
,
? !
) /
< !
-
, ! *
H !
) *
/ ! IH
D
!
*
, ! E
HI
! < ,
!
)
!
* /
,
!
*
)
/
!
*
<
)
;
7
4
9
, ! 5
69
5
7
6
8
$
8
8
67
*
, H !
!
,
!
*
-
++
- H * / /
7 !
! ? , * ! * 1
! / , ! 1 )
/ / ?
! ! *
?
/
1
*
!
, !
! * *
!
, /
6 %
A * 1
, !
- H
H
* 1
H ,
H
*
, ,
H
++
1
++
* *
* A
*
H / * 1 !
*
?
? *
)
&
!
/ ?
-
,
!
++
*
!
,
1
H )
!
)
H
,
1
!
-
-
/
a
a = a + 1
*
0
1
*
1
6
* (
(a / b) * b + a % b
1
b
a % b
++
1
@
/
++
!
!
*
*
a
Increment and decrement operators.
H
(a / b ) * b + a % b
, +
H 1 * celsius + 32
1 * 15 + 32
15 + 32
47
, , * /
0 H !
+ / ! / ! )
H ! ) H 1 / / *
*
H ! * , C , A / / * ! 1
* H * H
/
! <
!
- /
* H !
?
?
*
, ?*
H
F
H ?
*
1 " , -
H C 1
! * , 1 , int
, !
b
* * - *
' '
* %!
H 0
,
1
1 ,
* H
b
,
, 1
59
'
) < '
!
)
HI
?
15 degrees Celsius are 47 degrees Fahrenheit.
Unary additive operators.
* '
'
*
C !
*
*
A
&
6 )
int
, <
b
9 / 5
1
E
b
b
A
a
*
1
%
#
9 / 5 * celsius + 32 −→
−→
−→
−→
D
+! - 1 ,
* ,
* 6 ? 7 , , ,
* IH
, *
6 0
! 7 *
+ ! ? 6 * , , ,
7
* , ! b)b + a
$
1 , ! ,
*
H
!
&
H +! , , ! , , The modulus operator.
a % b
!
a
F *
H 7
* ,
!
,
,
C
*
!
, *
#
a = (a
a / b
a
/
fahrenheit.C
15
+
+
--
--
--
1
--
++
--
--
--
++
# include < iostream >
int main () {
int a = 7;
std :: cout < < ++ a < < "\n " ; // outputs 8
std :: cout < < a ++ < < "\n " ; // outputs 8
std :: cout < < a
< < "\n " ; // outputs 9
return 0;
}
+ 1
1
0
= {0, 1, 2, . . .}
#
, , , IH
?
1
, !
, A
) )
*
) ? ,
G
,
% !
, 1
*
H
2147483647 = 231 −1
32
std::numeric_limits<int>::min()
% *
*
H 1
1!
+
, H !
* (
H
!
1
,
A
1
H
?€
limits.C
, 1
'' * ,
*
, H ! *
H
H *
-
*
2
<
"
!
Program 6:
*
* !
+
+ )
* * A )* A 1 * , * 6
- * )*
H IH 1
,
1
, ,
1
! *
7
int
int
, ,
)
H
+
*
* * *
H
1 ,
!
)
3
D
? *
1
*
E
* 1
*
}
, ! / , % ?
*
? 1 *
+
* H H ? * 1
*
1 , D * !
1
E *
1
{ | ,
H
1
*
int main ()
{
std :: cout < <
<<
<<
<<
return 0;
}
unsigned int
2147483648
,
2
H
H * *
*
, , 1 !
, 1
,
int
int
! 2b
int
int
b
* (
1 ?
, ,
G
, 1 ,
1
A
* *
* * * * IH
1
2.2.5 Value range
1
%
* 12
!
1
* %=
+= 1
H
1
-
D *
? * E *
*
* ?
* i
a=(b*c-d)
H
1
* H &
, ? * *
- A A 1 ?
*
? ! *
* ? *
* '' 1 , ! +
%
H *
++i
!
;
!
i
,< H
H
-
a=b*c-d
C
++
limits
- &
* * , ,
" *
H
+
b
€
- *
* /
*&
1 ! , *
H 1
H / !
! H 6 , *
7
- , ) ! !
) / !
/
+= -= *= /=
*
,
!
)
@
, * /
C
{ )*
H
, *
, 1
* * H * /
! 1
,
?
* !
?
-
, 1
i++;
,
H H
/
!
)
*
A
- )
! /
!
) )
6
? , / *
! 7
<
-
C * 12
, ) !
/ / ! ! 1
)
/
!
/ C )
! , )
/ ! /
! % * 1 ! H
/
!
=
++i;
@
*
+ 1, . . . , −1, 0, 1, . . . , 2b−1 − 1}
* H
, H
, * H * H *
, 1
A
%
%=
* -= *= /=
=
#
Assignment operators.
b−1
, −2
int
, %
1 ( * 1
! /
& !
) ,
1 1
, ( * ! 1 *
H H 1 * /
12 * * * , ! 1 1
,
!
* !
*
& IH
H ) &
!
+=
1
1 b−1
{−2
!
+=
2b
Prefer pre-increment over post-increment.
// Program : limits .C
// Output the smallest and the largest value of type int .
# include < iostream >
# include < limits >
" Minimum int value is "
std :: numeric_limits < int >:: min () < < " .\ n"
" Maximum int value is "
std :: numeric_limits < int >:: max () < < " .\ n";
Minimum int value is -2147483648.
Maximum int value is 2147483647.
/
%
2147483647+1
int
2.2.6 The type unsigned int
int
€
5
69
5
7
6
8
4
9
7
8
8
67
-
,
? € 1 H !
* ! H
* 1
, H , , !
!
! &
;
**
!
'
'
a
3
b
4294967295
int
int
Conversion binary → decimal.
k
X
i=0
bi 2 i = b 0 + 2
k−1
X
i=0
+
bi+1 2i = . . . = b0 + 2(b1 + 2(b2 + 2(
H
1
1
=:n
{z
1
) *
C
|
F
*
i=0
∞
X
b k . . . b0
+ 2bk ) . . . ))
H H
bi+1 2i+1 = b0 + 2
!
n
-
bi
,
H
! ) *
1
!
13 = 1 20 + 0 21 + 1 22 + 1 23 .
C F ) H *
*
!
H
!
&
{0, 1}
i=0
∞
X
bi 2 i = b 0 +
,
*
i=1
∞
X
* - *
* & * *
1
! 1 ) !
, ! * i=0
!
*
bi 2 i = b 0 +
H
A
! !
H
!
1
1
- $ / ! ,
! 1
*
n
* A
H ,
1
*
- A
, ! H
* 1
+
1 A
b
1
*
,
?
%
H
,
,
*
A
1
int
13
* ,
, ! 1
, 1 1
!
1
unsigned int
*
Conversion decimal → binary.
∞
X
b−1
*
bi
b0
n
bi i 1
n = (n − b0 )/2
n = 14
b0 = 14
2 = 0
n = (14 − 0)/2 = 7
n=7
b1 = 7
2=1
n = (7 − 1)/2 = 3
n=3
2=1
n = (3 − 1)/2 = 1
n = b3 = 1
14 b3 b2 b1 b0 = 1110
i=0
∞
X
*
n
n =
,...,2
C
!
−2
H
-
2
-
n=
b2 = 3
1
2.2.7 Mixed expressions and conversions
1
,
H
)
F
unsigned int
int
b
b−1
int a = 3 u ;
int b = 4294967295 u;
unsigned int
n
H
0
!
1
+
H
H *
*
! **
*
!
limits.C
unsigned int
17+17 u −→ 17 u +17 u −→ 34 u
int
, +
F
unsigned int
F * ) C ! unsigned int
1
int
1
*
Minimum value of an unsigned int object is 0.
Maximum value of an unsigned int object is 4294967295.
H
int
unsigned int
? 1
* - ! "
,
int
,
,
, ?1
)
* ) !
1
!
17+17u
int
1
unsigned int
H
) 0U
* 2
'
! H )
/ / *
' !
;
2b
1
)
17+17u
6 1
! 7 *
!
H
*
6 * IH
! 2
1 7 < unsigned int
127u
,
!
2b
? !
& <
2
* &
!
'
'
H
1
*
,
U
, ?
, , , * ,
D*
,
E
, ,
!
!
6
? B
u
* 7
{0, 1, . . . , 2 − 1}
unsigned int
*
*
,
* *
*
1
+
b
1
H
?
HI
*
*
*
, , 3
, ! b
H
&
!
)
* unsigned int
2.2.8 Binary representation
− 1,
0, . . . , 2b − 1.
bi 2 i ,
1101
bi+1 2i
}
5
69
5
7
6
8
4
9
7
8
8
67
b
1
b=3
{2b−1 , . . . , 2b − 1}.
, #
*
- 1 % 6 7 *
,
/
!
1 H
" $ H !
!
HI * * ! * *
HI H int
n
int
n+n
-
HI
1
,
* !
,
* 1
1
!
H
- 1 ? int
n
n
int
2b
HI
A
, *
n + 2b
n+n
*
n
+
A *
!
- ,
G
A
H 1
* , 1 H
1 1 !
* ! ,
1! * ! 1 ,
H
<
H
* , G 1 ! 6 *
7 *
F )
H
−1
'
−2
!
&
, !
, IH 1
*
, 1
;
, A
*
, H *
+
{0, . . . , 2b−1 − 1}.
n
0
int
!
+
H
! 0
*
-
, 1 1 H 7
0 A 1
&
6
{0, . . . , 2b − 1}
-
*
(
*
,
unsigned int
n
−4
−3
−2
−1
0
1
2
3
1
!
n
' ! A *
*
1
*
* ,
!
, * A
*
1 *
H H 1
*
,
*
n
1
* * !
! ! ) *
(((b4 2 + b3 ) 2 + b2 ) 2 + b1 ) 2 + b0 = (((1 2 + 0) 2 + 1) 2 + 0) 2 + 0 = 20.
-
b
000
001
010
011
100
101
110
111
1
2b unsigned int
b=3
* , !
*
6
,
1
* 1
*
, n + 2b
H
int
b
,
1 A !
H !
1 ! ? 1
A !
IH * * * )
* % *
*
1 ,
1
'
F
'
, H
IH H b
n
0
1
2
3
4
5
6
7
n
!
Representing int values.
!
1 +
! !
, *%
?
!
, H ,
1 ! 1
> ! b
H
n
-
+! , , ?
H 1
1 !
n
,
7
?
1
IH
F
Representing unsigned int values.
1
1
F
H )
* H
*
,
* ? b4 b3 b2 b1 b0 = 10100
100
101
110
111
000
001
010
011
b
n
b=3
110
111
1101
101
−3
n
n
n
b
unsigned int
2b
unsigned int
n + 2b
unsigned int
2.2.9 Integral types
unsigned int
"
5
69
5
7
6
8
4
9
7
8
8
67
unsigned int
unsigned int
-a
int
4
4
4
a
'
'
H A
3
3
*
1
*
, *
*
)
4
1
16
r
%
3
2
13
l
,
1
!
!
2b
2
(
,
4
H 2
x3
x3 )
x3
2
2
2
2
x3
2b − a
2b
4
7
;
*
)
*
2
x2
x2
*
,
*
1
-
2
13
l
2
13
r
!
(
*
H
x2
( x2
( x2
,
*
,
1
x2
C ? * * ! ! !
2
4
r
, F
H
)
1
1
3
3
!
1
-
x2
1
x3 )
*
**
+
)
)
?
H
1
1
H
H
F
2
2
)
( x1
( x1
2
x1
*
1
2
!
)
*
( x1
* 1
,
x1
)
(( x2
,
x2
, *
*
*
;
*
A
, !
)
6
, 1
C
*
!
H
!
!
1
C
*
!
F
)
!
H / 8
1
* Unsigned arithmetic.
1
!
,
!
*
1
(( x1
*
*
( x1
x4
* *
, 3
)
*
!
! !
! -
,
)
*
F
)
1
H 1
C
, *
0x1F
-
HI 31 = 1 161 + 15 160
* !
*
C
9
H
)
- 1 C
*
! H
A
* H *
7 *
6
* 1
, 1
6 !
7 H F
* H
, 1 ! ) )
* !
1 * * ! A
* ,
* 1
* , ? 1
!
1
! H *
, *
& H
* !
+
0
?
,
H
i
i
i
x4
x3
* *
!
*
9 = 1 8 1 + 1 80
*
)
* !
! * IH
,
? G
1
4
2
,
* IH
* ,
* ,
, -
! * 6
) ,
, ?
! 1
,
7
3
3
x2
3
-
' * '
*
A H
, !
? 1
F
- IH
i
,
!
* , * * * * !
, 1
C , ? &
+
i
4
1
16
r
i
3
2
13
l
!
* * )
0
0
x3
2
13
l
*
0x
)
2
D
1
1
E
1
D
E
*
, *
*
x2
H & ! 1
< !
! *
! * , H H !
" 2
4
r
, 1 ) ! !
0 * * *
,
+
1
1
,
0x
Logically parenthesizing a general expression.
int
7
)
3
H
)
H !
1
C
*
+
x1
0
H
* *
! 1 )
HI !
*
& *
!
$ !
H +
!
0
, !
1 *
) ! !
* 0 ,
* * A
&
H
*
!
* %*
/
*
! * * *
Literals.
H
1
!
)
H
** H *
2 * C 1
1 *
!
!
F H !
! * C 1
*
6 ! *
7 011
2.2.10 Details
x4
x4 )
x4 )
x4 )
x4 ))).
2
13
l
x3
x3 )
x3 )
x3 )),
unsigned int
b
unsigned int
5
69
5
7
6
8
7
4
9
8
8
67
i
+!
int
11
i
12
++ i + i
i
6
1
! !
*
C
, * A
!
+ ! H
? *
! !
, *
H ! 1 ,
* ! 1
*
1 1 ! 1
!
+
;
6 * * , *
* !
H 7
* 1 1
*
&
+
!
H * * 1
,
( 1
H ,
!
)
*
&
H B
!
H *
1
*
A
€
%
,
B
,
!
B
!
!
,
H
H
!
, *
! H ? 1 , ) * *
*
! F
! * A
, !
H
,
, !
* / * /
*
? <
& *
*
1
!
H ,
* <* /
! * !
, 1 , H * ;
)
i
,
, ;
!
! ! H i = ++i + 1
?
(
H *
)
*
H
)
-
*
&
)
*
!
*
A
)
,
, ,
1
! H 1
F ) !
H C
*
! !
A
IH
HI !
A
* *
*
, *
)
*
6
*
, 1
HI
1
!
*
, 7
-
!
!
!
* /
5
69
5
7
6
8
7
4
9
*
"
$
8
8
67
!
1
!
*
&
1 ,
, 1
*
* *
*
, ,
!
)
'
;
*
*
*
,
!
A
' , H
,
A
HI
*
, )
++i + i
* H
i
++i
i
C
'
*
# 1
*
A
" i
)
'
H
&
* !
1
* / ! , ,
, ?
!
-
,
,
!
7
*
1
, 1
*
+
1
1
)
*
!
? )
*
C
H
1
+
* & IH ' '
* / !
! !
, * A , * ,
*
i = ++i + 1
H
*
6
17
!
*
i
i
*
+
*
H *
-a
C
long int
H
H
, 1
&
C
*
*
'
'
- -
H
! !
H ! , * / 1 ) * / )
H
,
* * / ) * * /
A -
int
unsigned int
)
?
unsigned long int
*
" --(-a)
a+(+b)
?
&
---a
*
!!
-
5
,
signed char short int int
1
---a
,
!
, <
!
?
*
(a++)+b
(a++)+b
- H
H ,
* *
C
1 !
* IH A
* ! )
* !
A
IH
, ) 2 - 1
H 1
0
1 H A
- !
? ! H ) !
) H H *
1
* H
a+++b
a++b
*
'
'
a+++b ---a
a+(++b)
a+(+(+b)
-(--a) --(-a)
-(-(-a)
Order of effects and sequence points
-
, *
!
!
/
, H
1
)
1
+
Other integral types.
* &
-
* ! H
1
H
1
-
, ?
--(-a)
+
-
- * C 1
* !
! 1
-
*
*
int
,
- * &
*
* * ! H 6 * (
* ,
, ,
? !
G * ?
*
int
unsigned char unsigned short int
7
!
'
'
++
,
1
* * !
1 short int
? !
H C H
' ' ' '
! * H
!
* A +
*
*
1
?
unsigned int
- * , 1 ! *
*
)
6
* /
(
*
!
(*
, €
1
7
+
,
!
?1
)
!
, )
+
)
-
C
H ,
* /
,
)
+
! * Sequences of + and -.
int
i = ++ i + 1
i
++i
long int
i
++i
5
7
6
i
1
€
#
+- * / %
6 3 7 1
# 6 $ 3
€ 7 ! #
2
3
2
7
6
c
!
1
$
1!
!
1 "
- -
3 ( 0
# $
" - - - - $ 0 2 "
- #- ! " !
0 2
$ (
#
6
3 7
-
!
/
" "
0 #
#
4
(
H -
#-
-
-
- -
1
*
*
.
$
!
! 2 #
6 !
3 7 " 2 !
*
,
E
D
!
6
"3 7
7
, *
6
* !
,
* ,
$
2.2.12 Exercises
!
!
/
"
0 #
%
H
,
%
)
A
&
1
* & , H 1 ! +
!
* & *
, < ! )
3
7
6
HI ,
*
!
)
*
;
C
) &
1
!
* B ?)
H
,
, , 1 1
! !
,
!
* 12
!
!
B
?
?
1
, 1
!
! , ! 0
6
3 7
H
,
* ,
B
1
1
, * *
,
6
3 7
H ? &
H A
- C
*
! * /
b
a/(b*c)
"
0 #
)
/5
#
*
) * C
# * ) * - C * )
int
unsigned int
5 b
0" #
a/b/c
c
0
b, c > 0
a b
(
a
#
!
/
!
)
c=-a=b
b*=++a+b
a+3*--b+a++
0
!
a
" *
C
+
H ,
) , ? ,
-
10%6*8%3
31/4/2
€
$
#
2 !
!
4 " # / ! $ ! #
H
H
(
:
* &
H
% * ,
< * , &
, H * *
)
b
-
, c=a
b)
B
* c=a+7+--b
a-a/b*b
7+a=b*2
€$
Exercise 15
Exercise 14
$
(a
+
unsigned int
%
, *
Exercise 13
-
, B
2.2.11 Goals
unsigned int
*
*
, B
H std::cout
-2-4*3
5u+5*3u
!
!
%
* std :: cout < < a < < " ^8 = " < < b * b < < " .\ n"
Exercise 12
, *
!
%
A *
nextvalue
<
, int
H
1
,
, *
! ,
6 * /
* !
*
* !
, 7
!
A
* *
( std::cout
H
1
* * &
:
( nextvalue *= 5) += 3
$! # 0
/
!
,
1
*
)
* ,
,
!
H
*
A
7
(nextvalue *= 5)
H
H
H
H
,
* +
€7
Exercise 11
€
7
A
nextvalue = 5 * nextvalue + 3
-
1
7
int
int
- 2
*
*
HI 1
,
? G
nextvalue
B
7
6
i = ++i + 1
)
6
int
* 1
unsigned int
-
! * B7 )
#
*
3
Operational.
7
6
Dispositional.
, 1
* 3
€
Single Modification Rule:
unsigned int
6-3+4*5
-1-1u+1-(-1)
c=a=-b
a-a*+-b
b+++--a
-
(bc).
"€
"
5
69
5
7
6
8
4
9
7
8
8
67
k = 3
3 " $
1
# !
1
B
1
2 8 2
k
a
3
2
! 2
/
#
0
4
8
7
6
2 -
|y|
6 #
3 7 !
2
a
# (
$ ! x, y
!
0 2 # 2
! $
#
/
−2.5
$ 0 ;
0 # 1
0 0
( , #
" ,
1
x + y/9
13 4/9
−1 − 1/9 = −10/9
/ !
"
" !
−2
0
2.2.13 Challenges
4
1
" #
!
2
projecteuler.net/
" #
$ % " ! 2 4 ! " k
0
!
$ 6 3 7 ! {1, . . . , 41}
2 !
#
* 1
!
!
;
:
-
"
!
# ! ;
!
!
"
!
4
-
!
2 6 "
3 7 2
0$
+
" "
3
0
9
/5 %
! # {0, ..., 40}
*
A
* *
!
p(k)
(3, 4, 5)
!
#
0
1
!
!
$
4
$
&
! k
"
(a, b, c)
!
/
! " @ 0
$
-
-
-
#
$
0" #
!
4
0
(
$ ;
!
!
3 #
- #
2
$
!
<
6 3 7 1
- 2
2, 5, 8, ..., 38, 0, 4, ...
!
0
" $
! "
$%
"
!
#
$ 0
Exercise 22
a2 + b 2 = c 2
pythagoras.C
1000
"
!
4
- !
" - ? - #
? #
- $ 0" - ? 2
243,112,609 − 1
2
( . !
2 - " $ # % 2 * 8
# 0
$
$ * 2 %
" ! . " ! <
2
! 1 #
!
0 0
" ! 2 . 4
"
$
x y9
"
-
threebin.C
0, 1, ..., 40
"
( " !
$! ! /
#
celsius.C
2
;
0
0
!
4
13 49
-1 -1/9
! $ ! !
.
!"
# # !
( *
" *
- 0 " <
/
1
-5 / 2
"
/
!
!
Exercise 17
!
;
Exercise 21
0
Exercise 20
; #
! 3 " " 2
Exercise 19
1 3 2
0 &
# 0 2 - 0
;
" !
3 ; ( ! #
0
Exercise 18
.
/ - ! 0 ; " 3
! 0 #
) ! 2
! (
$
(
!
Hint:
3
Exercise 16
a+b+c =
http://
"
"
5
69
5
7
6
8
4
9
7
8
8
67
% 1
# * 1 ;
!
*
x∧y = 1
: (x, y) → x ∧ y
x = 1
y = 1
#
{
,
,
}
#
$
1
#
#
"
!
"
!
#
"
!
x
0
1
F
#
2
#
&
"
x↑y
1
1
1
0
1
#
y
0
1
0
1
F
6
* 7
x
0
0
1
1
#
%
! #
!
"
!
y
#
1
*
E
! F
A *
* A
; !
)
!
* ( ,
H ! *
! *
H /5
2
0
1
1
0
! <
7
: (x, y) → x ↑ y
1
2
" x
$
$ !
;
% H #
!
*
*
<
- 1
!
F
$
#
!
$
y
0
1
0
1
0
!
-
(
#
$ 2
#
1
D
E
F
A
!
x
0
0
1
1
D
+!
# 0
2 #
0 ! #
/ !
! &
&
$0 $ #
x∨y
0
1
1
1
/ ! (x, y),
f
F
#
,
1
1
Figure 4:
#
f
#
%
(
#
1
*
H
%
(
E
!
*
A
!
? * 0
* , 0
2 /
!
" y
0
1
0
1
%
D
!
H )
(x, y) =
-
! H !
- *
%
# %
6
#
*
(
+
'
'
x
0
0
1
1
#
%
1
6
!
7
1
*
1
H
*
,
H *
!
,
* IH
!
1 , 1
1
*
% 1 , ! *
, , !
!
$
.
0
-
7
* 1
!
F
6
/
2
2
*
D
H E
IH
D
F )*
* 6 7
+ E!
-
!
* ! *
!
*
3
0
$0
.
!
*
1 &
< D F
E @ * 1 6
7
*
D +
* !
H H E! 1 !
f : (x, y) → x ∨ y
F
# $ &
1
D
E
7
H ,
x∧y
0
0
0
1
y
<
% !
! *
H * *
*
H *
A
!
*
: (x, y) → x
!
*
(x, y) =
#
, *
(
IH A ! %
* ! ! , 1 1
,
)* C
* <
* % & , * ? * , < '
'
Completeness.
2
Theorem 1
.
0
E
D
,
6 %* * H ! * ,
)
H *
1
1 1 ! *
A
H
1
!
* ! 1 ! *
H HI H
,
H
y
0
1
0
1
Definition 2
generated
x
y
$ #
$ # # $ # " # $ #
$ # 2
#
2
;
1815 1864
c0 : x → 0
0 := 1
n
1
-
x
0
0
1
1
#
:x→x
n
#
!
* * ! ;
*
1 * H * 3
1 * ;
1 6
; =
7
:= {0, 1}
:x→x
→
&
# #
7 1
f:
n
2.3.1 Boolean functions
6
1
bool
&
!
;
!
*
H
*
- !
*
H
& ) &
<
B
F )
? H
0
F
@ 6
7
+
€ A
1
!
/
)* ;
!
*
1 - &
, !
,
bool
2
n=1
c1 : x → 1
1 := 0
!
* !
*
1
;
!
*
HI
+
*
0
! %
#
!
HI !
*
1 !
!
* , (
*
!
! *
1
* ,
% ) *
1 ! 2.3 Booleans
x
1
0
(x, y))).
(x, y)).
f
F
F
complete
""
"
5
5
5
69
5
7
6
8
7
4
69
8
H
0
,
(0 == 0) == 0 −→ true == 0 −→ 1 == 0 −→ false ,
a=b=c
H
!
-
==
bool
F
A
0
*
HI
1
* !
1
-
* (
!
€ H 6
%
H # 7
! 6
% H 7
6
- 7
, # ! ; H * 1 ? &&
bool
)
'
'
%
*
1 ,
!
H
&&
, * , # 1 * , ! ) , )
- !
! H
H
!
-
0
;
1
%
1
!
*
,
1
,
" ;
!
1
*
-
!
#
H !
*
F
) *
+
; H H % ! # !
* * *
*
1
* 1
*
* * 1
* , *
*
,
*
H 1
%
0
H H
H
* H
-
!
a=b=c
a == b == c
int
, *
**
*
,
*
#
! H !
1
int
1
; , * * * ,
1 * *
*
%
!
C
1
A
A
#
%
F ) !
c
C
#
%
bool
&&
1 H *
1
* H (
*
- %
!
)
-
&
+
||
H H
)
, 1
!
H
==
a b
! !
HI
! *
C ! * C H
*
, !=
Boolean Evaluation Rule:
(a == b) == c
H
)
%
!
Relational operators.
bool
<= >= ==
!=
!
+!
, !
* *
*
*
,
*
*
*
*
Logical operators.
&&
#
* , C
Watch out!
!
F
*
*
A
!
F H
!
* , )
* H
, *
*
1
!
* *
!
*
* -
% *
# *
bool
bool
bool b = true ;
false
true
!
*
D *
,
*
? E ! * H
*
bool
bool
}
* H 1
*
}
;
f0000 (x, y) = 0.
||
(f1000 (x, y), f0100(x, y)),
(
(f0100 (x, y), f0010(x, y)), f0001(x, y)).
!
* -
1
*
1
A F ) +
*
,
f0010
;
H *
1
, 1
H *
(x, y),
(x,
(y)),
(y,
(x)),
(
(x, y)).
1
,
1
F H !
A
(0, 0, 0, 1)
bool
} {
2
,
unsigned int
{
6
* {
! * % # %
*
#
-
H
!
! H 1 ,
; ) *
€"
A
f1100 (x, y) =
f0111 (x, y) =
!
*
1
;
'
'
!
*
(y))
0010
# 1
!
* +
,
*
H H
+
(x,
=
=
=
=
!
, F ) H *
f0001 (x, y)
f0010 (x, y)
f0100 (x, y)
f1000 (x, y)
H
b
&
F
,
H
1
!
H
(* & € € 1 7 Proof.
f
(f(0, 0), f(0, 1), f(1, 0), f(1, 1))
0001
f b1 b2 b3 b4
b1 b2 b3 b4
= f0001
||
< >
<=
>=
<
==
=
f0000
7 + x < y && y != 3 * z
((7 + x ) < y ) && ( y != (3 * z )).
a == b == c
2.3.2 The type bool
a == b && b == c.
5
69
5
7
6
8
7
4
"
"
5
5
69
8
x
#
%
!
0
,
=
-
!
*
C !
*
1
-
x==0
c 1
)
1 & ,
+!
,
!
!
€
!
€
! !
*
, #
!
* * )
, , H
*
&
!
"
€
;
%
!
€
&
1 A
& !
+ ** * *
,
) !
* & *
!
€
!
€
!
,
! , *
*
!
€
||
( / 6 =
$ " 3
7 :
#
!
*
A
%
Naming.
H
* "
(
, HI !
1 ,
# * H #
B
! €
) *
€
*
* )
&
, - &
, 1
, * !
A
!
1
!
!
!
&
1
, @
€
-
HI
&&
! &
,
H 1
& * :
, H ! , !
1
, / *
H * (
*
* €€
€
€
!
,
H
, ? *
C
,
, * *
**
?
)
!
,
-
!
7 *
D
!
E 1
C , ! !
F ) &
H /
!
6
)
!
)
!
bool
%
*
-
!
* ,
* !
#
)
!
!
? A
+
*
)
)
?
* IH
,
# !
* , * ,
*
,
,
*
: ,
6 = " 7 !
H 1 !
* ;
1 * *
% %
#
,
A
*
H *
-
- !
? : !
?
! D
* *
6 ' E
' ? A
7 *1
A
0
bool
-
Prec. Assoc.
int
C
Arity
!
* %
* * 1 * ) 2
C C C
* Operator
!
<
>
<=
>=
==
!=
&&
||
H H
,
#
* ,
* Description
+
1
!
!
* *
-
%
#
" 2 !
!
F *
* !
1 1
* * !
1
* , !
* x >= y && x + 1 <= z && y > 5 * z && y > 7 * z
1
!
/
!( x < y || x + 1 > z ) && !( y <= 5 * z || !( y > 7 * z ))
)
* ,
*
,
+
* H *
* ,
!
- ,
*
Conversion and promotion.
bool
0
Table 2:
, *
H
% +
!
* * , !
H
* *
,
' ' &
1
!
!( x && y ) == (! x || ! y )
! De Morgan’s laws.
bool b = 5; // b is initialized to true
int i = b ; // i is initialized to 1
!( x || y ) == (! x && ! y ) .
2.3.3 Short circuit evaluation
||
&&
x != 0 && z / x > y
2.3.4 Details
5
69
5
7
6
8
7
4
"$
5
5
69
8
! 1
b
32
#
{
,
}
#
x2
...
xn
3 7
6
x1
7
1
, 7
, *
7
3
7
6
#
&
#
$
0
3
/
→
6 3€ 7 #
#
H
6
7
6
n
1
, f:
#
! 7
#
%
,
, #
%
,
(
! *
H
6
H
6
(
%
#
x, y, z
H
6
(x ∨ y) ∧ z = (x ∧ z) ∨ (y ∧ z)
(x ∧ y) ∨ z = (x ∨ z) ∧ (y ∨ z)
#
z)
"
!
(y
x 1 , . . . , xn n
xi 1 i n
2
z=x
%
n
*
* !
*
!
"
!
! -
!
! * *
)
* * H
! *
!
C
*
6
3 7
6
3 7
,
* , 6
* )
, , 7 ,
*H *H 6 * * H 1 H H 7 ,
, 12 *
! B
, !
1
;
!
*
* B ,
3
6
€7
!
*
B
1
;
* 1
,
,
3
7
6
2
0
#
+
1
1
H
* 1
7
* ,
;
, !
)
,
, * * * ! * * , IH
2
1
!
B
H
#
,
,
, *
,
H A
€7
bool
0
# 2
#
0 "
(x ↑ y) ↑ z = x ↑ (y ↑ z)
y)
-
Exercise 26
Exercise 25
(x
7
Exercise 24
#
Operational.
17
-
H
2
<
" 0
" "
*
*
! A7
*
1
B * ;
!
*
1 !
*
*
1
) 1
1
H %
Dispositional.
&
7
Prec. Assoc.
*
~
%
7
- (2) = 13
! H
1111
$
H
15
!
"
H
" Pb
i
b
x =
i=0 ai 2
ai , bi {0, 1}
0 i b
bitwise operator ϕf
P f : {0, 1} → {0, 1}
ϕf (x) = bi=0 f(ai )2i
g : {0, 1}2 → {0, 1}
bitwise operator ϕg
P
ϕg (x, y) = bi=0 g(ai , bi)2i
2
$
H
$
$ & &
#
#
"
!
!
, *
(
! *
1 €
* €
! * *
1
; C
H HI 1
1
7
~0
!
€
€
^
* €
€
Exercise 23
€
€
H
ϕ
(13, 9) = 6
A
* *
F
- H
, ?
1
* ?
1
!
*
Arity
0
Operator
~
&
^
|
&=
^=
|=
1
|
#
1
* (4, 13) = 13 ϕ
$ 1
Description
1
) - 0
64
)
(
,
% # 1
H 1
H &
1 !
H 1
' '
1
&
ϕ
%
Table 3:
0 0001
~
* , 6 ! )* !
! 7 , * ! %
- 1 *
- H
* 1
*
1
0000
!
*
! !
,
* & 1 ' &
'
H *
1
g
, €
€ @ * ! C !
$ 6
A 7
*
3 0 ? * (
f
H
,
1
! * ( € 1
€ *
, ! <
,
) !
-
& , H
)
,
, 1
Definition 3
P
y = bi=0 bi 2i
!
*
1
!
1
* ? 1 * ,
?
1
Bitwise operators.
2.3.5 Goals
bool
bool
2.3.6 Exercises
)
)
5
5
5
69
5
7
6
8
7
4
€
69
8
{ int main ()
{
# include < iostream >
1
1
1
AND}
| 1 {z
1
|0 1{zOR} 1
1
#
1
%
0
#
1
#
2
0
!
0
#
n+1
0
( # $ 0
n
" 2
2
#
/ ! !
% 0 1 -
#
%
1
%
# %
3
#
%
-
#
%
(
-
0 0
AND}
| 1 {z
0 0
{z }
| NOT
#
%
(0,
#
0
! /
#
2 /
! ,
3 !
! $
% 8
0 0 1 %
0 3
2 !
!
#
!
#
/
!
(
0
$
#
%
!
0
#
0
/ "
/
!
!
% 2
.
,
!
0
(
-
Exercise 32
.
# /
!
#
!
!
. 001
"
(
! #
! 2
.
# 0
"
0 ( /
$ ! ! 2 - "
$ 0
0
#
# !
! 3 * z > z || 1 / x != 0 && 3 + 4 >= 7
z > 1 && ! x != 2 - 2 == 1 && y
*
!
*
;
!
1
#
7
H
-
*
#
%
7
} { | a
$
"
x != 3 < 2 || y && -3 <= 4 - 2 * 3
6 3 7 !
6 #
3 7 $
2
%
}
$ !
6 3 7 " !
#
$
!
%
7
}
/
# include < iostream >
int main ()
{
int a ;
std :: cin > > a ;
std :: cout < < ( a ++ < 3) < < " .\ n" ;
bool b = a * 3 > a + 4 && !( a >= 5);
std :: cout < < (! b || ++ a > 4) < < " .\ n";
return 0;
}
<
z==2
6 3 7 1
1
1 !$ int
a < b && b < c
< 0
" !"
6 3
7 2
"
!
2
+
#
17
-
}
}
%
#
#
)
x==0 y==1
a < b < c
*
/5
}
c
!
*
!
,
,
int
0 "
!
0
!
!
/ #
@
:=
/ ! $ !
#
#
" 2 0" !
2.3.7 Challenges
%
2 #
0 "
/
!
" !
2 {
!
Exercise 28
7
a b
!
17
,
/ ! $ !
#
#
" 2 0 "
!
"
$ # /
Exercise 30
" $
$
5
Exercise 29
x y
z
4
7
}
!
Exercise 31
" *
Exercise 27
b
c
!
,
$
{
| } { {
$
4
{
" !
{
unsigned int a;
std :: cin > > a;
unsigned int b = a ;
b /= 2 + b / 2;
std :: cout < < b < < "\n" ;
bool c = a < 1 || b != 0 && 2 * a / ( a - 1) > 2;
std :: cout < < c < < "\n" ;
}
return 0;
(0, 1))), 1)
.
n
5
5
5
69
5
7
6
8
7
4
69
8
#
* )*
1
*
!
* F
,
* 1
)
!
>
,
cc
- *
H
>
,
IH B
!
!
,
* H G
,
!
-
if
,
7
6
* G
*
!
, * ) * )*
+
'
'
* 1
,
- if
!
!
*
1
*
bool
H
!
-
H , 1
1
?
, H
IH !
G
!
*
* (
- , !
H ! * !
, * * 0
)*
-
!
*
* G
C
)* * ! H , *
, -
!
1 ! & *
( *
!
C
*
)*
!
" eval.C
/
#
"
$ ! 2
2
0
$ ! 2 * # # =< ! # =
* 1
A
2 0
2
! $! 0 0 2
0 .
/ 0
!
" ! "
!
(
0 1
1
!
#
/
!
#
.
.
% : ! '
)* ( ! , - #
$
4
# %
$! 4
# $
.
0
" #
% 2 /
!
0 (
2
2
-
! 3
1 !
/
#
$
# !
"
/ /
!
2 2
4
!
"
8
#
%
-
#
%
/5
2
4
0
2 0
!
$ (0, 1))), 1),
1
if
*
$! !
s
)* * ! !
<
+
./ eval |./ zero |./ one |./ or
./ eval |./ zero |./ zero |./ one |./ and |./ not |./ or |./ one |./ and
0
!
eval
1
(
|
#
!
!
#
!
! !
2 - . 0 ! #
# " ! (
0 " !
! $ !
# " #
#
s
)
s
1
!
!
$
0
"
!
not.C n = 1
(1, 0)
not
not.C
2 " $
and.C or.C
{0, 1}
s
!
/
3
# !
0
"
!
0
one.C
0
! (
s
$ $! $ 2 /
! / ! 2
/ /
! "
!
1
0
%
#
2
!
s
" n=2
#
!
" . ! $
!
!
# " !
$! ! 2 2
#
$ 0
%
!
$
"
zero.C
1
"
!
.
0
3
- ! " !
#
!"
" #
#
: #
/ !
"
" $ !
#
or.C
and
-
$
0
#
2
$! 0
!
/
n
(
<
!
! and.C
(1, 1, 0)
(1, 1, 1)
!
(
% !
! #
2 $
#
# !
"
0
(0,
2 / $
" $
# !
# "
/ ! !
#
#
! $
!
Hint:
2
!
"
!
(
!
/
"
" #
$0
.
2.4 Control statements
2.4.1 Selection: if– and if-else statements
5
5
5
69
5
7
6
8
7
4
"
69
8
) +
/ ! for
sum_n.C
for
1
1 )
+
H
)*
* A
sum_n.C
IH
if
* (
for
* * H
€ )
+
! *
HI H H *
& * , 1 1
* !
,
?
B
7
6
D*
E
,
+
)
&
1 H
1
) ,
*
*
+
!
!
* H *
,
1
H
,
even
!
E
D
-
2
"
!
<
Program 7:
%
H
for statement.
*
for
i
%
1 * &
, , 1
+ H +
&
)
,
1
*
* H *
, n
2.4.2 Iteration: for statements
i
n
!
1
a
&
:
,
H
!
H
odd
i=1
A
1
!
{|
!
!
*
)*
)*
B H H B
!
,
!
< ! , F H )
* ! 7
6
)
-
1
1
*
1
! E
D
,
1
!
1
,
* !
H // Program : sum_n .C
// Compute the sum of the first n natural numbers .
} { | } { int a;
std :: cin > > a;
if ( a % 2 == 0)
std :: cout < < " even ";
else
std :: cout < < " odd ";
Pn
!
1
else
Sn =
else
! B
H D
* E
1 A 1
!
a
even
s
if-else
a
+ ! 1
6
$ * ! * * /
7
H if
1
,
*
H , n
* ,
1
!
& H ?
* , H &
* H
1 )
, 1
+!
else
else
*
%
+
!
!
)* *
G
HI ,
1 1
/ if
* 6 *
7 +
, ( * *
H !
D
* * , E
if
1 ! * !
if
a
H
& 1
IH
!
, ,
1
int a;
std :: cin > > a;
if ( a % 2 == 0) std :: cout < < " even ";
# include < iostream >
int main ()
{
// input
std :: cout < < " Compute the sum 1+...+ n for n =? " ;
unsigned int n;
std :: cin > > n;
// computation of sum_ {i =1}^ n i
unsigned int s = 0;
for ( unsigned int i = 1; i <= n ; ++ i ) s += i ;
// output
std :: cout < < " 1+...+ " < < n < < " = " < < s < < " .\ n ";
return 0;
}
for
for
if
bool
5
69
5
7
6
8
7
4
9
67
8
8
7
5
45
79
67
n
n
H
, A
1
?
* (
*
1
*
1
!
*
-
c s = n * ( n + 1) / 2;
*
,
!
3
€
$
$$
$$
$
H
B
!
*
H
€
,
1
* !
!
H
C
* A
*
* 1
1 *
* * H 1
,
*
*
H 1
3
*
!
1
H
H
#
H
H
7
1
A
*
-
*
)
+
+
1
* H
3
*
,
*
* F * 3
6
=
!
,
H )* !
%
!
!
*
+!
H
#
H ,
+
!
-
&
)*
!
*
!
*
, A
@
1
*
H H
H
#
H * !
+
!
sum_n.C
,n
* !
* *
A
100 101 = 10100
*
sum_n.C
n
+
H
+
for ( unsigned int i = 1; i <= n ; ++ i ) s += i ;
for
!
H 1, 2, 3, . . . , 100
,
H
!
- s
#
n(n + 1)
Prime numbers.
F
n
H
)
101
i=1
n
X
)
H
Gauss.
& A
H A
1 ,
! 1
! for (;;)
n == 2
1
)*
i <= n
* %*
* *
# - i
d
, , < 1
! 1
+ ! , 1
! H * -
1 IH ! !
* * ! ! ! 1 !
for (;;);
*
-
1
1
*
H
*
<
for
:
0 H
A
H
*
&
* !
*
A
H
!
1 ,
* *
* A
D
1
* * E
H
&
*6
, !
6 "
!
# 7 7
*
- ! 1
, * 1
*
,
,
*
!
1
, !
1
**
! ,
*
+! 1
F
H )*
for
for
for
1
,
H
*
1
)*
H *
% F
H
H % , H
) ! 1
& * * &
1 - , * i
,
+
s += i
i == 3
3 <= 2
for
i <= n
1
+
2 <= 2
i
1
%
- !
H 1
C * H , 1
! *
!
F H
*
n == 2
i <= n
1 <= 2
s
1
!
H A
! *
& *
!
1 for
*
*
3
1
Infinite loops.
s
H &
H
)
%
! , E +
* ,
* H )
*
- ! H * 1 ! D!
3
for
for
+
H
*
1
! H ,
1
+
&
,
#
!
(
*
* * / H
D
A /
*
!
*
,
E
- & , +
! * * H * H , 1 <
1
+
* &
1
!
,
, 1 )* ) -
1
* 0 !
1
* * 1 ,
* !
, A
1
* 1 ! !
, 1
+
sum_n.C
D E *
,
H IH
, 1 *
1 ) *
*
*
,
A ! !
!
<
+ !
sum_n.C
for
1
@
2 < A
* A
1 *
) +
* H &
1 s += i
i == 2
i
5050
i = n(n + 1)/2,
{2, . . . , n − 1}
2
n
5
69
5
7
6
8
7
4
9
$
67
8
8
7
5
45
79
67
*
* G !
* ; * H A *
3
*
1 '
1
'
{
2.4.3 Blocks and scope
}
1
! 1 1 * * A A * * * 6
!
,
1 % * 1
A H
!
% *
)
* 7
%
* * 1 ) * * <
return 0;
Visibility.
1
,
* ,
*
int main ()
{
{
int i = 2;
}
std :: cout < < i ; // error , undeclared identifier
return 0;
}
D
1
*
H ?
*
* )*
, ! *
!
F
H )
H
s
A
*
A
H
*
sum_n.C
<
1
! *
IH A
*
*
*
F
) !
1
*
*
C
# include < iostream >
"
%
!
*
* 1
@ , 1
* H A
1
, 1 * *
A !
- 1 * A * 1 * 1
A {}
! 1
*
*
A
- *
1 * A
* *
C *
* ! * 1
H A
? *
#
)* $ !
// Program : prime .C
// Test if a given natural number is prime .
int main ()
{
// Input
unsigned int n;
std :: cout < < " Test if n >1 is prime for n =? ";
std :: cin > > n;
{2, . . . , n − 1}
// Computation : test possible divisors d
unsigned int d;
for ( d = 2; n % d != 0; ++ d );
, !
- 1 ;
*
*
* A A
! ! 1 ! 1 * 1 * A ! 1
, * !
* !2 1 H * 1
,
,
)
1 IH *
&
1
A
"" " H -
1
? &
,
n % d != 0
!
d == n
;
1 E
D E *
!
&
* *
* +
*
1 * // Output
if ( d < n)
// d is a divisor of n in {2 ,... ,n -1}
std :: cout < < n < < " = " < < d < < " * " < < n / d < < " .\ n";
else
// no proper divisor found
std :: cout < < n < < " is prime .\ n ";
!
! 7 #
!
# H
H , ! , ! B !
6
H
H
6
HI
,
1
A 1 ' '
H
1
* H , , 1 ? if
,D
1
E
!
1
* A * F
)
H n
2
!
{
H * 1
*
n
{ }{ { { {{ { {| } { | } for
"
!
Program 8:
!
*
C
+
for
}
( * * ! H 1 * A * C 1
* ! 1 * +
* H
*
n
main
main
for ( unsigned int i = 1; i <= n ; ++ i ) {
s += i;
std :: cerr < < i < < " -th partial sum is " < < s < < " \n";
}
s
€
5
69
5
7
6
8
4
9
7
67
8
8
7
5
45
79
67
!
*
i
H A7
*
E
i
!
<
(
!
# ( " 3 $ !
- !
#
(" " (
$ #
- -
2 -
0
D
!
2
"
"
" 0
0 !
" "
#
1
, !
A
* A
for
E3
= * ,
H 1
1
* A
* ,
1
5
7
6
8
4
9
7
67
8
8
7
5
45
79
67
69
5
E2
0
*
"
-
HI
!
*
!
*
E1
#
* * H H
, *
1 & * *
! +
F
)
D
!
%
D, E1 , E2 , E3
,
i
i
i
0 1 2 3 4
H
,
H
H
H
* H ! !
E3
H
H
"
| &
} { E2
1
E
* -
! !
- * ! !
H"
&
H * - 1 *
"
!
,
1
&
)
,
%
!
E1
=
6
* 6
1
&
* A
,
+
* H
E7
*
H * )
D
!
&
!
*
!
D
+
Program 9:
std :: cout < < i ;
return 0;
* D
*
A
1
, ! * !
H
!
!
* * -
*
* * "
;
D * 1 * ,
, 1 E 6 * , !
!
H
! !
* * *
H *
*
* * *
* E2
D
F , * 1 * * H !
*
!
! 1
<
*
% H
!
& * * * +
* (
*
* *
1
1
* H A
, !
*
int main ()
{
for ( unsigned int i = 0; i < 10; ++ i ) s += i ;
std :: cout < < i ; // error , undeclared identifier
return 0;
}
!
# include < iostream >
*
+ *
* ,
!
,
1
*
&
*
H
1 !
*
?
2
i
*
&
! H * , 1 6
A
$
7
, !
,
* !
H (
* €
! F "
Figure 5:
$ H
* , +
* *
D
**
D
! !
* * !
! < F * 7
* -
!
!
* * 1
! $ *
*
* *
1
%
* !
+
! H *
?
H
A
* , *
!
!
* i
*
int main ()
{
int i = 2;
for ( int i = 0; i < 5; ++ i )
std :: cout < < i ;
// outputs 0 , 1 , 2 , 3 , 4
* *
! 1
* 1
C *
! !
! * IH
*
D
*
H
F
!
1 * !
!
* * ! # - * * D
H
)
%
) - * ! !
* * , !
*
1
1
Declarative region.
!
, ,
!
i
* Control statements and blocks.
} { Scope.
i
E 1 , E3
// outputs 2
}
2
for
E
D
D
#
std::cin
+
*
std::cout
# include < iostream >
1
// Program : collatz . C
// Compute the Collatz sequence of a number n.
1
<
1
!
H
H
H
H
? !
-
B
7
B6
(
* H %
1
!
H
C
, !
1
! *
F
,
A *
1
*
*
, H
1 A * # )* * H
7
6
!
**
?
1
?
1
H , !
* F
! *
H
,
* .
1
* =
H *
=
*
;
A H "
A * 6
, * 7
6
*
H 7
+ ! *
H 1 A * ! * 1 H * * = "
1
-
+
(
!
) H & ,
H 1 H *
! !
,
, 1
1 , H "
HI * * while
*
# )*
for
1
while
(ni )i
H
n
, 3
1
while
(
*
- 1 % H IH
* while
for
*
, 1 ! *
* *
+
6
*
* H 7
!
,
1
, * 1 ,
, 1
% * , *
H )*
*
! for
n=5
i
*
!
The Collatz problem.
n0 , n1 , n 2 , . . .
n0 = n
ni−1
ni−1 /2,
ni =
3ni−1 + 1,
ni−1
,
!
for
*
,
< ) , A
!
,
, ;
1 *
*
1
*
,
* c , ! H
D *
! * E
! !
, 1 , ,
1
A
* 1
*
for
C
!
n
!
*
!
*
)
A !
, * 1
*
*
*
!
%
?
)*
- 1
,
* "
F
)
H for
- ) *
* * A
* 1 A
1
) +
!
*
IH
*
H
*
% while
k
for
F
) , H !
H
HI C
*
(
*
C
*
} { int i = 5;
for ( int j = 0; j < 5; ++ j ) {
std :: cout < < ++ i ; // outputs 6 , 7 , 8 , 9 , 10
int k = 2;
std :: cout < < - - k ; // outputs 1 , 1 , 1 , 1 , 1
}
H
*
,
1
&
(
*
1
1
*
*
!
! , * * ! H
B
* /
* / !
! 0 H k
1
,
H
, !
* D
!
, E
1
1
H ,
- 1
&
*
*
1
1 *
! "
for ( unsigned int i = 0; i < 10; ++ i ) {
int k = 2;
// do something with k
}
{ )* A
) *
! H
- *
, 1 6
*
D
1 * E
7
* 1
,
H
2
Storage duration.
2.4.4 Iteration: while statements
while
for
for
1.
1
5, 16, 8, 4, 2, 1, 4, 2, 1, . . .
"
5
69
5
7
6
8
4
9
7
67
8
8
7
5
45
79
67
do
a
do
? H H
1
!
*
!
,
!
*
&
H
+
!
!
H 1
HI
A
* H
*
-
B
1
!
*
1
, ! 1 *
bool firsttime = true; firsttime ||
a
1
)
1
while
1 IH H ;
1
, ! )
A
H * * * *
, D
*
1 ? *
6 E
!
* *
*
* * , H * ! )*
H
* - 1
* ( C
€ *
&
1 H 7 !
do
#
A
* *
, *
* *
%
7
6
2
"
!
<
%
, H * *
1
&
!
!
C
, )*
!
!
H #
H
H
H
H
{{ {
{
do
* H
n = 27
for
H
H
H
H
H
H
H
H
@ 1
,
& H !
) H !
1
C *
! H !
}
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
1
H H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
n
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
1
*
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
for
1
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121
364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526
263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754
377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079
3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154
3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488
244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4
2 1
H
H
while
+
H
A H
*
1 !
< * 1
+
!
-
{|
} { | * , &
!
%
+! !
*
!
&
)*
? !
1
,
*
,
*
6
B7
while
)
!
!
-
#
!
= ,
!
HI 1 ! !
#
,
)* !
H *
*
while
do
,
IH )*
*
while
for
Do
A H // Iteration
while ( n > 1) {
if ( n % 2 == 0)
n = n / 2;
else
n = 3 * n + 1;
std :: cout < < n < < " ";
}
std :: cout < < "\ n";
return 0;
! 1
!
1 , 1 A 1
* 1
* * A
6
* +
, 1
, 1 1 ,
* *
! ! !
* , * 2 !
% H
1
* ?
* collatz.C
,
IH A
1
1
Program 10:
H 7
1
*
,
, *
#
int main ()
{
// Input
std :: cout < < " Compute the Collatz sequence for n =? ";
unsigned int n;
std :: cin > > n;
}
2.4.5 Iteration: do statements
do
do
bool
for
firsttime = false
0
int a ;
// next input value
int s = 0; // sum of values so far
do {
std :: cout < < " next number =? ";
std :: cin > > a;
s += a;
std :: cout < < " sum = " < < s < < " \n" ;
} while ( a != 0);
n
a != 0
5
69
5
7
6
8
7
4
9
67
8
8
7
5
45
79
67
#
*
H for
while
do
while
for
do
!
H 1
!
)
1
!
* =
)*
H ?
*
H A 1 A
H )*
)* H * !
! *
1 - +!
!
do
continue
H !
)*
c
B
B
*
)
B
*
2
*
-
- & 2 -
* /
2
B ) * continue
H
*
1
,
)* * 1 , +!
while
continue
H
A
0
)*
2
E
G
*
D2
H !
* !
*
G
1
1
*
1
*
G
*
2 * &
*
1
!
*
!
H * A
!
@
2 G
) 1
,
1
* ,
* *
* ,
* H 1
+
H 2
A
H
!
+
H *
* 1
H !
* D!
* ,
E
C
!
*
H 1
%
*
*
!
! H A ! )
* 1 !
1 * *
A , 1
*
*
=
G )
1 6
* *
H 7
2
,
break
break
=
for
H
*
) G
D
!
E
, !
! , !
* ! ! 6 7 F )
H
)*
*
for
+
*
*
0
break
!
*
for (;;) {
std :: cout < < " next number =? " ;
std :: cin > > a;
if ( a == 0) break ;
s += a;
std :: cout < < " sum = " < < s < < " \n" ;
}
1 %
* A H 1
1
* 1
*
&
for (;;) break ;
break
!
*
!
, +
*
*
! )
!
H
- * 1 1
break
!
break
+
do {
std :: cout < < " next number =? " ;
std :: cin > > a;
if ( a == 0) break ;
s += a;
!
do
C
,
1
, )
) , = *
? H * !
C
, H
!
D
*
E
while
switch
break
2.4.6 Jump statements
std :: cout < < " sum = " < < s < < " \n" ;
} while ( true );
a != 0
do {
std :: cout < < " next number =? " ;
std :: cin > > a;
if ( a != 0) {
s += a ;
std :: cout < < " sum = " < < s < < "\ n";
}
} while ( a != 0);
continue
for (;;) {
std :: cout < < " next number =? " ;
std :: cin > > a;
if ( a < 0) continue ;
if ( a == 0) break ;
s += a;
std :: cout < < " sum = " < < s < < " \n" ;
}
2.4.7 Equivalence of iteration statements
$
$
5
69
5
7
6
8
4
9
7
67
8
8
7
5
45
79
67
H
H
H
H
1
)*
! ) c ,
* H
H ,
! # )*
!
F
H ,
1
* A
!
*
1 6
* H A *
/
%
7
* !
-
*
A
* & H )
, for
H
A
*
*
b
)*
A H
+! while
#
*
+
IH
( * #
H
# *
, #
)
! !
H
B
7
B
7
6
A
A
< &
6
7
6
while
bool b false
while b = !b
7 B
7
6
?
A
*
H
)
!
! H
,
H
< A
+
+
H 1
H H A
* , *
!
)*
continue
#
for
-
* H
* 1
<
!
(
!
* !
* &
! +
!
, C
IH
A HI *
& ! * 1
, B
E A
* C , * * ,
D ,
!
*
* H
E
7
6
?
)
H
, 1
!
<
1
1
)
)
*
D
+
!
H 6
!
*
C
, :
*
H *
<
+
*
?
B
7
(
for
* H
break
b
for
while
b
1
b
H
while
H
if
1
!
while
while
break
do
B7
6
,
#
7
! ,
1
*
A H c *
A
* H
1 1
* 2
+
!
* H
<
!
! 1
!
* ! * D E!
1
⇒ do
while
while
# A
)*
H
do
⇒ for
A⇒B
#
6
@
B
H # * )
*
; H
H
+
for
while
do statement ⇒ while statement.
else
break
while true
B7
6
⇒ while
B
!
A
6
while
*
)*
while
do
do
H
do
if
if
do
while
#
for
while statement ⇒ for statement.
for
break
b
b
€
$
$
5
69
5
7
6
8
4
9
7
67
8
8
7
5
45
79
67
0
,
for ( unsigned int i = 0; i < 100; ++ i ) {
if ( i % 2 == 0) continue ;
std :: cout < < i < < "\ n";
}
continue
if ( true )
if ( false )
if
Where do I belong?
else
if
Where do I belong?
*
!
*
*
i += 2
!
,&
1 ! !
+
*
*
*
B * -
IH !
! H
D
- E
, @ * , 1 1
simpler expressions
else
1
* / ! (
* )
% HI *
, * ! * ? , H ;
* H *
*
1
*
?
*
*
H C
* * *
,
A * ! ,
! H
* *
* * H
*
!
*
1 C , !
H
i
H
if
(
H 7
! '
* '
/
!
*
IH , ! !
* 6 1 1
H
H !
!
*
C , : , !
, H
,
G
?
1
* -
*
C
*
while
Nested if-else statements.
for
for
1 HI !
*
H 1
*
1
*
!
,
*
*
B -
,
!
,
) !
* !
H *
* A
2
* *
+
*
*
* !
*
*
H 1
!
,
!
1
*
-
H
H
+
H +
* ments
+
1
* 6
*
* * /
< !
, 1
7 *
1 1
! 1 * 1 H
G
H
*
!
!
, 1 * #
A
<" ! * *
!
* while
continue
)
!
for
break
1
2
100
%
2.4.8 Choosing the “right” iteration statements
1 1 * ,
* * 1
, !
H B
,
H HI / do
do
-
1
1
!
!
*
,
*
* )
HI
A
while
, H
!
!
A 1
H *
< 1 H
H *
0 !
A
, )
, @
)
*
(
&
1
for
break
*
H !
b
fewer lines of code
fewer state-
for ( unsigned int i = 0; i < 100; ++ i)
if ( i % 2 != 0) std :: cout < < i < < "\n" ;
simpler control flow
if
for ( unsigned int i = 1; i < 100; i += 2)
std :: cout < < i < < "\ n";
int i = -1;
while (( i += 2) < 100)
std :: cout < < i < < "\ n";
−1
unsigned int i = 1;
while ( i < 100) {
std :: cout < < i < < "\ n";
i += 2;
}
2.4.9 Details
if ( true ) if ( false ); else std :: cout < < " Where do I belong ? ";
else
if
$
$
5
69
5
7
6
8
4
9
7
67
8
8
7
5
45
79
67
switch ( x ) {
case 0: std :: cout < < "0 ";
case 1: std :: cout < < "1 " ; break ;
int
Point of declaration.
int i = 5;
i
1
, $$
7
%
* H
* %
* ? * * * % %
H *
6
$
= *
!
*
?
!
,
, 1
IH )*
!
A
1
1
!
7
7
6
! D *
* * *
& 1 1
E H , ,
A , * 1 IH @
! !
6
'
* '
* * !
1 ,
-
1
* HI
1
*
!
1
,
-
* *
)
&
A *
A
. 1
" !
D
H
E ;
* B %
-
6
$
?€
$
? 7
;
)*
?
'
'
, 6
7
*
-
D
E
!
1
H
!
* 6 1
7 !
*
, +
!
6 A
H
7 1
1
!
!
!
, 1
H H
- !
%
C !
1
! * !
, 1
,
H
A
*
* 1
* <A
)
A
1
-
H *
A
*
*
,
F
B
!
! B
/ , 1
* *
! * !
,
!
!
*
*
else
&
!
D
< ''
!
) +
!
* 1
, IH if
*
#
! * H 2 A * * H
-
*
H
- * * **
, * !
' ' ?
B
switch
%
E
*
-
switch
*
C
, -
D, ! *
* * !
* 1
* *
1 ? * -
? The Halting Problem, Decidability, and Computability.
1
*
<
-
* , !
1
*
,
1
1 * ,
1 *
x==1
&
* 1
H E
% ! H 1
1 , +
*
? ,
1
1
01
!
*
break
!
!
1 1 F
* !
1
1
,
+! , ! - ,
* ! *
default:
! D* *
*
1 1 - * !
E C 6 , H !
' '
C
* &
7 * *
*
1
H
H default:
default:
switch
*
switch
x==0
switch
break
!
!
!
1 +! , H )*
1 *
1 )* * !
bool
* &
H ,
!
*
! 1
* !
! , * x
else
switch
case
if
switch
H
)
*
*
*
!
The switch statement.
switch
)
, HI )*
*
6 1
+
!
! /
! if ( true ) {
if ( false ) {
; // null statement
}
else {
std :: cout < < " Where do I belong ?";
}
}
, 7
1
! !
)
!
*
!
*
H ; // null statement
else
std :: cout < < " Where do I belong ?";
}
default : std :: cout < < " whatever ";
whatever
break
switch
λ
"
$
$
5
69
5
7
6
8
4
9
7
67
8
8
7
5
45
79
67
A
* !
* H *
B H %
6
7
2
H 1 !
* *
!
*
1
6 2
* H 7 6 *
€
< 7 *
*
* !
! * H
)*
A
G * (
!
'
A
'
break
?
, 2
-
? !
-
& +
*
H ,
D E 1
! * 1
H IH C
*
!
,
1
1
* / 0
$6
, =
$
$
7
, * * 2 $
A
(
,
1 3
*
!
*
)*
B
'
'
0, 2, 4, 6, 8
( F
,
* , 1
1
1 C
!
, !
, *
H 1 !
,
, C H
! 1
&
H , !
! )
1
* * &
H !
*
*&
? &
*1
*
A
C
-
H
H A
3
7
1
6
, !
*
1 ,
* *
1
* 1
!
< * *
H
F
+
!
! H !
, * 1 * & ! H C
H & !
- , 1 * ! * IH
!
C * $
!
* H
!
H
!
!
* k
!
H
H=
do
H=
while
*
H=
for
!
!
, !
*
* 1
E
+
! H *
2
*
C * , * , static
continue
1
* else
&
!
*
)
$
H A *
H A * )* !
< *
* k
*
7
!
* / * $ 6
=
$
! $
, " 7
/
*
H
1 * 7
*
1
:D 6
* int x;
C
*
A
if
{ F
,
1
* )
-
H 1
A
?
1
?
*
+
static
* *
Dispositional.
€
<
)
Jump statements.
)
B
0, 1, 3, 6, 10
, !
*
!
!
= = H
H=
B B for ( ; n > 1; std :: cout < < ( n % 2 == 0 ? n= n /2 : n =3* n +1) < < " " );
?
7
B * *
1
* H A
* H
H
* , bool
n
7
H 7
500
*
1
1
#
!
H *
! '
H '
A
!
,
*
*
* 1 ) * - if
*
i
5
7
*
! 6
H while
C
=
*
-
!
* H
!
1
*
-
*
1
,
, * <
1
&
,
1
* !
*
! * !
! H 1
H 1 ! * !
-
)
Static variables.
7
*
+! )
H ) !
- , ,
H * *
1
*
, ! ,
F H H
,
1
,
!
*
)
n
!
H
,
, * / ! * A
1 ! C
*
6
* (
€
€
H 7
int i = 5;
{
int i = i;
}
A
H =
"7
A
!
*
2
*
C
,
* The Collatz problem and the ?-operator.
,
1
!
for
,
i
i
for ( int i = 0; i < 5; ++ i ) {
static int k = i ;
k += i;
std :: cout < < k < < "\ n";
}
i
x
return
goto
2.4.10 Goals
$
$
5
69
5
7
6
8
7
4
9
67
8
8
7
5
45
79
67
}
{ Exercise 37
20
3
7
76
3 7
6
3
"
x s
!
#
<
# !"
"
" 0
#
#
#
!
#
# 1 2
Exercise 36
# include < iostraem >
int main ()
{
unsinged int x = +1;
{ std :: cin > > x ; }
for ( int y = 0 u ; y < x ) {
std : cout < < ++ y;
return 0;
}
! !
#
(- ? (- ( - € (- 2 4
!
"
!
6 #
€3 7
6 3 7 76
€7
3
3
6
"
{|
! 6
! 1 !
"
7
!
*
!
* !
* * , A
!
3
7
6
, * A
!
*
! 1 ! 6 *
C
B 7 6
3 7
!
, B H=
H=
!
, C
6
"3 7
!
!
B
&
, *
, * B
* 6
3 7
&
*
7
6
* , 3
6
B
, 7
&
6
3 7
&
B
)*
)
, 3
6
€7
} { | } { *
*
H
*
4
2.4.11 Exercises
20
H !
B
1
3
3
€
A
7
6
1
1
# / $ 2 #
4 0
"
!
"
! "
#
! " !
H
* +
7 76 6 "
*
!
* *
*
&
1
, * 6
*7
3
Exercise 35
6
3 0
€7 6 3 7 !
/ /
0
Exercise 33
! <
(- # ( $ - # / 0" (
! !
"
do
6
unsigned int s = 0;
do {
int i = 1;
if ( i % 2 == 1) s *= i;
} while (++ i < 10);
?
(- "
!
while
0
!
4
} { for
{ | } { 2
!
Exercise 34
"
Operational.
# include < iostream >
int main ()
{
int s = 0;
{
int i = 0;
while ( i < 4)
{
++ i;
int f = i + 1;
s += f;
int s = 3;
i += s;
}
unsigned int t = 2;
std :: cout < < s + t < < "\n ";
}
int k = 1;
return 0;
}
i
# include < iostream >
int main ()
{
int x ;
std :: cin > > x ;
int s = 0;
for ( int i = 0; i < x ; ++ i ) {
s += i;
x += s / 2;
}
std :: cout < < s < < "\ n";
return 0;
}
$$
5
69
5
7
6
8
4
9
7
67
8
8
7
5
45
79
67
x
%
#
dec2bin2.C
0 %
0
n
8
!
#
[1, 50000]
n==2
6 3 !
7 "
6
3 !
7 !
#
!
n
n==10
3 $
7
6
2
n!
<
1
n==2
2
1011
0
$
/ # "
! #
2
1101
#
0 0
0 $ !
$ n
!
* # cross_sum.C
n
n = k , s.t. k<n ∧ k|n k
12 < 1 + 2 + 3 + 4 + 6
P
perfect.C
n==112
Exercise 43
"
!
4
2
"
!
4
dec2bin.C
$
" ! #
( /5 2
/
2 - ! #
# n
4
# #
4
( !
!"
- 2
" #
2
/ !
! # ! 6
3 !
7 fak-1.C
<
# $ %
" !
"
#
2 !
!
!
n
- <
!$ 0 "
# "
) 2
# / 5 !
#
0 / ! "
" !
# 2 0
!
Exercise 44
Exercise 46
n==11
" 4
# !
! "
( 2
/5
/ !
!
2 - # ! 6 ! 3
7
Exercise 45
%
# $
n==11
$! (
1
#
Exercise 42
/
1
4 (
! 2 ! $ 0
$ %
#
" %
)
0 2
<
. ,
<
2
%
4
<
0 #
!
6 € 3 7 6 "3 7 6 3 7 1 | } { Exercise 41
n
0 $ <
2
!
n
!
unsigned int n;
std :: cin > > n;
int x = 1;
if ( n > 0) {
int k = 0;
bool e = true ;
do {
if (++ k == n ) e = false ;
x *= 2;
} while ( e );
}
std :: cout < < x;
!
$ #
!
" ! 0
# $
# $ Exercise 39
0
"
!
2
# include < iostream >
int main ()
{
unsigned int x;
std :: cin > > x ;
int s = 0;
for ( unsigned int y = 1 + x ; y > 0; y -= x )
s += y;
std :: cout < < "s= " < < s < < " \n";
return 0;
}
#
Exercise 40
{ | } { #
"
| } { { | } { ! 2
6 3 7 0
6 0
3 7 6 1
3
7
!
"
$
! !
!
! " !
0$ 0
:
Exercise 38
# ! 2 1
6
3 6 $ 3"
76 3 7 $
2
!
"
!
#
€7
# include < iostream >
int main ()
{
{ unsigned int x ; }
std :: cin < < x ;
unsigned int y = x;
for ( unsigned int s = 0; y >= 0; - - y)
s += y;
std :: cout < < "s= " < < s < < " \n";
return 0;
}
# include < iostream >
int main ()
{
int x ;
std :: cin > > x ;
int s = 0;
int i = -10;
do
for ( int j = 1;;)
if ( j ++ < i ) s += j - 1; else break ;
while (++ i <= x );
std :: cout < < s < < "\ n";
return 0;
}
01
n
4
1
n
28 = 1 + 2 + 4 + 7 + 14
n
10
€
5
69
5
7
6
8
4
9
7
67
8
8
7
5
45
79
67
!
βi
i=−3
βi 10i ,
+
H
1
H A
!
fahrenheit.C
82.4
2
!
!
" 0 $
!
" " .
/
!
!
#
3
7
6
2
,
&
8
)( *+ " -
= 5/16
$
0 ! "
" ! $ 55
5
2 4 0 ! 1
float
-
H
28
1 float
double
fahrenheit.C
float celsius
28
int
unsigned int float
F
" 2 "
0
3
! #
# " !
! " 0 "
" ! $
2
1
π
< H
* ( *
€ € ,
A A F ) F
10
7
* D*
/
E
! * !
*
1 A F
H , 1
1
2
2=
!
" int celsius
!
, 2 )
2 )
7
8
!
fahrenheit.C
!
,
'
'
82.4
F IH =
H 7
!
*
* !
$
2 2
π
H 6
*
"
"
!
4
# ! 2
1
0
#
! 9
# 3
(
! $ $ # 2 $ 4
- !
"
mersenne.C
!
+
!
A
1
* 1
A
82.4
* * !
+
!
*
H *
G
1
,
!
, , * 1
, ! 1
!
<
D
E
82
/
i
Fixed versus floating point.
{0, . . . , 9}
6
X
1
*
H
*
1
n
) &
!
#
+
! 0
3
!
2 $ 2.4.12 Challenges
famous_last_digits.C
" 243,112,609 − 1.
! * ,
1
* 1
!
* * 1 ! H H
!
#
n
!
9
.
?C
n
!
9
$
0
n
#
3
0
" ! " #
0
ifm::integer
" Exercise 49
.
n
n
)
< !
3 2 2
" " 0
$ 0 !
"
$ 0
! 3
" !
!
2 Exercise 48
)
/5
267 − 1
" 0 2 # 0 " # "
. 0
4 " $ ! " 0
$ 3 $ 3
0 Hint:
!
" 0
# $
" " 0 # " <
# #
# !
#
1 # # 2 4
3
# 0
#
)
2 )
3
3
$
Exercise 47
* 2
#
#
(
"
!
4
7
6
#
#
3
2 -
2
. !
!
#
#
% !
# dice.C
p/q
http://projecteuler.net/
2.5 Floating point numbers
761838257287 193707721
double
n=9
double
82.4
5
69
5
7
6
8
4
9
7
67
8
8
7
5
45
79
67
%
float
+
*
!
H , H
! H *
* &
, * - ,
G IH
H
1
H
1 ( H
* 6 *
* 7 1
) !
, D
* !
E * , ! 1 )
float
)
1
'
'
H
unsigned int
double
float
double
2.5.1 The types float and double
// Program : euler .C
// Approximate Euler ’s constant e.
# include < iostream >
int main ()
{
// values for term i , initialized for i = 0
float t = 1.0 f ;
// 1/ i!
float e = 1.0 f ;
// i - th approximation of e
std :: cout < < " Approximating the Euler constant ...\ n ";
// steps 1 ,... , n
for ( unsigned int i = 1; i < 10; ++ i ) {
E
e
5
!
*
H 1 ,
#
!
6
7 - !
! H 7
/
1
, * < ,
*
+
H
!
1
- 7
,
)
0
%
&
)
7
H
!
! F ) H A
, *
! * H 0
!
, ! * H * H * , ! ,
!
> *
! > !
!
6 * double
1.23e-7
D*
10e
123
+
123e-9
1.23
int
1 -
HI
*
0
9
1.0
1
;
,
1
1
!
! * &
* 6
H 7
1
.
! 6 ! !
H 7
H - ) * !
! *
6 !
! 1 6
!
" !
H
A * !
H ! !
7 6 6 1
7
C
7 *
!
E
float
*
!
H C
H
* ! ! H 1
*
, A
1 ! 1 H
* 1
" % 1
Literals of type float and double.
int unsigned int
1.23 10−7
double
f
F
double
)
!
!
1
1.
1
*
* H - , H ! ,
*
1 !
1 * * ! * ,
&
1
0.1
G
-
0
) 6
!
& *
H ! 1
) H
6
7 7
* *
D C
*
* !
* E -* 6
* *
1 " H 7
!
!
e
H
1
!
?
G
82.4 = 824 10−1 ,
0.0824 = 824 10−4 .
.
@
!
*
* *
,
C
*
A
H & HI
)
?
,
!
" !
/
7
H
F
)
82.4
0.0824
)*
G
1
3
* H
H - ?
unsigned int
double
%=
double
int
float
, double
,
*
,
!
int
82.4
{ | } { ,
. * ! !
G
6 H !
1
7 * " ) !
H *
1 double
)* H *
, , 1 ! *
*
float
* A
H *
, ,
float
10
, ,
double
A
0
) &
,
H
, 1
, !
H -
! ! IH
G
* 1
' '
$
!
H *
H , ,
,
, * (
*
€
€
6 int
float
double
1.23e-7
.1
E
double
e
∞
X
1
= 2.71828 . . .
i!
i=0
"
5
69
5
7
6
8
4
9
5
7
8
6
67
6
5
7
t /= i
H
H
+
*
A
H * , )* , A , * ! ,
,
<
*
,
)
)
H
float
(unsigned int)(1.6f)
!
float
T x =
H
−1
6
H
H
H
A
* ( € € *
- ,
! ) !
* A
+
2
!
!
, * !
! +
* H
,
<
float
*
!
* H *
* (
€
H
*
, *
,
€
)
t
) ,
,
,
&
! 1
! ! !
-
H *
* * ! float
double
double
−1.6
*
1
i
x
A
"
!
double
G
, 1
, *
,
! H !
*
, * +!
float
G
)
H
,
*
,
unsigned int
float
x
−2
float
*
,
&
+
1 , ,
7 )
H double
float
,
6
1
A
,
+
?
* H H 7
* , & ,
float
,
#
F ,
) 1
* , ! G
, , 1
*
*
* double
int
int(-1.6f)
,
!
H
? A
*
double
*
)
,
!
1
H
-
H * A
!
*
,
,
int
)
7
) 1
* * H
,
!
* * ,
, ,
! ! & ) * , 1 * A bool
- )
) ! )
* *
, H
*
/ !
H
int i = -1.6f;
H 7
6
1/i!
float
float
unsigned int
2.5.2 Mixed expressions, conversions, and promotions
e
Approximating the Euler constant ...
Value after term 1: 2
Value after term 2: 2.5
Value after term 3: 2.66667
Value after term 4: 2.70833
Value after term 5: 2.71667
Value after term 6: 2.71806
Value after term 7: 2.71825
Value after term 8: 2.71828
Value after term 9: 2.71828
7
H
}
return 0;
* e += t /= i
float
H 7
) !
*
+
{|
}
* * 1
)
! )
? *
)
, 6 - 1
}
F *
) * D* *
,
* E !
7 H
7
*
6
7
7 H
&
A *
int celsius
*
,
H 7 H
1/(i − 1)!
)
i
,
!
)
?
1/i!
!
&
, ,
1
*6
) !
unsigned int
!
G , +
1 * H H !
, , - -
HI G
)
* 1
) HI &
1
* , * , e += (t /= i)
<
,
1
1
! G
)
) 1
, 6
* (
€
€
7 - &
,
*
int
,
! ,
,
Program 11:
,
i
* euler.C
unsigned int
fahrenheit.C
9 * celsius / 5 + 32
*
- +!
* )
, *
IH
!
?
&
* *
t
e += t /= i ;
// compact form of t = t / i ; e = e + t
std :: cout < < " Value after term " < < i < < " : " < < e < < "\n ";
double
int i = -1.6 f;
i
float
2.5.3 Explicit conversions
unsigned int
float celsius
5
69
5
7
6
8
7
4
9
5
8
6
67
6
5
7
HI
d0 = 0
0
0.1
-
H
!
H
c
,...,e
" * *
C
i
e {e
d0 d1 . . . dp−1
β = 10
!
1
H
" ! !
!
1
1
*
!
1
6
!
H
/
" 7
%
&
H 7 1
!
H
6 &
1
!
/ ! 1
7 6 " !
7 $
6 ! e
, A A ! %
!
* 1
*
A * *
A
G * * 1 1 *
*H - *
* H A
*
;
*
*
*
1
* <
!
1
)
1
H
{0, . . . , β − 1}
)
p
!
,e
1
1
* 1
/
!
G {−1, 1} di
s
e
0.1 100 0.01 101
1
!
1 H
1* 1
C *
1
* , H
)
,
!
H
First number
=?
Second number
=?
Their difference =?
Computed difference
βe
H
-
,
!
,
,
+
H * H , , 1
? * G ! 1
!
!
< + ! 1
, 1
1
*
,
7
F " G 1
!
H
1 HI D
E
1
< &
6
H
F
G
- *
* 1 , !
*
! * * *
,
?
First number
=?
Second number
=?
Their difference =?
Computed difference
H
G
(β, p, e
,e
(β, p, e
i=0
p−1
X
s
=? ";
- s
β
e
2
// Computation and output
std :: cout < < " Computed difference - input difference = "
< < n1 - n2 - d < < " .\ n" ;
return 0;
-
=? ";
float d ;
std :: cout < < " Their difference =? ";
std :: cin > > d;
F
)
# include < iostream >
// Program : diff . C
// Check subtraction of two floating point numbers
*
!
)
1 0 H std :: numeric_limits < double >:: max ()
}
1.79769
, 1 !
7 : 6 * H 1
) * 6
7 * )* 1
H
1
1 A
! 1 float n2 ;
std :: cout < < " Second number
std :: cin > > n2 ;
G
*
*
/ 1
* /
€ *
! A *
* A
1.79769e+308
<
} { | } { int main ()
{
// Input
float n1 ;
std :: cout < < " First number
std :: cin > > n1 ;
2
Program 12:
{|
10308
"
{ {{ {
{
double
!
}{
!
1
*
1
* *
)
2.5.4 Value range
1.5
1
1.5
1.0
0.5
- input difference = 0.
1.1
1.0
0.1
- input difference = 2.23517e -08.
1.1
2.5.5 Floating point number systems
di β−i βe ,
}
d0 .d1 . . . dp−1 βe .
1.0 10−1
$
5
69
5
7
6
8
4
9
5
7
8
6
67
6
5
7
i=0
xi = (xn+1 − 1)/(x − 1)
65, 535
1.1
)
77.1
850
100, 000
(2, p, e
,e
H
)
1
*
=:x
!
0
1 X
bi−1 2i .
2 i=−∞
| {z }
bi i
x = 2(x − b0 )
H
0
,
-
bi−1 2i−1 = b0 +
,e
1
- 1
! (2, p, e
x = 1.1
p, e
e
b0
b−1
b−2
b−3
b−4
b−5
b−6
→
→
→
→
→
→
→
..
.
1.1
0.2
0.4
0.8
1.6
1.2
0.4
1
i=−∞
0
X
*
bi 2 i = b 0 +
x
−1
+!
)
F H !
1
*
, 1
)
!
<
C
*
1
, 1
* * 1
H ,
1 H
) &
)
F
*
N
2k
!
# H
1 C
! 1 ) * &
* 1 ! H
C *
* ! C x = 1.1
b0
k
-
! * ? , ? 1 1
! * ,
*
! C
A *
* !
! =
=
=
=
=
=
1
c
i=−∞
−1
X
1
double
0.1
0.2
0.4
0.8
0.6
0.2
2
2
2
2
2
2
1.1
1 G 1 1
)
1 H !
& * 1
* & < * ( * *
?
1
* =
=
=
=
=
=
,
*
* ,
1 6 A ,
7 1 G ? 1
p
, 1 * &
H H G
H
2(1.1 − 1)
2(0.2 − 0)
2(0.4 − 0)
2(0.8 − 0)
2(1.6 − 1)
2(1.2 − 1)
A
HI
1
* c
y
float
bi 2 i = b 0 +
x = y + 2k
* :
#
1
H 1
, , i=−∞
0
X
x
*
x
i.
H
H * ! F
)
; H
* * ,
*
H
,
+
x =
,
6 * * * * !
7
1 x<2
1
!
)
βe+1 < βe+1 .
e
0.1
i
- H
1
, *
,
1 B 1
1
G
? D !
E x
G
% 1 *
?
*
1
)
*
*
, D
!
* E
*
, A
)
/5
d0 = 0
e
x
"
1.1
bi
i−i+1
x=1
s
2
)
i−i< p
p
0.1
x
)
x
i
,e
i i
7
H
- H
1 !
* 1.25
(2, p, e
x
&
!
1.1
i
, !
β=2
1
β
di 2−i 2i .
)
, 1 :
G +
1 1 H 1
* D
* * 1
1 )
H $ E
? (β − 1)β−i βe = 1 −
p
!
1
H 1 HI ) ) B
*
C
H 1 , ! * ,
€ G
e
)* x>0
* H
1 !
* H ,e
1
-
/ * 1
)
, !
, 1
*
! (
* *
* €
€
1 & * 1
$
/ ! 1
G
1
Computing the floating point representation.
x
i=0
i−i
X
!
-
i=0
p−1
X
#
(2, p, e
1.25 = 1 2−2 + 1 20 .
*
(β − 1).(β − 1) . . . (β − 1) βe =
bi−i 2i−i =
-
{0, 1}
)
−∞
di := bi−i
i=0
i−i
X
bi 2 i =
1
1
bi
i
e
6
1 ! H " 1
H
1
" &
! *
0
)
*
x
, ! H ! 1 )
!
!
!
1 * & IH C
bi 2 i ,
Pn
+
*
i=i
i
X
)
i=−∞
∞
X
x=
x=
1.0 . . . 0 β = β ,
e
y < 2
x<2
=1
=0
=0
=0
=1
=1
=0
1.00011
)
{∞}
x = 0.1
The Excel 2007 bug.
0.1
€
5
69
5
7
6
8
4
9
5
7
8
6
67
6
5
7
!
!
-
Arithmetic operations.
x
^
H
1 * 1
!
* G
! 1
) *!
G
2−p
*
*
HI
H A
1
*
*
?
A
0
, 1
1
* ) 1 1 , G 1
1 1
; F
! * , H
* * H , , !
! * ! +
! !
* ,
H * &
2
* , 1
,
,
1
! H
3
4
5
x
^
H
6
€
1
)*
!6
*
!
* -
7
1
G
H
!
)
H
x
- , 1 !
H
C
!
, )
*
1
G
?
)*
1 D &
E 1
1 * *
, G ? 1
1 ,
1
*
1 ,
)* < A * 1
G
H
1
!
2−p
H
*
)
0
% H
!
H
* * ! 1
- x
1 , ! 7 1
- * C * !
* 1
G
1 1
* ! 6 ,
€ D ,
IH *
E * * 1 :
! * ! G H 7 ?
1
**
! 1 * H 1
1
* / )
* 1 1 !
1 ,
H H H
)
1
1
)
- H
*
* *
, H
H A
<
*
*
1
!
!
x x
2i−p+1
H H !
H
- ,
1 1 H ! * :
$ !
! *
; * 1
* * H 1
H
1
* , , x
* 1
0
F (2, 3, −2, 2)
1.11 22 = 7
[1/4, 7]
* 6
G !
H
1 G
1
x
7
}
1
,...,e
x = x + 2i−p+1
{e
1
1
G
0
!
< H 1
*
H
1 * H H
, ! 2i = 2i−p+1 .
1.00 2−2 = 1/4
A
x
2−p ,
2i−p
H
i=−∞
|x − x
^|
|x|
#
bi 2 i .
x
^
x
G *
1 ,
!
1
B
!
6 * * 1
1
C * !
* : |x − x
^|/x
G
1
bi−p+1 = 0
x
2i−p+1
)
#
x
x
^
p−1
p
+
!
1
1
)*
C
, * * A
* /
&
! )
H 7
! , !
1
*
1
*
65, 535
H
x
p
,e
i−p
i=i−p+1
i
X
bi , i
G ( 1
1 , 1 ) H
A )
) 1
77.1
bi 2i = bi .bi−1 . . . 2i ,
-
H !
)
1 77.1
$
*
x = bi .bi−1 . . . bi−p+1 2i =
, H *
1
bi 2 i
i−p
X
(2, p, e
x
850
65, 535
1
2i−p+1
2i−p+1
i=−∞
%
0.1
#
x−x=
i−p
X
i=−∞
i
X
x=
bi−p+1 = 1
Relative error.
1.1
65, 535
65, 535
65, 535
x
x
2i
2−p ,
x
^
x
x = 0.
x
bi = 1
8
x
x
x
5
69
5
7
6
8
4
9
5
7
8
6
67
6
5
7
, 1
*
,
, G
1
*
*
!
,
H !
G
* H , + 1
) H * H ) 1
* *
€ :
€ 7 ,
H
*
, !
, 1
)*
6
(
?*
!
* ,
A
*
,
G 1
!
* H
'
1 + *
1
?*
'
-
* &
1
HI 2
A
- ,
*
*
H
* C
H , ,
* & 6 * * 1 < 1
, , c - ! !
1 , H
1
*
H 7 +! ?
1
*
0
* * 1 1
!
1
, 1 H
* ) ,
!
*
!
1
F
, ! 1
1
*
1
* IH
1
1 IH
,
)
H
I
* &
, C
1 *
€
1
1
H **
!
)
+
H -
&
D " E
! 1
1 *
HI * , <
)
! H
*
)
* 1 , 1
HI )
1 C
!
1
1
!
1
H
euler.C
1
G
1
double
* *
*
IH *
, H
6 7 , ? 1
1
H G , 3 IH !
1 !
1 , * float
,
*
H *
F
)
+
dp−1 = 0
Floating Point Arithmetic Guideline 1:
!
& D
!
*E
G
1
H
F Requirements for the arithmetic operations.
H !
!
!
)*
G
*
, +
< * )
*
H 1
1 1 1
* &
1 IH 1.1
double
F (2, 53, −1022, 1023)
double
C
'
'
*
1
! , * , !
,
1
,
*
,
float
float
-
2.5.6 The IEEE standard 754
+
1.001 20 .
0
*
!
p=4
float
A - )
! !
* *
* ,
IH
! HI *
, 1
1.00101 20 .
F (2, 24, −126, 127)
+
100.101 2 .
! *
* * H !
G , 6
1 !7
5
5
5
H !
, ?
−2
! * G
1 7 - , 1
IH
! *
1
*6
?
!
"
!
1.111 2−2
+ 10.110 2−2 .
0
float
HI
*
1
,
H 1.111 2−2
+ 1.011 2−1 .
* &
&
!
H
1
G
1
H
*
- *
* & +
C H 1
€ 1 !
B * * * 1
!
1 H
1
G
* *
+
&
H 1
, 1
1
-
( * *
, ?
1 * 0
B ! )
32
#
1
!
H *
!
G
Value range.
double
double
p=4
32
254 = 28 − 2
8
2046 = 211 − 2
64
2.5.7 Computing with floating point numbers
1.1
"
5
69
5
7
6
8
4
9
5
7
8
6
67
6
5
7
Hn =
i=1
i
.
Compute H_n for n =? 100000000
"
1 / i
*
*
A
)* ! , 2
<
"
!
* &
1
G
*
H
1
&
- ) )*
, 1
HI
G , 1
1
1
*
H *
C
H *
)
*
HI 1 * HI !
1
* ,
)
*
6
7
,
H !
:
)
) -
IH
)*
H ,
?
G
H
*
1
)*
*
F
*
H
* &
H 1 * 7 ! * 1 6 , 1
{ }{ { { {{ { {| } { | } !
{
*
G
HI *
! H )
* * )
*
, H H
, 1
, 3
!
!
*
*
! )
A G ( * *
C ! C
, !
H A 1 , *
*
H - A
/
*
1 H
* 0
H
HI
, !
* H C
, H 1
/
, HI
*
C
, * @
* B * A ! 1
H
n
,
* 1
Program 13:
*
/
* &
-
n
n
2p + 2
/
2p
p+1
)
bi 2 i ,
A
!
p
(
*
1
(
: * H 1
Adding numbers of different sizes.
2p
1
Hn
p
-
βp
€
{
p
(bp , bp−1 , . . . , b0) = (1, 0, . . . , 0, 1)
2p + 1
2p
* / * 6 1
! *
* +
H
H 7 HI !
==
-
-
E * & ! G
1 6
) 1
H !
/
7 DA
i=0
p
X
2
* - , * /
*
H * & *
*/
H *
1
!=
(x + y) * (x - y)
n
X
1
n
& * !
& * * * * 1
! 6
! *
* * * 1
1
!
! 7 F !
&
H ? 1 8 !
IH 2p + 1 =
IH x * x - y * y
n
1
1
1
!
!
69
7
6
5
5
*
8
7
4
9
5
8
6
67
6
5
7
)
*
+
* , ,
)
,
, G
1
Hn
1
n
harmonic.C
// Program : harmonic .C
// Compute the n - th harmonic number in two ways .
# include < iostream >
int main ()
{
// Input
std :: cout < < " Compute H_n for n =? ";
unsigned int n;
std :: cin > > n;
// Forward sum
float fs = 0;
for ( unsigned int i = 1; i <= n ; ++ i)
fs += 1.0 f / i;
// Backward sum
float bs = 0;
for ( unsigned int i = n ; i >= 1; - - i)
bs += 1.0 f / i;
// Output
std :: cout < < " Forward sum = " < < fs < < "\n"
< < " Backward sum = " < < bs < < "\n" ;
return 0;
}
Compute H_n for n =? 10000000
Forward sum = 15.4037
Backward sum = 16.686
2p−1 − 1, c = 2p−1 + 1
1
b = 2p , a =
4
0
,
* +
6 H 7 &
1 / G
! 1
!
!
,%
! 1
G *
* 1
!
C
1
, +
* *
*
0 ! H
!
1
*
*
-
H
%
*
* H , ) * C ,
, * C
* &
) H 1
H
1
* 1 * *
C * *
*
:
H *
?
!
*
*
* &
,
*
* & <
!
* * *
*
*
! H +
!
, *
H IH 1 * * &
& C
1
?@!
H !
!
1 * -
*
! )
H
6 ? ! 7 1 G
* H 1
- 1
, 1
! * 6
H 7
1
*
* * H
*
!
*
)
1
7
6
!
C
1
H
1
1
G
* & 1
!
*
, 1
float
float b = 16777216.0f ;
float a = 8388607.0f ;
float c = 8388609.0f ;
!
*
10
0.1
H "
1 /
Other floating point number systems.
A
* H 1
? +
H
Floating Point Arithmetic Guideline 3:
1
1.1
G !
IH
1 !
+ *
H a = 0.
*
1
1
- HI ! * c
2p − 2
;
a, b
b2 = 22p
!
+ 6
H 7
* !
A 1 1 /
1
HI
H ! , 1 * !
< * -
1
-
IH
! 1
*
A
1 , ,
1 *
- 1
)
< %
!
1
* ! 1
* 6 , !7
% &
H / ,
* 1 !
1
*
4
* IEEE compliance.
*
d
)
*
d
*
A H
,
! !
*
!
+
1 ^
d
d
*
1
2p
4ac
4ac
2−p |d|
*
*
* * *
) , H ?
? F
)
H b2 − 4ac
.
2a
p
1
,%
H 2
A
18.998
Hn
d
Hn
, 1
,
2
1
1
,
n−γ<
2n
-
b2
7
)*
, b2
Floating Point Arithmetic Guideline 2:
^
d
H
H 1
*
A
&
!
H *
+
=
)*
ax + bx + c = 0,
,
!
*6
, 7
H 2p + 1
C
* C
/
- * *
* 1
G * * & ! !
H * *6 ,
, 7 γ = 0.57721666 . . .
n = 108
1
−b
2
A
+
1
< Hn −
2(n + 1)
+
Cancellation.
d = b2 − 4ac
! * *
H , + ! H !
1 *
G ,
! 1
1
i
6
^
|d − d|
- &
IH 1
1
* 1 ! ! 1 ,
1 <
* ! 1 IH,
G !
*
r1,2 =
HI
* * *
%
1
Forward sum = 15.4037
Backward sum = 18.8079
4ac = 22p − 4
^=0
d
4
2−p
b2 − 4ac
2.5.8 Details
p = 64
p = 32
β = 10
double
// 2^24
// 2^23 - 1
// 2^23 + 1
std :: cout < < b * b - 4.0 f * a * c < < "\n ";
€
$
5
69
5
7
6
8
4
9
5
7
8
6
67
6
5
7
float
float
?
double
, 1
* double
H
* 1
1
, !
!
1
1
* *
H * 1 G
B *
) *
1
, 1
* ,
+
A
, ! , , ,
H 7 6
1 * 1
!
1
H ! , , C
, ,
@
! 0
, 1 * A *
*
* /
!
7
!6
)
! !
*
! * * * A + H *
1
!
β
p
e
e
H *
!
- *
& * 1
A
1
* , , 1
H std::numeric_limits<float>::min()
1
float
!
B
*
-
+
%
*
H 1 * 1
1
* 1 1
1
,
,
?
*
- 1
!
float
double
B
float
H +∞
!
, *
H
! 6 D
1 E 7 1
,
G
1 ,
d0 .d1 . . . dp−1 βe
3
Dispositional.
%
&
H A
A
C
!
1 H 1
)
*
!
Special numbers.
%
* A
G
B 1
,
1 ,
G
)
1
! HI )*
A +
d0 = 0
, , *
) & *
!
G
1 B 7
!
H
*
*
* /
* H !
*
F H
*
* * * A
, ,
* 1
ac4
)*
, L
€7
A
1
A
G
// 2^24
// 2^23 - 1
// 2^23 + 1
IH * ,
,
, 1
long double
!
float
)*
7
7
A
+
*
1
*
*&
G
1
'
+ *
1
!
H G
? '
-
1
)* ! H
1 , !
* * & @
! + *
!
* !
G 55
5
A ! ? std :: cout < < bb - ac4 < < "\n" ;
7
A
F
+
*
* !
* H !
A * & ! G
1
! A H 1 * *
float bb = b * b;
float ac4 = 4.0 f * a * c;
limits.C
1
? long double
double
* 1
)* long double
F (2, 64, −16382, 16384)
>
1
*
,
*
, & 1 !
long double
!
*
1 #
' ' IH * ,
!
l
!
G
H * long double
double
A
float
+
long double
double
double
long double
! H
1 1 *
! ! !
)
*
* H H 1 !
The type long double.
+!
1 ? ) G
* ! 1
+ H
4ac
H
1
Numeric limits.
float double
numeric_limits
) , 1
!
HI
! (
*
€
€
, float b = 16777216.0f ;
float a = 8388607.0f ;
float c = 8388609.0f ;
int
std::numeric_limits<float>::radix
std::numeric_limits<float>::digits
std::numeric_limits<float>::min_exponent
std::numeric_limits<float>::max_exponent
+1
+1
,
0
−∞
NaN
2.5.9 Goals
€
€
€
5
69
5
7
6
8
4
9
5
7
8
6
67
6
5
7
s
float d = 0.1;
|x2i − s| < 0.001.
xi
s
2
" "
#
#
$
#
(
;
1
!
# !
#
#
( , - 2
8
2
"
"
"
$ 2
" $ " ! 0
# 3
2 " $ 2 "
" "
! ( - (
, 1
β=2
.
!
/
0" #
1
$ #
"
!
double
0.5 1 1234567890
0
!
4
"
! " 0 0 $ # ! !
3
" 3 " $! " $ 2
,
-
(
-
(
%
* 3
B
(
$
" #
"
" ! " 0
$ $ (
" $ / ! !
2 1
G
!
*
6
"3 7
$
β=2
( !
, /
- 1
!
!
0
1
3
# 0
#
$
int
"
!
π
x0 , x1 ‘, x2 , . . .
2
0
4 int
double
1 1 1
+ − +
3 5 7
1 1 2 1 2 3
+
+
+
3 3 5 3 5 7
xn =
!
#
# !
" ! ! !
!
/
.
" # !"
Exercise 61
!
π
= 1−
4
π
= 1+
2
π
n→∞
4
Exercise 60
0
x0 > 0
-
16 * (0.2f + 262144 - 262144.0)
0
4
392593 * 2735.0f - 8192 * 131072 + 1.0
2 + 15.0e7f - 3 / 2.0 * 1.0e8
6 / 4 * 2.0f - 3
1234567890.2
1
F
!
,
G
!
*
3
7
6
2
. ! !
#
!
"
!
4
!
# "
$
float
F (2, 24, −126, 127)
32
4 ! 1 !$ 1 " !
! " ! ( ) 1
1
2
5
Exercise 59
)
(
2
2
" 5 #
5 5 /
/ !
2
2.5.10 Exercises
Exercise 58
x<2
( - !
!
B
1
, G
A
6
3 7
*
H
1
G G
1 1
B
, *
& 6
3 7
!
B
1
4
6
B
H
,
)
,
, * H
6
7
3
+
1
1
H
* 4
!
3
7
!
3
"
0 #
0
!
7
6
-
(
!
"
for ( float i = 0.1; i != 1.0; i += 0.1)
std :: cout < < i < < "\ n";
0 #
0
6
!
, (
!
3
€7
1
*
Exercise 57
$ 2 .
!
" "
0 #
#
7
double
,e
-
!
-
17
Exercise 56
&
2
)
!
$
/
-
!
<
7
int unsigned int float
(
$
(β, p, e
(2, 5, −1, 2)
4
! !
"
#
" d
#
11.1
-
(
#
/5
-
1.3
7
*
float
F (2, 53, −1022, 1023)
!
(2, 5, −1, 2)
2
" $
-
1.52
babylonian.C
)
Exercise 54
(β, p, e , e
Exercise 55
4
" ! " " , 5 5 5
" ( - Exercise 53
!
0
8
Exercise 52
$
int
!
Exercise 51
0.25
<
Exercise 50
5
5
5
Operational.
for ( float i = 0.0 f ; i < 100000000.0 f ; ++ i)
std :: cout < < i < < "\n ";
x 0<
1
float
s
1
s
xn = (xn−1 +
).
2
xn−1
s.
€
€
5
69
5
7
6
8
4
9
5
7
8
6
67
6
5
7
#
2 #
3
#
" ! 0
"
$
"
N
|zn (c)|
2
n
c
2
2
n > 0.
n
|c| > 2
0
|zn (c)|
2
c
N
0
/
1
:
#
!
CGAL::orientation
<
!
0
# ! ! 0 !
0
0 <
:
0
-
(
-
<
0
#
#
#
"
0
.
2 : #
0 "
$
2
"
!
.
# !
" 0
!
#
"
!
4
$ $
"
$
"
!
!
0 0
" 2
.
/
2
! libwindow
#
0
0$
,
(
$
$ # $ "
!
Exercise 64
#
-
2.5.11 Challenges
Hint:
-
!
# / 2 !
2
& 0$ !
!
#
$
e
0
2 0
)
1
!
#
" !
,e
#
3
c
z0 (c) = 0
.
:
"!
/ 5 "
2 ! 2
!
2 (2, p, e
libwindow
!
* !
# #
" 0
" !
p e
c
|zn (c)| 2
n
c
0
x
|zn (c)| > 2
! !/
2 #
! 9
1
z0 (c), z1(c), . . .
# !/ " #
$ 0
2
.
9
"
* x
zn (c) = zn−1 (c)2 + c,
fpsys.C
.
)
!
#
- 0
#
# (
# #
0 0 $ (
! ! # $ / !
!
! #
# /
#
0 2 - ! .
9
,e
!/ #
4 9
! ! / "
! 2 0
(
(2, p, e
0 " 9 #
9 2 2 #
%
( #
! $ "
$
"
c=0
"
0" 0 " 2 0 ' $ $ ! $ $ /! 1 Exercise 63
" # "
$
#
"
$
" $ /! 3
0
n
!
$! ! $
Exercise 62
Hi all,
This should be a very easy question.
When I check if the points (0.14, 0.22), (0.15, 0.21) and (0.19,0.17) are
collinear, using CGAL::orientation, it returns CGAL::LEFT_TURN, which is
false, because those points are in fact collinear.
However, if I do the same with the points (14, 22), (15, 21) and (19, 17) I
get the correct answer: CGAL::COLLINEAR.
"
€
€
5
69
5
7
6
8
4
9
5
7
8
6
67
6
5
7
1000
!
1+1
n
" $
7
6
2
<
"
!
* 12 6 {
{
{ }{ { { { {
{ * 12
!
H 7 7
/ C
*
! 6
*
*
7 *
C
{|
)
F
+
* (
H €
*
A *
, IH
1 C
IH ,
* ! ! 1
1 *
*
1 !
! (
* H
*
, ,
! , 1
* IH
6 !
* IH
*
,
7
+
$ ! ! !
" 0 # # $ $
2
4
! =<
=
2
* * $
1
.
$ # #
1 2
#
$ ! 0 ! $
#
$
0
#
"
!
! " /
! " "
#
2 # include < iostream >
} { | } !
, F H
)
H H H
!
, , !
C ,
* ! , {
=
=<
.
$ !
&
H
A
(
, !
H
1
$ " 3
0
$
" crossed_out
, !
1
! )
1 *
,
1
! % * 6
!
* 7 !
H !
$
" $ $ #
" ! ! 1
$
2 $
"
n−1
n = 1, 000
!
n > 0
,
7 *
F
?)
,
6
A
,
1
%
Program 14:
)
&
!
* !
!
-
+
' ' H * 1
* A H 1 $ 1
*
crossed_out[i]
i
!
%
! 0
/ 1
7
!
n
* !
Definition.
$
n
7
2
C
* *
%
H * ? 7 C
*
+
!
*
? 1 ? A
6 F , H
" 7 *
n
H
*
< 5
*
<
1
0
*
*
2.6.1 Array types
H
H A
n−1
C
2
*
1, 2, . . . , n
H
*
* *
H
! 1 1
n
1
( 1
+ ! A
A < , 1
H
)
1
1 C *
* *
! 1 n
H A
0
H 2
+
!
1 A
1
!
2.6 Arrays and pointers
bool
n
// Program : eratosthenes.C
// Calculate prime numbers in {2 ,... ,999} using
// Eratosthenes ’ sieve .
int main ()
{
// definition and initialization : provides us with
// Booleans crossed_out [0] ,... , crossed_out [999]
bool crossed_out [1000];
for ( unsigned int i = 0; i < 1000; ++ i)
crossed_out[i ] = false ;
// computation and output
std :: cout < < " Prime numbers in {2 ,... ,999}:\ n";
for ( unsigned int i = 2; i < 1000; ++ i)
if (! crossed_out[i ]) {
// i is prime
std :: cout < < i < < " ";
// cross out all proper multiples of i
for ( unsigned int m = 2* i ; m < 1000; m += i)
crossed_out[ m ] = true ;
}
std :: cout < < "\ n";
}
return 0;
€
€
5
69
5
7
6
8
7
4
9
5
9
8
67
6
- -
) 1
*
! * / - !
! ! !
* +
!
)
* ! ! !
H
a
, 1
* 1
+
H H
* H
i
0
i<n
i
[]
n
a
a
s
a[2]
p
i
*
s
*
s
a
p+si
2.6.4 Arrays are not self-describing
int a [5] = {4 ,3 ,5 ,2 ,1}; // array of type int [5]
int b [5];
b = a;
// error : we cannot assign to an array
&
**
<
!
H **
H 6
7
**
, 6 * ( * € B 7 ** **
* *
$ *
* 1 A
, ! * 1
*
&
1
H
+
* / * * & 1 ! * HI *
H
! #
? @? ! 1
* *
* 7
, !
! )
1
!
6
A H
7 , 1
) H !
& $
1
!
A
!
1
!
* (
6
'
'
€ " 1 /
! !
*
7 % !
**
H $ !
*
1
&
!
A
*
7
E
+
H !
D
)
7
HI
1
!
, * *
&
B
*
)
, A
, 1 *
- 7D
7 H E
1
* 1
< C * A 6
H
! -
7
a
A
a[0]
IH
!
*
**
* H )
* A , ! )
,
* HI 1
A
! !
, ,
+
-
)
- , H * ! 7 7
!
!
C
*
i<n
< H
H
7
!
0
i
a[1]
!
*
H +
! * ** * *
**
*
A
1
! - H
* ! ! &
H
, 1 F H
)
1
A
*
*
i
{
a[0]
!
*
!
)
*
s
int a [5] = {4 ,3 ,5 ,2 ,1};
Figure 6:
" $
!
!
# $ " $ " $ !
! 0 7 2 #
$ $
2
5
$
a
**
2.6.3 Random access to elements
-
* * *
1 , H
* > 1
!
!
1 H A ;
, ,
!
A
, n
-
i
F
"
2.6.2 Initializing arrays
!
1 *
! +
! !
) *
6
, 7
*
! &
!
H 7 D
F ** H E
IH n
7
int
)*
'
'
0
!
-
&
!
*
, !
! 6 A ,
!
ti
!
*6
i
H 7
1
*
,
,
* H
!
* (t1 , t2 , . . . , tn)
! ,
$ H ! H * * (
H *
* 1
!
* 1
*
!
!
n
int[5] a
!
! * -
int[5]
?
a
!
a
int a[]
-
*
+! !
! !
*
*
, !
H
, !
1
[n]
[n]
a
%
double
!
bool
[n]
Watch out!
n
a[10000]
a
a[n-1]
int a [] = {4 ,3 ,5 ,2 ,1};
€$
5
69
5
7
6
8
4
9
5
9
7
8
67
6
*
7
*
!
* 12
!
7
*
!
6
!
$
H
,
**
/
HI *
!
A
D E *
%
! H
H A
1
*
2−3
H
!
D
* - ) ! B E
A
<
,
! *
*
H
?
1
*
HI
**
*
D6 * ) E 7 1
*
1
s
)
!
7
A
1
1
1
%
!
7
1
Iteration by pointers.
;
!
! * A
*
H 1
H ! * , *
1
+
*
! * H *
*
! H
H
, !
&
)*
* C
-
s
*
H A
*
* &
p
!
*
1 1 H ! 2 H
*
1 ;
A
* *
H *
)
+
*
*
!
*
! C !
* ! * 1
< *
#
1
*
n
0
A
A * 1
A
=€
! +
,
, !
**
1
%
Iteration by random access.
,
*
2
(n − 1)s
7
%
= € )
! *
C
, #
n−1
,
!
1 7
!
, 1
A
*
)
A
1 ) 1 )
*
+
!
F
H / $
.
a[n-1]
,
* * 12 1 IH *
F *
A
F
2.6.5 Iteration over a container
7
b
7
7 *
-
0
* 12
!
H ! %
6
! A ! *
p + si
!
p + (n − 1)s
> +
$ , !
C7
!
1 * * , H H & $ A A
, ! ! ) % * #
*
, 1 #
*
2
&
! DA 6
E !
)
7
<
A
!
* 1 #
*
; " <
a[2]
!
*
H
,
H *
A
* 1 !
B
* i
1
*
* ' H ' *
,
, *
6
* H
' ' H ,
, !
1
1 7
%
C
*
a[1]
!
1
0
a
0
a[0]
p + 2s
1 A
H , , * A
1
C
*
H A
* !
* ! ! 1
* 1C *
* *
- * !
! H *
!
H *
*
!
!
Figure 7:
/
+
/ H *
! ** 12
* * - D 1
* 2
E C 6
H 7
p+s
,
*
p
H , *
H
*
**
!
*
*
1
€
5
69
5
7
6
8
4
9
5
9
7
8
67
6
a
p, p + s, p + 2s, . . . , p +
p
4−5
s
bool * begin = crossed_out;
// pointer to first element
bool * end = crossed_out + 1000; // past - the - end pointer
// in the loop , pointer p successively points to all elements
for ( bool * p = begin ; p != end ; ++ p)
* p = false ; // * p is the element pointed to by p
2.6.6 Pointer types and functionality
The dereference operator.
!
,
1
(
"
-
!
#
(
# -
!
7
6
, , * 2
1
!
C
7
6
, ,
* 12
IH C
*
1
* 2
!
*
&
, *
-
1
! * !
7
$ A
!
* &
! * A
F
, > 1 *
* ! ,
H A
A
+!
* A
1
H * +
p
* 12
6
7
!
,
1
H )
iptr
int* iptr = 0
F
.
i
!
* H
The null pointer.
H
,
!
*
Figure 9:
1
(
*
double * j = i
int i = 5;
int * iptr = & i ; // iptr initialized with the address of i
i
1
, * 12
1
*
* *
,
1 !
, H
,
- 1
* 1
*
12
D* *
,
*
E +
! * H * *
*
1
HI j
! *
double
,
, *
- , ! !
* ,
,
,
*
+
1
!
, ! , - !
) ! 12
*
+!
, * !
H
int*
*
,
* int
int
!
+
* 12
H 1
1
A
HI 7
B
6
!
*
C
7
*
H
+
- H
&
!
*
7
*
B
HI
7
*
,
6
H 7
* *
* / i
12
*
C
-
12
*
!
! 1 *
1 )* A
i = j
iptr
1 ! ,
* * *
!
) -
,
! * 12 ! ,
1
!
*
!
i
==
F
- H *
# * 1
A
! 1
* 2
+
The address operator.
" !
int*
#
double
j
=
! -
, !
,
1
< ! ! ,
7 !
H !
, 7
- !
) !
1
* 2
! * +
!
* ,
-
*
!
, 1
!
*
* !
1
#
7
!
$ #
7
!
$
2 !
Figure 8:
int i = 5;
int * iptr = & i ; // iptr initialized with the address of i
int j = * iptr ; // j == 5
!=
T*
T
iptr
0
!
H *
*
!
F
,
5
69
5
7
6
8
4
9
5
9
7
8
67
6
#
* ( 0
€ < " 1 ! H ,
1 1
!
* * * A *
, 1 C
**
1
1
!
0
i
k+i = n
n
H
i
n
s
i
ptr + expr
s
ptr + expr
*
k=n
k+i
, k+i
k
0
,
!
H 7
6
*
+!
1
+
F
- * ! 1
, !
1 ! 1
! &
* * H 7 6 * 1
! ! 1 1 !
) ! B H H *
*
* k, 0
,
!
ptr
i
s
'
?
*
,
?
!
7
! H !
* 1 * ,
*
!
?
,
* -
H
&
!
!
1
-
Adding integers to pointers.
*
(k + i)
i
p
p + si
!
*
! H 1
H
/ C
, <*
!
* , , !
*
1
!
1
H * A
, HI ;
! , * A
H
-
! H
H A
*
1
! 1
* 1
H !
1
%
! !
7
&
*
! * 1
*
F ,
) H 7
*
,
#
H * + A !
A , < *
A 1
A * !
"
*
* H * * ! * !
! ! IH 1 H
1 ,
* ! D ! H E
+
** B H 1 ** !
* crossed_out
H
7
!
1 , D
?
!
E 6 * * +
!
H ! ! // pointer to first element
, ,
& -
<
? *
!
* ( *
€ !
" A ! = €
?
? €
!
* ,
int * begin = & a [0]; // address of the first element
- ! H *
A ,
, * !
1 B **
*
?
?
-
*
,
*
*
B
*
H ! F
' IH ' *
!
D
E *
! * (
& €
"
int a [5];
int * begin = a ; // begin points to a [0]
! 6
, * A
!
!
7
!
/
?
%
? ?
#
;2 *
$ ( H
,
H
* ! !
' A
' H * 1 " )
[n]
H
!
H
&
1 ? , ? .
!
%
!
+
IH
B <
,
* *
*
* %
!
H 1
&
B , H !
* * *
* ** iptr
*
int * iptr ;
// uninitialized pointer
int j = * iptr ; // trouble !
1 *
*
**
E
H
) * * D
* 1
H
bool * begin = crossed_out;
begin
a[0]
#
%
%
2.6.7 Array-to-pointer conversion
< * *
*
!
H +
*
!
*
+
* 12
H D
E
!
,
2.6.8 Pointer arithmetic
bool * begin = crossed_out;
// pointer to first element
bool * end = crossed_out + 1000; // past - the - end pointer
// in the loop , pointer p successively points to all elements
for ( bool * p = begin ; p != end ; ++ p)
* p = false ; // * p is the element pointed to by p
+ -
n
n
ptr
"
5
69
5
7
6
8
4
9
5
9
7
8
67
6
?
*
*&
1
+
! ! ?
* ? *
* ( * , € " H !
7
i
H
6
? ? 7 - !
! )
!
*
*
-
k2 , 0
!
%
) *
!
H
n
,
+!
1
* k1
-
HI
!
1
1 * *
& D
! !
E
1
,
-
k1 , 0
H * 1
+
Pointer subscripting, or the truth about random access.
; *
,
, H
H
&
, 1
C
*
H H
H
! < <= >
**
! )
! 1 * * * & 7
6 6
* ? * ? * 1 *
! 7
! 1 * !
H 1 1
! )
!
!
, )
* ? ? *
H C
*
6 F
1 1 * 7 *
IH
1 , 1
1 ,
!
H
! * !
& ! +
H * 6
C
*
, H 7
C
, 1
*
1
+
- ! H 1
, C H *
< H
*
p < end
H * i
H
7
, 6
C
1 *
%
* & ? ? 1
!
, k1 − k 2
'
, 3 *
(
*
* €
"
* )
) !
! IH
1
H )
+
? ,
? A
Pointer subtraction.
* !
,
? &
6
, ! !
)
*
1
!
- −
1
C
for ( bool * p = begin ; p != end ; ++ p)
* p = false ; // * p is the element pointed to by p
n
*
,
C
H , 1 ! (
( * H
* *
* * , *
? !
n
n
k2
>=
H 7
6
1 IH
,
*
bool * end = crossed_out + 1000; // pointer after last element
end
&
-
*
1
* IH
! - 1
, 1 * ! <
-=
- 1 6
p
+!
p
p = begin
n
n
k2 , 0
n
==
!=
H F
--
k1
n
? 1, 000
!
? )
7
(
* ! k−i
? k1 , 0
*p = false
< <= >
++p
0
p
*
k2 = n
*
,
< * end
) 6 7 6
7
/ * ! ? ? crossed_out
1, 000
i
k
k1 = n
1
Pointer comparison.
* * , !
C
H H 1 H € %
"$ H 1
2
*
* ;
* *
0
*
&
H 1
! ! !
) )
H
*
H F
!
++
k+i
0
*
% H +=
0
n
*
−
i
k
k1
k2
p != end
p
p
>=
n
k2
k−i
[]
1
*
?
, 3
H !
*
H 1
A
*
5
69
5
7
6
8
7
4
9
5
9
8
67
6
**
6
H
) &
! H A 7
, * !6 ) H 1
€ * 2
1 !
!
1
*
A
0
2 * 1 +
! * 1
A
1 A 1
* 1, 000
int unsigned int
!
7
A
? & <
)
* * * - * *
* & ! * /
! *
? *
!
'
'
1
6
!
%
*
!
- 1
!
! $
*
/
!
? *
1
,
7
bool
6
2.6.9 Dynamic memory allocation
n
1
1
* 2
A
!
A
(
, H
*
H *
1
2
* &
HI
!
*
-
1
2 * & A
!
!
1
H * * !
!
1
)
(
12
*
* 1 !
* ! , *
1 H - 1
* 1 * ! ! !
1
!
H )*
)
* , !
- A * )
*
*
2 <
* & 1
!
*
D
*
* E
1
G
*
A
1 * 6
* * C
* 12 7
- !
*
* A
!
+ ,
- 7
D
* E
6
!
, 7
$
) )*
(
!
* 1 !
) ; ? *
* HI
,
F
!
)
!
%
)
*
6
* *
, 1
HI H (
*
D €
7
, 12
E *
6 !
* ! * 1
*
"
"
'
'
!
€
int
* 1
H * 2
!
7
IH
, 1
*
1
1
* * ,
+
$ $
1
2 * $
, 6
! !
*
7 * ,
H
1
H
*
*
*
1
* 2
! #
0 !
2
. 2
.
# ! ! / ! !
! !
!
/
/
2 Prec. Assoc.
{
* %
1
!
HI *
! !
A H
!
A ! Arity
,
, H &
*
F )
What have we gained with pointers?
,
! , !
Operator
[]
*
&
{{ { {| } { | } { H
,
1 ! ,
A !
* 1 <
* H , A /
1 1
!
H
1
Table 4:
*
H *
H %
, %
Description
new
n−1
delete
n
n
// Program : eratosthenes2.C
// Calculate prime numbers in {2 ,... ,n -1} using
// Eratosthenes ’ sieve .
# include < iostream >
int main ()
{
// input
std :: cout < < " Compute prime numbers in {2 ,... ,n -1} for n =? ";
unsigned int n;
std :: cin > > n;
// definition and initialization : provides us with
// Booleans crossed_out [0] ,... , crossed_out[n -1]
bool * crossed_out = new bool [ n ];
// dynamic allocation
for ( unsigned int i = 0; i < n ; ++ i)
crossed_out[i ] = false ;
// computation and output
std :: cout < < " Prime numbers in {2 ,... ," < < n -1 < < " }:\ n ";
for ( unsigned int i = 2; i < n ; ++ i)
if (! crossed_out[i ]) {
$
5
69
5
7
6
8
4
9
5
9
7
8
67
6
!
#
#
#
#
#
%
"
!
* * 12
H 1 ,
? & - ! ?
1
, &
)
, !
*
* H
* ,
1
!
delete
& !
* , ! H 1
* * <
+
! *
* H !
)
H
&
*
, !
*
1
* 2
?
1
&
, 1 H * ,
new
-
int
* F
/
) * H A
*
,
1
1
!
* 2
1
C
* *
?
*
+
* * H
1
1
* / , H )
delete
+
HI
*
*
!
)
7
2
<
"
!
1
,
, - * , /
, H
A
*
!
!
)
- * H
H
) * ! * #
* * 12
* *
,
G
) 1 ? * * #
* * # * * '
'
+
!
1
!
% H "
!
7!
n
delete
+!
int * i = new int ;
int * j = new int (6);
F
The delete expression.
+
7
,
H
H
,
1
&
!
* * B
! !
delete
j
! !
- )
€ ! **
1
1
*
A
7
6
* delete
delete
* ,
!
7
{ | {
{ { { }{ {
}
return 0;
T
7
crossed_out
* crossed_out
// i is undefined
// j is 6
// * i is undefined
// * j is 6
i
int
* /
0
7
}
n
,
1
)
)
* IH
+
,
// free dynamic memory
n
A
int*
int
new
H
!
! * 12
!
HI 1 7
1
* *
,
1 -
12
* delete [] crossed_out ;
int i ;
int j = 6;
H
int * i = new int ;
int * j = new int (6);
new
& H
*
* , H * /
!
+
+
Program 15:
7 *
! " * 12
H !
, €
.
,
* 12 1
* 2
, !
}
std :: cout < < "\ n";
!
1 ! H F
)
H !
! * * 12 ! , 1
H
The new expression.
* ,
* +!
! ,
)
new
* H !
*
1
* 2
!
new
new
new
:
new
n
-
7
&
!
,
// i is prime
std :: cout < < i < < " ";
// cross out all proper multiples of i
for ( unsigned int m = 2* i ; m < n ; m += i )
crossed_out[ m ] = true ;
// * i is undefined
// * j is 6
delete j;
delete i;
new
delete
€
5
69
5
7
6
8
4
9
5
9
7
8
67
6
S
97
’\0’
*
- 1 H A
, H , ? E *
*
H
a
- & 1
*
! *
%!
* H &
! !
!
&
4
0
char
’b’ ’o’ ’o’
text
’\0’
* H F
)
* 1 &
(
1 *
* %(
+
* +
*
,
7
7
B
!
1
,
* 6
8
{−128, . . . , 127}
%
)
C
*
!
*
*
* 1
H
!
IH
!
, H
*
*
*
1
H * * )
* * ,
*
*
!
- H
* char(’a’+1)
* * ! * * 1
1 *
A
A H *
’\n’
char
From characters to text.
, 6
* {0, . . . , 127}
'
'
A
1
C
*
’b’
H 7 * )
, 6
*
,
%
" *
*
*
*
, !
6 * !
B 7 H
A
!
)
+!
ASCII
)
98
H
!
*
*
C (
int
H
*
" *
1
IH ! A
(
*
! !
* * )
1 H
! / - H
! H , )
! ,
1
’b’
, 3
H A
*
*
C
*
1 * * * - * ! , , + !
6 * $ A ! C 3 *
( IH * ) 1
*
A
!
! ! 7 * ! %
H , *
1 1
* * * * ! !
! ! 6
/ *
H H
7 *
)* !
) ! ? 1
-
,
int
A
, !
1
*
! * 1
* 2
* %
IH
'
' *
ASCII
H
7 -
HI , * H C , H !
- * * * *
6 !
* !
, *
a
char c = ’a ’;
*
*
char
*
(
!
,
* ) : 1 H )
* 1 IH
1
*
A
std :: cout < < " Prime numbers in {2 ,... ,999}:\ n";
char
’a’
C
* * * 1 * )
H
, !
' '
H !
1 ! * 1
1
* A
!
1 ) G * A
) +
2.6.10 Arrays of characters
DA
! Z
A
6 *
* * *
!
H
’a’
'
C
-
!
A
'
,
&
H H * char
*
z
’a’
ASCII
H
+ -
char
H 6
! *
, A
- H 7
Dynamic Storage Guideline:
*
6 &
& 7 1
* !
** ! 6 * 1
!
! ! , * 7 * * " 6 " 7
, 1
) !
H 7 *
char
unsigned int
unsigned int
char
#
c
1
&
, ! 1
’a’
F H !
< &
The type char.
a
9
$
I
! H
Memory leaks.
int
int
$
I
delete[]
for ( char c = ’a ’; c <= ’z ’ ; ++ c)
std :: cout < < c;
for
abcdefghijklmnopqrstuvwxyz
std :: cout < < ’a ’ + 1;
char
’a’+1
’a’+1
{0, . . . , 255}
char text [] = { ’b ’ , ’o ’ , ’o ’ , ’l ’}
char text [] = " bool "
0
’\0’
’l’
5
5
69
5
7
6
8
4
9
5
9
7
8
67
6
7
int main ()
{
// definition and initialization : provides us with
// Booleans crossed_out [0] ,... , crossed_out [999]
bool
* &
! ?
int main ()
{
// search string
char s [] = " bool ";
# include < iostream >
std::cin
E
IH
1 !
*
,
*
H
* # include < iostream >
// Program : eratosthenes. C
// Calculate prime numbers in {2 ,... ,999} using
// Eratosthenes ’ sieve .
)*
std::cin
* 1
H
D
!
- *
1
) * * 1 * *
* ! !
%
<
*
)
! !
&
& ** H
eratosthenes.C
"bool"
16
IH A
H
*
H
bool
!
* 1
1
,
*
2
<
" " "
!
Program 16:
0
bool
:
* * ,
* H H
1
* # , ! * !
* * ,
(
*
,
!
)*
i
for ( unsigned int j = 0; j < m ;)
// compare search string with window at j - th element
if ( w < m || s[j ] != t [( i+j )% m ])
// input text still too short , or mismatch :
// advance window by replacing first character
// find pattern in the text being read from std :: cin
std :: cin > > std :: noskipws ; // don ’t skip whitespaces!
unsigned int w = 0; // number of characters read so far
unsigned int i = 0; // index where t logically starts
// cyclic text window of size m
char * t = new char [m ];
& - )
+
* H // Program : string_matching. C
// find the first occurrence of a fixed string within the
// input text , and output the text so far
// determine search string length m
unsigned int m = 0;
for ( char * p = s ; * p != ’\0 ’ ; ++ p ) ++ m;
1 7 6 !
* H
, 1
* * * & !
! ) "
, 1
+
!
-
!
!
)
* !
! H ) H 0 0 ( )
! *
*
* * , H *
1
* 1
*
?
*
6 !
1 9 *
1 % " 6
) 7 *
€ 7
! *
*
, 1
!
<
-
! *
) +! * H
*
| } { |
!
*
* *
IH
!
6 *
& H * !
! *
B7
A **
i, i + 1, . . . , i + m − 1
*
m
{
m
1
7 * H 6
1
!
H 7
) 6 & * ** !
* ! *
* { { { }{ { { {{ { {| } { | } {
2, 3, . . . , m + 1
1, 2, ..., m
if ( std :: cin > > t[ i ]) {
std :: cout < < t[i ];
++ w ;
// one more character read
j = 0;
// restart with first characters
i = ( i +1)% m ; // of string and window
} else break ;
// no more characters in the input
else ++ j ; // match : go to next character
std :: cout < < "\ n";
delete [] t;
return 0;
}
i+m
std::cin >> t[i]
std :: cin > > std :: noskipws ; // don ’t skip whitespaces!
"
5
69
5
7
6
8
4
9
5
9
7
8
67
6
n
n2
0
nk
new
n
n1 , . . . , n k
(i, j) → mi + j
i=`
m
n
j=`
(i, j)
m
j
H
*
?*
? 7
'
'
)
7
6
* H
H
*
7
nk
{0, 1, . . . , m − 1}
`
m
- !
A )
;
&
!
,
1
n2
{0, 1, . . . , n − 1}
H
)
6
int *p[3]
int
*
H E
1
*
A A
'
'
,
1
6
D 7 !
E &
% !
*
!
* 7
H
nk
,
i
* .
nm
)
, 1
+!
new
mi + j
p
H *
*
int n = 2;
int (* p )[3] = new int [ n ][3];
, n
!
A
7D
n2
n2 , . . . , n k
A
%
H
, !
*
!
H
H
,
A
*
D
7
;
1
!
"
€
6
* (
**
H E
+
H H
7
nk
* -
H
n2
* n2
Arrays of pointers.
+!
1 * 1
!
Pointers to arrays.
1 , H *
< nk
nk
)
*
n2
!
n2
!
*
n = n1
2 !
&
* < * & !
* *
* 1 G
1 ! 7
-
!
* * * * *
A *
!
**
* ! * 1
!
$ 6 !
* ! H 1
&
F
) * 1
H ,
%
H
+
!
A
1
!
)
ni i = 1, . . . , k
{0, 1, . . . , nm − 1}
1
0
- *
* * *
* 1 ,
1 !
%
! ?
1
* H )
F
!
*
'
, H
+
'
7
new
a[i]
a[1]
p
, H G
1 ! !
1
* int a [][3] = { {2 ,4 ,6} , {1 ,3 ,5} };
a[1][2]
* 1
int a[2][3]
int
1
* 2
, !
* * a[i]
a[i][j]
+
7
- , n1 , . . . , n k
7
3
* !
* !
%
*
!
F
H
)
*
?
H
H * , 6
!
!
int[3]
a[1][1]
!
$
7
, 6 &
!
2
*
) ! !
H
, , )
*
int a [2][3]
a[0]
#
i
1 < B
0
a[1][0]
{2,4,6}
)
!
C
a[0][2]
* * Dynamic allocation of multidimensional arrays.
$
a[0][1]
int[2][3]
a[1]
$
!
% ) H ) *
*
H A ! ! C A
H )
*
1
<
* HI
! ?
n1
-
1
* a
int[2][3]
{1,3,5}
1 !
9
a[0][0]
*
!
D
G
E
* 12 A
!
!
?
H * * F * F
)
H
! H 3
H
Figure 10:
:
*
a
,
!
6
$
&
7
6=2
j
a[0]
&
!
! H
n2
a
! 0 1
A
* *
* 1 *
& * A *
* *
2
a
int
3
+
!
,
7 2.6.11 Multidimensional arrays
// type of * p : int [3] <= > p : int [3]*
int* p[3]
nk
m
5
69
5
7
6
8
7
4
9
5
9
8
67
6
S
, 3
* H ) 7 &
H ! 1
1 1
!
1 ! 6
&
T
+
S
T
S
S
C
T
∞
C
`<∞
H G
1
1
H
.
"
T
S
#
!
2
3
2
3
2
3
2
3
2
3
2
3
2
3
2
3
2
3
4
5
4
5
4
4
5
4
5
4
5
0
1
5
0
1
0
1
0
1
0
1
0
1
T
4
6
7
6
7
6
7
.
/
.
/
.
/
4
5
4
5
4
5
0
1
0
1
0
1
7
7
7
/
/
/
6
6
6
.
.
.
6
7
6
7
6
7
.
/
.
/
.
/
8
9
8
9
8
9
*
+
*
+
*
+
6
6
6
.
.
.
8
9
8
9
8
9
*
+
*
+
*
+
8
9
8
9
8
9
*
+
*
+
*
+
<
>
?
>
?
>
?
?
?
?
=
>
>
>
<
>
=
>
<
:
<
:
:
(
(
(
<
=
=
;
=
;
;
)
)
)
<
=
<
=
:
;
<
=
:
;
:
;
(
)
(
)
(
)
>
?
>
?
>
?
<
=
>
<
=
<
=
:
;
<
=
:
;
:
;
(
)
(
)
(
)
'
&
'
&
'
&
>
>
>
<
>
<
<
:
<
:
:
(
(
(
'
&
'
&
'
&
S
G
21
!
#
'
&
'
&
'
&
B
C
B
C
B
C
"
B
C
B
C
B
C
#
"
#
"
#
"
#
"
#
$
%
"
#
$
%
$
%
$
%
,
-
,
-
,
-
$
%
$
,
-
,
-
,
-
%
!
!
!
!
!
!
!
!
B
C
B
C
B
C
"
#
"
#
"
#
$
%
$
%
$
%
,
-
,
-
,
-
!
!
!
!
@
A
@
A
@
A
@
A
@
A
@
A
@
A
@
A
@
A
) $
5
69
5
7
6
8
4
9
5
9
7
8
67
6
A
*
* 1
H A 1
* ! !
7 F 6
€ 6 !7 * ) *
B
#
)
, * !
?*
B , H A A
1* H , H * T
*
:
A % 1
1 *
S
S
1
!
+
! )
1 H * < A
* ,
1 * ! 2 /
* ? 1
!
< 7 6
& H * * * *
!
! ! 1 S
!
< !
*
H
, 1
* 7 +
+
!
* ) F H
& € 6 1
7 ,
!
! H * , 1
C ! 6
Figure 12:
i+1
! H 7 6 1 * ! &
*
7 1 ! 1
! * ; !
H G H ,
! * H n
1
6
int
*
!
// a points to the first element of an array of n pointers to int
int ** a = new int *[ n ];
for ( int i = 0; i < n ; ++ i)
// a[ i ] points to the first element of an array of m int ’s
a [i ] = new int [m ];
1
*
! A
1
1 1
!
* 1 #
a[0][0] a[0][1] a[0][2]
!
!
int
6 *
,
0
;
* 1 H 1 * ,
1
$ * C ! * " A 7
1 "
1 ! * C S
1
D E 1
1 1
1 H !
*
C ! *
?
* &
m
*
,
&
HI 1
1
1
IH
, - !
H , H !
!
a[i][j]
, !
6
! - ! * &
! !
,
H n
H *
T
i
n
! !
! 6 , C
7 * * A
1
1 , #
!
D
* ( E
€
"
H 7
!
* 1
-
H
* * 8
!
$
n
a[i]
* $
$
- 2
A
D
E
/ < F *
1
F
H
m
*
! ) 7 < *
% "
!
, ! !
6
1 ! * ! ,
!
6 *
#
IH
* H
* *
* , !
!
+
! * H -
IH 1
a[i][j]
0
a[1][0] a[1][1] a[1][2]
*
*
*
* *
!* G H 1 , * C !
+
0
? , ,
/
B) * H * !
! !
)
! ) int
!
A
*
S
1
$ $
Computing shortest paths.
9
a[1]
7
6
int**
H
n
H *
Figure 11:
%
*
(
1 6 IH !
1 *
* 7 1 1
* * A
*
1
*
, D 6
E
7 *
1 % 1 !
) * 1
* *
6 7 *
n
*
!
2
*
* HI 1
!
,
1
a[0]
a[i][j]
S
12
T
S
T
m
i
*
* H
F
floor
i−1
// main loop : find and label cells reachable in i =1 ,2 ,... steps
for ( int i =1;; ++ i ) {
floor
−1
i = 1, 2, . . .
−2
i−1
i
main
i
C
S
S
0
i=2
#
v
w
v
w
v
w
v
w
v
w
v
w
v
w
v
w
v
w
V
W
V
W
T
T
V
W
V
W
V
W
x
y
x
y
x
v
v
x
y
x
y
x
y
t
u
t
u
y
t
u
t
u
t
u
t
u
v
T
T
X
Y
X
Y
X
Y
V
W
V
W
V
W
V
W
V
W
V
W
X
Y
X
Y
X
Y
P
X
Y
X
Y
X
Y
P
Q
P
Q
Z
[
Z
[
Z
[
L
M
L
M
L
M
X
Y
X
Y
X
Y
P
Q
P
Q
P
Q
Z
[
Z
[
Z
[
L
M
L
M
L
M
Z
[
Z
[
Z
[
L
M
L
M
L
M
`
a
`
a
`
a
^
_
`
a
^
_
`
a
^
_
`
^
_
a
^
_
\
]
\
]
\
]
J
K
J
K
J
K
^
Z
Z
Z
L
L
L
_
\
]
\
]
\
]
J
K
J
K
J
K
`
a
`
a
`
a
^
_
^
_
^
_
\
]
\
]
\
]
J
K
J
K
J
K
H
I
H
I
H
I
`
a
`
a
`
a
^
_
^
_
^
_
\
]
\
]
\
]
J
K
J
K
J
K
H
I
H
I
H
I
H
I
H
I
H
I
d
e
d
e
d
e
D
d
e
d
e
d
e
E
D
E
D
E
D
E
D
E
F
G
F
G
F
G
N
O
D
E
F
G
F
G
F
G
N
O
z
{
z
{
z
{
r
s
r
s
r
s
x
y
x
y
x
y
t
u
t
u
t
u
x
y
x
y
x
y
t
u
t
u
t
u
z
{
z
{
z
{
r
s
r
s
r
s
z
{
z
{
z
{
r
s
r
s
r
s
|
}
|
}
|
}
n
o
n
o
n
o
z
{
z
{
z
{
r
s
r
s
r
s
|
}
|
}
|
}
n
o
n
o
n
o
|
}
|
}
|
}
n
o
n
o
n
o
‚
ƒ
‚
ƒ
‚
ƒ
‚
ƒ
€

€

‚
ƒ
‚
€

€

ƒ
€

~

~

~

l
m
l
m
l
m
€
|
|
|
n
n
n

~

~

~

l
m
l
m
l
m
‚
ƒ
‚
ƒ
‚
ƒ
€

€

€

~

~

~

l
m
l
m
l
m
k
j
k
j
k
j
‚
ƒ
‚
ƒ
‚
ƒ
€

€

€

~

~

~

l
m
l
m
l
m
k
j
k
j
k
j
k
j
k
j
k
j
†
‡
†
‡
†
‡
f
†
‡
†
‡
†
‡
g
f
g
f
g
f
g
f
g
f
g
h
i
h
h
i
i
h
i
h
i
p
q
p
q
p
q
h
i
p
q
p
q
p
q
†
‡
†
‡
†
‡
f
g
f
g
f
g
h
i
h
i
h
i
p
q
p
q
p
q
„
…
„
…
„
…
b
c
b
c
b
c
d
e
d
e
d
e
D
E
D
E
D
E
F
G
F
G
F
G
N
O
„
…
„
…
„
…
b
c
b
c
b
c
„
…
„
…
„
…
b
c
b
c
b
c
„
…
„
…
„
…
b
c
b
c
b
c
i=0
T
U
T
U
V
W
R
Q
P
Q
P
Q
3
2 1 0
3 2 1
3 2
3
T
U
T
U
T
U
P
R
S
R
S
P
Q
2
3
2
3
2
2
3
2
3
2
3
3
2
3
2
3
2
3
4
5
4
5
4
4
5
4
5
4
5
0
1
0
1
5
0
1
0
1
0
1
0
1
T
# ! # "
!
T
U
T
U
T
U
T
S
U
R
Q
P
Q
S
R
S
R
S
N
O
N
O
i = nm − 1
S
R
S
R
S
S
R
S
P
Q
R
N
O
N
S
0
!
Figure 13:
S
i=
1 0
2 1
2
R
0, 1, 2, 3
O
6
7
6
7
6
7
.
/
.
/
.
/
4
5
4
5
4
5
0
1
0
1
0
1
6
7
6
7
6
7
.
/
.
/
.
/
S
S
2
N
S
O
6
7
6
7
6
7
.
/
.
/
.
/
8
9
8
9
8
9
*
+
*
+
*
+
8
9
8
9
8
9
*
+
*
+
*
+
*
HI
* 2
, *
* !
1 1
< * *
1 F
1 *
*
*
!
0
C
8
9
8
9
8
9
*
+
*
+
*
+
>
?
>
?
>
>
?
>
?
>
?
<
=
<
=
<
=
<
=
<
=
?
<
=
:
;
:
;
:
;
(
)
(
)
(
)
:
;
:
;
:
;
(
)
(
)
(
)
>
?
>
?
>
?
<
=
<
=
<
=
:
;
:
;
:
;
(
)
(
)
(
)
&
'
&
'
&
'
>
?
>
?
>
?
<
=
<
=
<
=
:
;
:
;
:
;
(
)
(
)
(
)
H
!
*
2
*
1
&
'
&
'
&
'
&
'
&
'
&
'
B
C
B
C
B
C
B
C
B
C
"
#
"
#
B
&
'
&
'
"
#
C
"
#
"
#
"
&
$
%
$
%
#
$
%
$
%
$
%
,
-
,
-
,
-
$
%
,
-
,
-
,
-
!
!
!
!
!
!
'
B
C
B
C
B
C
"
#
"
#
"
#
$
%
$
%
$
%
,
-
,
-
,
-
!
!
!
@
A
@
A
@
A
@
A
@
A
@
A
!
1
*
A
7
!
1 0
1
2
floor
0
R
i
N
, 1 6
1 1
7
* * * !
< ! , !
`−1
S
i
O
`−2
`
.
S
!
7
i−1
6
* 1
21
6
!
T
&
*
i−1
! *
?
H
7
int
int
*
7 H
1 H
6 &
! , * * 1 ! *
) F
7 <
- i
! !
!
*
* * !
+
H 0
i−1
!
!
1
* *
HI
!
F
* 6
* H
H ! 1 *
, , H 7
*
* 1 1
!
* * * 1
F
! H S
S
0
0
A
* '
* '
!
, 1
!
* , )* 1
1 *
1 * 1 ! !
+
H
2 * 6 1 * , , 1 1
7 1 1
; 1 1 * <
! H
H * * 1 ,
! * 1 ∞
!
m+2
n+2
1
1
i
S
T
20, 19, . . .
6
* 1
G
1
& ! * *
,
1 ! 1
6
S
T
-
i
The shortest path program.
!
! -
&
7 - !
! !
* ,
* * < ! G ) * H
1
, * ! G 2 * ( * * ) T =S
T
1
1
+
H * &
*
n+2
H
, 2
*
* 1 *
* 1 1 `
+
H * ! 1
1 1
* 1 7
,
< 1
*
1 6
1 * *
* 1
S
* H )*
C
i 0
*
S
T
i=1
T
i=3
C
bool progress = false ;
for ( int r =1; r <n +1; ++ r )
for ( int c =1; c <m +1; ++ c ) {
if ( floor [r ][ c ] != -1) continue ; // wall , or labeled before
// is any neighbor reachable in i -1 steps ?
if ( floor [r -1][ c ] == i -1 || floor [ r +1][ c ] == i -1 ||
floor [r ][c -1] == i -1 || floor [ r ][ c +1] == i -1 ) {
floor [ r ][ c ] = i ; // label cell with i
progress = true ;
}
}
if (! progress ) break ;
}
@
A
@
A
@
A
€
5
69
5
7
6
8
4
9
5
9
7
8
67
6
//
//
//
//
//
// target coordinates , set upon reading ’T ’
int tr = 0;
int tc = 0;
assign initial floor values from input :
source :
’S ’ - >
0 ( source reached in 0 steps )
target :
’T ’ - >
-1 ( number of steps still unknown )
wall :
’X ’ - >
-2
empty cell : ’ - ’ - >
-1 ( number of steps still unknown )
−3
S
T
floor
F
,
H
B *
6
)*
H 7
*
, 1
+
1
*
1
, ’X’
’T’
)
while
// mark shortest path from source to target ( if there is one )
int r = tr ; int c = tc ; // start from target
while ( floor [r ][ c ] > 0) {
int d = floor [r ][ c ] - 1; // distance one less
floor [r ][ c ] = -3; // mark cell as being on shortest path
// go to some neighbor with distance d
if
( floor [r -1][ c ] == d ) - - r;
)
#
"
! /
" !
"
%
#
!
Figure 15:
’-’
i = 23
// read floor dimensions
int n ; std :: cin > > n ; // number of rows
int m ; std :: cin > > m ; // number of columns
21 20 19 20
)
*
// dynamically allocate twodimensional array of dimensions
// ( n +2) x ( m +2) to hold the floor plus extra walls around
int ** floor = new int *[ n +2];
for ( int r =0; r <n +2; ++ r)
floor [r ] = new int [m +2];
22 21 20 21
17 18
20 19 18 19
23 22 21 22
10 11 12 13 14 15 16 17
*
H
! 1
1
%
) F
, 1 F
* 1 1
H
1 ! * )
*
T
!
"
!
!
!
!
!
!
14 15 16 17 18
!
2
*
*
* * 1
H 7
* 1
!
!
!
15 16 17 18 19
’S’
i = 23
nm
9
8
7
9 10
9 10 11
10 11 12
11 12 13
8
9
8
G
6
5
6
7
4
3
5
4
6
5
2
3
4
!
.
1
# 0 " "
! !
# (
" !
2 - $
" #
0
2
1
3
9 10
H
2
7
C
H )
*
H ! * G
* IH !
* - & ! !
* ,
) H !
6
*
F
+! * €
* ! A
F
6
!
5
3
1
C
! + !
* ! !
6
*6 !
* 7 )* H 7
1
!
floor
H
1
Figure 14:
!
!
4
8 12
------X - - - - -XXX - -X - - - - --SX - - - - - - - ---X - - -XXX - ---X - - -X - - - ---X - - -X - - - ---X - - -X -T - -------X - - - -
for ( int r =1; r <n +1; ++ r)
for ( int c =1; c < m +1; ++ c ) {
char entry = ’- ’;
std :: cin > > entry ;
if
( entry == ’S ’) floor [ r ][ c ] = 0;
else if ( entry == ’T ’) floor [ tr = r ][ tc = c ] = -1;
else if ( entry == ’X ’) floor [ r ][ c ] = -2;
else if ( entry == ’ -’) floor [ r ][ c ] = -1;
}
// add surrounding walls
for ( int r =0; r <n +2; ++ r)
floor [r ][0] = floor [ r ][ m +1] = -2;
for ( int c =0; c <m +2; ++ c)
floor [0][ c ] = floor [ n +1][ c ] = -2;
S
5
69
5
7
6
8
4
9
5
9
7
8
67
6
<<
<<
<<
<<
<<
}
!
// print floor with shortest path
for ( int r =1; r <n +1; ++ r ) {
for ( int c =1; c < m +1; ++ c)
if
( floor [r ][ c ] == 0)
std :: cout
else if ( r == tr && c == tc ) std :: cout
else if ( floor [r ][ c ] == -3) std :: cout
else if ( floor [r ][ c ] == -2) std :: cout
else
std :: cout
std :: cout < < "\n ";
}
’S ’;
’T ’;
’o ’;
’X ’;
’-’;
// delete dynamically allocated arrays
for ( int r =0; r <n +2; ++ r)
delete [] floor [r ];
delete [] floor ;
# include < iostream >
)
"
#
!
)
"
%
!
'
Figure 16:
#
ooooooX - - - - oXXX -oX - - - - ooSX - oooooo ---X - - -XXXo ---X - - -X -oo ---X - - -X -o - ---X - - -X -T - -------X - - - -
"
A
F
A
H *
*
*
)
+
!
*
return 0;
69
5
7
6
5
int main ()
{
// read floor dimensions
int n ; std :: cin > > n ; // number of rows
int m ; std :: cin > > m ; // number of columns
} { | } { | {
{ { { }{ { { {{ { {| } { | 1 +
!
1
*
* A
IH * *
1 IH
* < floor
’\n’
!
G
H * * H
’o’
* 1
−3
H F }
{
8
"
7
4
9
8
5
67
9
6
else if ( floor [r +1][ c ] == d ) ++ r;
else if ( floor [r ][ c -1] == d ) - - c;
else
++ c ; // ( floor [r ][ c +1] == d )
// dynamically allocate twodimensional array of dimensions
// ( n +2) x ( m +2) to hold the floor plus extra walls around
int ** floor = new int *[ n +2];
for ( int r =0; r <n +2; ++ r)
floor [r ] = new int [m +2];
// target coordinates , set upon reading ’T ’
int tr = 0;
int tc = 0;
// assign initial floor values from input :
// source :
’S ’ - >
0 ( source reached in 0 steps )
// target :
’T ’ - >
-1 ( number of steps still unknown )
// wall :
’X ’ - >
-2
// empty cell : ’ - ’ - >
-1 ( number of steps still unknown )
for ( int r =1; r <n +1; ++ r)
for ( int c =1; c < m +1; ++ c ) {
char entry = ’-’;
std :: cin > > entry ;
if
( entry == ’S ’) floor [ r ][ c ] = 0;
else if ( entry == ’T ’) floor [ tr = r ][ tc = c ] = -1;
else if ( entry == ’X ’) floor [ r ][ c ] = -2;
else if ( entry == ’ -’) floor [ r ][ c ] = -1;
}
// add surrounding walls
for ( int r =0; r <n +2; ++ r)
floor [r ][0] = floor [ r ][ m +1] = -2;
for ( int c =0; c <m +2; ++ c)
floor [0][ c ] = floor [ n +1][ c ] = -2;
// main loop : find and label cells reachable in i =1 ,2 ,... steps
for ( int i =1;; ++ i ) {
bool progress = false ;
for ( int r =1; r < n +1; ++ r )
for ( int c =1; c <m +1; ++ c ) {
if ( floor [r ][ c ] != -1) continue ; // wall , or labeled before
// is any neighbor reachable in i -1 steps ?
if ( floor [r -1][ c ] == i -1 || floor [ r +1][ c ] == i -1 ||
, *
& ! * !
:
H A
#
(
1 , , > ? * A
6 A
* * 7 H >
! A
1 !
1 1
!
&
1 1
*
*
* *
*
**
H +
1
* ! ,
H ? 6 A * !
C
* A 7 A
* 1 * *
! ) ! #
1
!
*
!
*
H
*
HI
)
*
*
!
12
*
* , * 1 *
H
?
@? !
1
* * '
'
H
!
Arrays are insecure.
&
** !
, *
A
H 1 * &
) * D1 !
, E *
IH
* . 6 ,
7 , * 1 *
A ! A !
7
1 1
! *
6 1
* A 7 * !
* . * * 6
1 ? *
*
, *
* D
E
1 2 1
1 7 ' ' C
C % *
* * !
& D )
! * - E 1
IH
1
1 * A
A
* *
?
%
,
1
6
1 * HI , A
#
&
+
!
,
H
%
* * * *
F * H ) &
)
H
!
H
Arrays have fixed length.
1
H *
* A
*
D
2
*
AE
*
+
}
,
6
7 !
HI
*
*
!
F
1 1
G C
C !
*
, A
H
1
,
return 0;
*
H
A
*
}
}
}
{ }
if (! progress ) break ;
*
*
}}
}
2.6.12 Beyond arrays and pointers
}
! *
)
* 1
1
, !
%
H // delete dynamically allocated arrays
for ( int r =0; r <n +2; ++ r)
delete [] floor [r ];
delete [] floor ;
’S ’;
’T ’;
’o ’;
’X ’;
’-’;
*
<<
<<
<<
<<
<<
,
// print floor with shortest path
for ( int r =1; r <n +1; ++ r ) {
for ( int c =1; c < m +1; ++ c )
if
( floor [r ][ c ] == 0)
std :: cout
else if ( r == tr && c == tc ) std :: cout
else if ( floor [r ][ c ] == -3) std :: cout
else if ( floor [r ][ c ] == -2) std :: cout
else
std :: cout
std :: cout < < "\n ";
}
<
2 !
Program 17:
"
!
} | } } } }
| } { | {
{ }
// mark shortest path from source to target ( if there is one )
int r = tr ; int c = tc ; // start from target
while ( floor [r ][ c ] > 0) {
int d = floor [r ][ c ] - 1; // distance one less
floor [r ][ c ] = -3; // mark cell as being on shortest path
// go to some neighbor with distance d
if
( floor [r -1][ c ] == d ) - - r;
else if ( floor [r +1][ c ] == d ) ++ r;
else if ( floor [r ][ c -1] == d ) - - c;
else
++ c ; // ( floor [r ][ c +1] == d)
}
& %
,
!
!
, A
* F , * , ! , 1
H A |
}
? !
*
*
H 1
*
A
!
*
floor [r ][c -1] == i -1 || floor [ r ][ c +1] == i -1 ) {
floor [r ][ c ] = i ; // label cell with i
progress = true ;
}
5
69
5
7
6
8
7
4
9
5
9
8
67
6
// computation and output
int main ()
{
// define a constant n
const unsigned int n = 1000;
// definition and initialization : provides us with
// Booleans crossed_out [0] ,... , crossed_out[n -1]
bool crossed_out[n ];
for ( unsigned int i = 0; i < n ; ++ i)
crossed_out[i ] = false ;
# include < iostream >
int main ( int argc , char * argv [])
1
"
*
,
!
*
A
*
H 2
1
!
* ,
< H *
*
*
,
+
H
,
*
B
*
*
,
1
+
!
A
(
, * ?
*
H
!
)
F
,
!
*
A
!
!
A
&
- * A
? &
1
* * !
?
G
*
)
,
1
2
<
"
!
-
A
?
IH 1
* < 1 1 !
* ! n
, "
, !
*
*
H
,
, *
,
A
Program 18:
Command line arguments.
+
)
-
*
1 * , 1 2 1
? * F
HI H * * !
* A
& 1
A
? const
!
// Program : eratosthenes.C
// Calculate prime numbers in {2 ,... ,n -1} using
// Eratosthenes ’ sieve .
*
-
*
1000
&
A
, n
10000
,
!
A
A
(
, 1000
* *
!
/
Constant expressions.
10, 000
- %
, G
! ) 1
*
* * ** , * * *
!
,
"
1
{
2.6.13 Details
} { # include < iostream >
A A } { | } B 7
}{
* 6 *
A *
, * €
A ,
*
* H * *
! ' 1
' ! * *
| {
{ { {
H * A 1 1
*
+
*
*
1
G ) 1
! A
, , *
{ {{ { {| {
%
7 !
*6
Why arrays, after all?
std :: cout < < " Prime numbers in {2 ,... ," < < n -1 < < " }:\ n ";
for ( unsigned int i = 2; i < n ; ++ i)
if (! crossed_out[i ]) {
// i is prime
std :: cout < < i < < " ";
// cross out all proper multiples of i
for ( unsigned int m = 2* i ; m < n ; m += i )
crossed_out[ m ] = true ;
}
std :: cout < < "\ n";
}
return 0;
n
const unsigned int n = 1000;
n = 10000; // error : can ’t assign to a constant
const unsigned int n ; // error : uninitialized constant
1000
// Program : string_matching2.C
// find the first occurrence of a string ( provided as command
// line argument ) within the input text , and output text so far
"
$
5
69
5
7
6
8
4
9
5
9
7
8
67
6
1
p
i
int
0
(
a
"
!
!
$
" $
3
7
6
2
# 0
( 0 a
# " 3 2 %
!
"
$ "
# !!
/
!
!
"
"
!
{0, . . . , n − 1}
0
$
n
i=n
i
#
0
$
#
i
a
n
!
$ #
!
2.6.14 Goals
Exercise 66
n
-
C
& H , 1
"
€
* (
*
1
0$
!
"
!
"
0 #
4
-
"string_matching2"
2
9
H 7 , * 6
* *
7 - *
* , 1
!
*
* ! 6 2 6
7
*
./ string_matching2 bool
"
"bool"
!
H ! !
! &
? H
,
! 1
* * 1
* ,
*
!
*
< 7
H
?)
6
A
1 * &
!
*
) ( ) !
*
* B
!
*
!
?*
6
"3 7
)
*
A
-
,
& !
!
?
6
* ! *
!
* 7 *
!
* 1
argv[]
"
*
)
H %
1
* B
} { | 6
7
6
7
* * ,
IH
3
// search string : second command line argument
char * s = argv [1];
-
* H 7 * 1 B
* B
*
*
1
*
A
* -
6 7 !
2 B *
A
1
**
6
3 7
* * !
*
C
7 B 6
6
3 7
B
7
if ( argc < 2) {
// no command line arguments ( except program name )
std :: cout < < " Usage : string_matching2 < string >\ n" ;
return 1;
}
! !
1 1
! 2 - #
/ B
1
! A
B H **
7
1
argc
2
,
, 1
€7
2
argv[0]
"bool"
s
6
1
7
argc
A
* 6
B 7
, !
1
* 1
1
1
7
7
A
*
*
B A *
!
*
*
*
1
!
1
* )
"bool"
"string_matching2"
H
* +
Operational.
H 7
Dispositional.
H 3
6
?
&
3
* * 6
6
€7
,
1
{
main
2.6.15 Exercises
Exercise 65
argv[1]
# include < iostream >
int main ()
{
int a [] = {5 , 6 , 2 , 3 , 1 , 4 , 0};
int * p = a;
do {
std :: cout < < * p < < " " ;
p = a + * p;
} while ( p != a );
}
return 0;
n=7
int
"€
"
5
69
5
7
6
8
4
9
5
9
7
8
67
6
5 4 3 6 1 2
read_array.C
1 2 3 4 6
n
4 3 6 1 2
1
Exercise 73
int main ()
{
int a [4][2][3] =
{ // the 4 elements of a:
{ // the 2 elements of a [0]:
{2 , 4 , 5} , // the three elements
{4 , 6 , 7} // the three elements
},
{ // the 2 elements of a [1]:
{1 , 5 , 9} , // the three elements
{4 , 6 , 1} // the three elements
n
# include < iostream >
of a [0][0]
of a [0][1]
of a [1][0]
of a [1][1]
6 0 3 7
76
3 7
3
6
6
€7
3
(123)
!
n1 = π(nk )
2
nk = π(nk−1 )
#
# $
#
! ! #
! 0 $ #
) 1 ! $ /
" !
" 2 # ! $ 0
3 2 0
! #
-
#
!
# #
7
6
! 3
-
/
!
1 1 ! 0
$
/ / 2 - #
# -
!
( "
!
!
! !
* * * #
" # π
# !
! ! # 2
$ # !
!! "
#
2
π
m
m(n − m + 1)
$
( n 1 n2
2
#
.
!
"
π(0), . . . , π(n − 1)
{0, . . . , n − 1}
cycles.C
s
π(3) = 1,
"
π
m
π
"
1
!
/ ? *
"
#
4
n
π
t
#
"
,*
/5
#
"
!
5
read_array.C
(04)
π(2) = 3,
#
!
n1 , . . . , n k
...,
),
!
n3 = π(n2 ),
"
π(1) = 2,
!
! $
2 1
2
1 4
! ! !
"
!
1
$
# 2 $ !
$ # !
"
# !
*
*
* π
"
π(0) = 4,
π(n1 ) = n1
2
/
2
n2 = π(n1 ),
$
0
6
3 7
8 0 #
!
:
{0, . . . , n − 1}
0 Exercise 72
m > 1, n
/
" "
" " #
" #
%
"
),
/ 2
%
# " #
!
!
- . ! $
0 (
! "
#
! #
1 $
$
3
€7
6
$ /
Exercise 71
cycles.C
{0, . . . , n − 1}
#
<
j
$
" n1
$ #
A
!
A
<
π
$
i
2
2
(Aji )
,
(A)
2
!
AA−1
0
"
2 %
"
Aij
!
0 ! 2 !
/ 2
0
k_composite.C
k
"
M
i
/ # n = 1, 000, 000
i
n
A−1
0
"
!
# a
A−1
ij
# 1
#
$ 2
$
( !$ $
# A
2
!
A−1
p
5 4 3 6 1 2
!
4
3 3
n
3
/
$ 2 / "
0
6
3
€ 7 6 3 7 6 0
/
3 7 1 "
!
.
0
i
" read_array
! #
" ! " #
3
i
#
0 ! !" / ! (
# $ ! #
/
0 / ( / $ # 2 3 invert.C
#
/5
,*
20 = 2 2 5
0
k
{2, . . . , n − 1}
k
- !
" $ #
p
$
.
$
j
2
$
"
0
# A
sort_array.C
"
A !
( .
0
# n
"
2
0
3
(M)
!
4
!
(−1)i+j
!
A−1
ij =
Exercise 70
Exercise 69
5
$
n
/
!
$
! " !
! "
! 2 2 (
!
#
$ #
" " 2 $
2!- / ! 6 !
3
€ 7 ! "
6 3 7 !
Hint:
"
!
" Exercise 68
p
!
" " 2
!
" "
0 " 3 # $ 2 !
" . $ 2 !" !
! 2 - 6
3 $ €7
6 /
3 " 76
3 ! 7 ! ! (
!
#
$ #
Exercise 67
k
6=2 3
p
π
π
nk )
(n1 )
π(4) = 0
n
"
"
5
69
5
7
6
8
4
9
5
9
7
8
67
6
Exercise 75
n2 + n + 41
0
$ /
i−1
!$
$ !
! #
(-
2.6.16 Challenges
(
$#
!
!
#
2
2
$
!
!
#
4
*
*
2 ! " !
1
0
3 0 $
$ " #
#
$
# !
0 !
" # #
! ! $ . !
! $ 2 #
"
$
(
#
$
!
#
$
! (
"
$
*
2 *
2
$ #
2 4 $ # ! $
!
$ 0
*
, 1 $
! . 1
!
C
! *
#
"
!
$
$
$ $
.
(-
-
" 4
$ # !
"
#
0 " $
0 ! ! 2 ! 2 #
. " ! " #
3
0
(
$
"
!
read_array2.C
0 $ # %
"
)
! ( $ $
# 0 - $
a
" 2
-
(
k
2 i
shortest_path_fast.C
$#
2
"
#
i
" "
"
k
5
"
!
"
"
0
#
#
2
"
$
"
$
$
"
2 return 0;
:
Exercise 77
{97, . . . , 122}
!
"
#
/ int
!
-
a
"
Exercise 76
i
/
!
}
$ " #
0
!
2
19 of 520
36 of 520
31 of 520
9 of 520
6 of 520
19 of 520
0 of 520
34 of 520
0 of 520
37 of 520
};
)
r:
s:
t:
u:
v:
w:
x:
y:
z:
Other :
{65, . . . , 90}
<
2 " 2
.
< # ! "
"
!
4
'
2
& # $
#
" "
" 0
$
!
#
"
#
$! 3
0
"
!$ !
# #
(
!
#
!
!
$ ! "$ ! $
$ "
2 elements of a [3]:
7} , // the three elements of a [3][0]
5} // the three elements of a [3][1]
27 of 520
0 of 520
3 of 520
20 of 520
10 of 520
30 of 520
43 of 520
4 of 520
0 of 520
9
!
!
4
!
2
$
$ 2 $
!
6 3 7 !
$
# 4
0 ! $ # 3 # $
0 #
3 0 !
!
!
" !
!
2 "
2 &
!
#
" "
2 elements of a [2]:
0} , // the three elements of a [2][0]
3} // the three elements of a [2][1]
i:
j:
k:
l:
m:
n:
o:
p:
q:
{0, 1, . . . , 127}
’Z’
’z’
2
$
(
3
" %
" ),
0 ! 0 #
! ! #
/ / $ 0 0
frequencies.C
* !
$ 6
- ; "
7 ! )
2
6
3 #
" 7
$
threedim_array.C
a
# 4 !
Frequencies:
a:
45 of 520
b:
5 of 520
c:
5 of 520
d:
28 of 520
e:
65 of 520
f:
4 of 520
g:
13 of 520
h:
27 of 520
’A’
’a’
0 !
! " !
! 26
char
!
Exercise 74
!
/5
.
,* #
/5 $
/
"
# #
2 /
1
7
5
69
5
7
6
8
4
9
5
9
""
"
8
67
6
"
!
#
!
!
$
},
{ // the
{5 , 9 ,
{1 , 5 ,
},
{ // the
{6 , 7 ,
{7 , 8 ,
}
ak
a
1
read_array.C
# #
"
0xd1 d2
!
(
" " !
2 $
"
$(
#
$#
0
2
2
#
.
2
/ ! $
/
2
1 15 + 3 = 19
8
α
"
0 0 2
0 0
1
" 5 $ / / " # #
"
$ #
$
#
#
#
0
0
/! ),
0
$ !
5 !$ 2 8
/! #
" !
0
0$ " #
!
0
" 3
!
!
$
0x00
0 !
di
0
!
$
$ !
2 $
. !
/! 0 2
$ /
#
0x13
" "
2
8
0x15
XBM
"
2
"
!
"
" !/ 0 2 . 3
0 0
! #
!
" " " 3 3 3 3
3 0
0
0 2
.
3 0 0
00010011
1
2
test
test
#
4
XBM
0
2
# XBM
{−128, . . . , 127}
/! 0
1 $
. !
" $ 0 !
" #include
#
10, . . . , 15
{0, . . . , 255}
{0, . . . , 255}
xbm.C
XBM
#
# $ $ / 0
! # $ ) , 0
! " 0 ),
"
!
" ) #
#
/ 2 # ( 3
/! ! #
"
0 2
3 .
!
0 0 "
" 3
#
$
- 2
! /5
" ! " #
1
α − 256
char
test
! $&
0 " 3
3 ( " ! $ ! 0
#
" 2
0
f
16d1 + d2
-
#
!
$ 8
0
! $
0
$ "
0
C ! # 2 4 $ a
# define test_width 16
# define test_height 7
static char test_bits [] = {
0 x13 , 0 x00 , 0 x15 , 0 x00 , 0 x93 , 0 xcd , 0 x55 ,
0 xa5 , 0 x93 , 0 xc5 , 0 x00 , 0 x80 , 0 x00 , 0 x60 };
!
/ n2 − 10n + 2
n=2
2
0
3
n2 + n + 41
#
(
!
"
!
$ 5
n2 − 10n + 2
(
3
" p
XBM
"
3
2
3
#
! !
/ 2
"
(
- ! 2
! 2 - #
.
"
0
( "
( −7
test_bits
test
#
#
" p
" 8
2
0 " "
5 0
$ 2 !"
" . $ 2 4 !$ $ " 0 #
5 5 $ 1
$
!
an2 + bn + c
0x00
#
7
#
$ n=1
( "
! <
XBM
"
#
! - 0 ! $ " $
# 0
(
/5
! #
.
$ "
" 2
41 43 47 53 61 71 83 97 113 131 151 173 197 223 251 281
313 347 383 421 461 503 547 593 641 691 743 797 853 911 971
1033 1097 1163 1231 1301 1373 1447 1523 1601
0x13
16
0 2
"
2 5
$
0
$ )
2 0 *
#
2 -
"
2
.
40
/! "
"!
/
4
3
! XBM
#
"
$
XBM
XBM
$! 0
3
# $
!
= < = ! " =
=<
Exercise 78
2
n=0
−14
/! test_width
#
"
2 " 3
/! /
2 ! / ! $ *
$ # 0
! " " $ |an2 + bn + c|,
40
n = 0, 1, . . . , 39
{0, ..., 9, a, . . . , f}
19
n = 0, ..., p − 1
# define rotated_width 7
# define rotated_height 16
static char rotated_bits [] = {
0 x3c , 0 x54 , 0 x48 , 0 x00 ,
0 x04 , 0 x1c , 0 x00 , 0 x1c ,
0 x14 , 0 x08 , 0 x00 , 0 x1f ,
0 x00 , 0 x0a , 0 x15 , 0 x1f };
test_height
"
5
69
5
7
6
8
7
4
9
5
9
"
8
67
6
"$
} { | } { , - * 6
1
!
, 7
, +
*
G * !
!
1 ,
,
!
, * ! , !
*
*
1 1
$
,
* C
! * H
6
* ( 1
!
€ ! H
*
7 IH
*
# include < iostream >
// PRE : e >= 0 || b != 0.0
// POST : return value is b^e
double pow ( double b , int e)
{
double result = 1.0;
if ( e < 0) {
// b^ e = (1/ b )^( - e)
b = 1.0/ b ;
e = - e;
}
for ( int i = 0; i < e ; ++ i ) result *= b;
main
!
*
!
,
- A *
!
!
*
H *
!
* ,
*
&
1
A
+
e
// Prog : callpow . C
// Define and call a function for computing powers .
* *
H
H !
*
*
, +
'
'
* (
H €
# H 7
* * C ! *
*
C !
IH * A HI 1 0 ,
A
, IH
, /
IH !
/
6
* * *
HI
*
!
+
2
!
!
"
"
!
/
! #
0 0 $ # # $ !"
$ !
"
0 3
2
# 2 $
4 0 Functions
'
'
!
e
be
double
)*
&9
"
=
=<
#
.
Chapter 3
! * *
+
&
&
!
*
2 " "
" 3.1 A first C++ function
b
b
69
5
74
6
8
7
4
#
pow
!
?*
1
!
assert
A
H !
*
! *
?
*
B
!
1
,
+
*
'
'
!
HI
*
H
?
! 1
* *
)
!
!
H , *
, G
H
1 *
*
! 1
*
* )* +!
- 1 , H
!
,
G
, *
! * ,
*
1
1
!
*
H
)
!
* ,
!
1 ! * !
& , ? 1
,
* /
/
* !
pow
*
b
,
!
2
*
*
H
(
H * (
, €
!
!
*
*
*
H
&
F
* ,!
H
<
&
!
*
&
!
1
& - H * *
*
, " 0 *
! *
*
1
! !
*
+
*
! !
1
H *
!
, 6
, 6 ! 7 7
pow
* !
* *
pow
!
H
!
!
*
* * * * , H , * *
1 ?
-
!
<
!
* * !
*
1
*
+
*
1 !
H
- b
! ! * ,
* *
*
C
*
)
7
6
H
1 , / H
1
, A
Arithmetic pre- and postconditions.
!
H
H * // PRE :
Assertions.
-
be
1
<
20
!
"
!
return 0;
pow (0.0, -1)
< !
1 *
*
! * H
!
)
%
H * ;
*
A
% pow
e >= 0 || b != 0.0
3.1.1 Pre- and postconditions
H
0
e be
*
-
!
!
}
' 1
' * %
* *
*
b=0
#
*
{ { { }{ { { {{ { {| {
0.25
2.25
5
81
-512
*
+
* H
% & * /
!
b
be = (1/b)−e
, 7
pow
! 6
b = 0
, ;
)
*
*
*
1 ? ! ! * * F , ) H H
1 ,
,
! !
, ! e
1
!
*
* ,
?
*
!
, *
1
1
!
outputs
outputs
outputs
outputs
outputs
H
e
H
*
!
!
*
!
*
,
,
, * ) - & ?
A !
H H ,
H C
, , 1
! //
//
//
//
//
*
?
, pow ( 2.0 , -2) < < "\n " ;
pow ( 1.5 , 2) < < "\n " ;
pow ( 5.0 , 1) < < "\n " ;
pow ( 3.0 , 4) < < "\n " ;
pow ( -2.0 , 9) < < "\n " ;
1
e
if
* !
H * * !
! * A !
*
* * * A 1 ! , * &
A / * +
! * * ! ! * ! * - * * !
B ! *
H
*
Program 19:
e
for
$
- *
H
H
!
*
!
&
*
*
,!
# ! *
6
* 7 #
!
*
*
7
6
*
1
!
*
* '
!
*
*
'
€
H 7
return result ;
HI
b
<<
<<
<<
<<
<<
! H * !
* * , , , * /
; c
&
* * !
!
! "
A
!
6 ( ? * ? *
int main ()
{
std :: cout
std :: cout
std :: cout
std :: cout
std :: cout
e
// PRE :
}
e
pow
be
e
e
double
e >= 0 || b != 0.0
e
// POST : return value is b^e
bool
6
69
5
74
6
8
7
4
5
74
6
4
79
€
callpow.C
H
*
,
1 C ,
*
& H
* ) H
1 ! ,
? 1
7
6
F ) H )* H
* (
! pow(2.0,-2) b
if
0.25
!
*
!
,
* 1
,
!
!
*
*
-
0.5
H
−2
void
result
void
H
H
H
2
H
!
*
! *
7 H
7 * , $
! 7
H
# #
"
$
H
H
H
H -
7
7
7
76
!
*
1
)* *
! ,
, * / ! H ! 1
* !
* & *
)
-
&
!
*
!
7 - 1 , 2 )
* / , !
! * ! ! * ,
*
, *
- 1
*
HI * / ,
! ! )* ! *
* !
*
*
0
! =
!
$
&
B
return
!
7
*
-
*
,
HI
* (
1
!
!
€
cassert
'
'
!
*
*
-
H &
H pow
, * /
H 1
,
(
*
!
*
*
#
7
6
D
E
(
* 1 * *
& * ( 6 6 A * * ( H
! 7
€ ! € 7
7 $ H H 7 H 7
1
*
*
H A IH
C
* !
! *
H * H
1
*
assert
H
- , ! ,
! *
!
* * *
**
* H 7
! * , ,
, *
& * *
,
6
1
H
1
!
e
0.5
!
D1
* E
,
1
*
, - ! *
A
* ! ! , * !
H
* * H * ! * A ,
!
7 *
&
*
6
!
7 H 1
H 7 )
- !
) , *
" 1
*
,
F ! H
H H
*
+
*
H - 1 ,
, !
! )
H
?
)*
, ,
+!
*
H
H
H 7
6
,
H ? ! )* *
! * !
!
!
?
*
* *
* * A
-
!
, 1
!
+
- H $
)
!
! * * !
,&
!
*
* *
F
H ! !
* *
*
& )
*
! 0 ! * & HI * 1
! *
+
*!
H ! *
callpow.C
H
H
,
1
&
1
!
A
! * &
1
F ' '
! * 7
-
1 ,
)*
B )* @
* H * *
!
'
'
*
1
*
, !
* *
* *
H 2 1 *
*
! *
H !
< *
, ,
, * H
assert
pow (0.0,-1)
+
F
*
- &
*
H 1
H *
A
H 1 !
* *
H IH
A *
?
*
%
&
HI
!
!!
*
1
1
// PRE : e >= 0 || b != 0.0
// POST : return value is b^e
double pow ( double b , int e)
{
assert ( e >= 0 || b != 0.0);
double result = 1.0;
// the remainder is as before
...
}
main
3.1.3 Function calls
pow(2.0,-2)
3.1.2 Function definitions
2
pow(2.0,-2)
3.1.4 The type void
6
69
5
74
6
8
7
4
5
74
6
4
79
} { ! 1 * H * * 1 H , * !
1 *
1
D
1 !
* A
1
E
!
- ! !
*
!
A !
*
* - , ,
!
! ! !
*
H H H *
!
! H 1
! *
int f ( int i)
*
1 *
H * *
* (
H
!
*
pow
1
!
pow
*
, ,
* 1
! !
, *
1 !
* 1 (
*
! H ,
!
6 !
1 6 !
! H &
* !
1 H
7 * ( ,
€ * 1
* !
H 7
* *
! )*
b
B
*
;
{ !
*
1
!
,
&
!
!
,
1
# * &
* ! =€
! * H ! = € H H F 1 H *
b
*
1
(3 , 4)
€
! ) !
*
1
! * !
6 , *
1 , * 1 * 7 "
pow
}
b
IH
* 1 , A
? * * / 1
! ! !
* *
* *
!
,
- H ) ! H IH ,
H 1
! IH
) !
-
{ | e
!
!
*
, * *
H , * void f ()
{
++ i ;
}
H
,%
!
*
C
!
*
b
(
*
!
int i = 0; // global variable
} { | } { B
# include < iostream >
callpow.C
pow
H
!
!
3.1.5 Functions and scope
*
, )* !
,
! !
*
*
&
return
main
void
int main () {
double b = 2.0;
int e = -2;
std :: cout < < pow (b ,e ); // outputs 0.25
std :: cout < < b ;
// outputs 2
std :: cout < < e ;
// outputs -2
*
-
F ! !
!
*
* , &
*
! * *
& !
! !
print_pair(3,4)
-
7
€
, * *
H
6
* (
void
void
- * !
1
! !
*
, 1 )
! *
!
& !
* 1
// POST : "( i , j )" has been written to standard output
void print_pair ( int i , int j)
{
std :: cout < < "( " < < i < < " , " < < j < < " )\ n";
}
*
1
H H
* H !
* 1
,
, A ,
! !
1
*
6 A
!
*
+
void
!
*
H
*
A
!
*
1
C
}
6
!
*
!
*
,
7
H
)
%
print_pair
7
* H - D
E
* !
!
*
! H *
*
* , , /
*
! *
, , IH 1 !
*
A ? int main ()
{
print_pair (3 ,4); // outputs
}
int
return 0;
pow
e
e
e
pow
std::cout
# include < iostream >
// in the scope of declaration in line 3
int main ()
{
f ();
std :: cout < < i < < "\n" ; // outputs 1
}
return 0;
6
69
5
74
6
8
7
"
4
5
74
6
4
79
int main ()
main
# include < iostream >
int f ( int i ); // scope of f begins here
1
* €
* 1
i
// Program : perfect2 .C
// Find all perfect numbers up to an input number n
# include < iostream >
// POST : return value is the sum of all divisors of i
//
that are smaller than i
unsigned int sum_of_proper_divisors ( unsigned int i)
!
)
*
H ! +
, 1
1
,
!
!
*
1
!
H
1
!
1
HI
!
*
* *
!
*
# & H
H 6
& *
H 7 1 ! ?
* f
HI
1
,
g
H
! * !
* 1 ! *
* &
g
1
!
*
*
f g
g
C
1
!
*
*
f
*
n
test whether i is perfect
all proper divisors of i
*
A
, ! , C
1 - H
IH
, 1 *
* &
, (
H !
,
1
1
D
, ; E
, !
! *
*
1 & ! g
*
perfect.C
!
*
H
, A
)
& ! * )
*
H *
%
!
*
!
main
H
* 1 /
;
if
* 1
1 -
!
* HI % D
1
A E
*
* G H !
6
D 1 * E * 7
# include < iostream >
, * * B !
F
,
, 1
1
!
! *
- ! *
! !
* !
*
& 1
1 !
1
*
1
* *
1 !
!
*
1 * A H *
*
!
* f
?@ !
)
<
*
*
f
H
int main ()
{
std :: cout < < f (1); // f undeclared
return 0;
}
1
*
*
1
&
* !
) H
,
+
!
*
1
69
5
74
6
8
7
4
6
5
74
6
4
79
{
A
! * main
int i = 5; // invalid ; i hides formal argument
return i ;
int f ( int i ) // scope of f begins here
{
return i ;
}
!
*
int f ( int i)
{
{
int i = 5; // ok ; i is local to nested block
}
return i ; // the formal argument
}
{
*
*
!
1
!
* * , 1
C
& % ! * * H
1 ,
1
,
* int i = 5
H
f
main
H
!
- ! ,
IH
1
H ! * )
1
H &
*
* * Function declarations.
1
*
!
}
}
f
;
, *
*
!
{
std :: cout < < f (1); // ok , call is in scope of f
return 0;
}
int f ( int i)
{
return i ;
}
f
f
f
3.1.6 Procedural programming
n
n
i
1
compute the sum of
i
#
$
!
* !
* !
,
1 !
, B 1
1
H H A
%
*
! , $
!
1
*
-
? ? H C *
*
1 * +
1
1 ,
HI *
*
*
1
1
1 * ! , ! !
%
/
,< 1 1 !
*
0 %
& , 1
HI H & + ! *
!
H
1 1
! * A H
!
, A
,
2
! - H H @
! *
* !
6
! ! H
1
C
) * H *
!
7
, <
#
!
"
!
int a[5]
GOSUB
GOTO RETURN
C
!
A
1
perfect.C
GOTO
H
* }
6
%
1
H
1
1
H +
H
G
*
H
!
!
2
-
int a[5]
H
* !
C
, &
)
* ,
*
H
,
H
H
F
!
*
* *
)
*
1
A
7
!
! H
, 3
HI 1
!
,
} { | {
{ { { }{ { { {{ {
return 0;
H H
1
, 1
IH A
-
// computation and output
std :: cout < < " The following numbers are perfect .\ n" ;
for ( unsigned int i = 1; i <= n ; ++ i )
if ( is_perfect ( i )) std :: cout < < i < < " ";
std :: cout < < "\ n";
I
S
*1
A
!
int main ()
{
// input
std :: cout < < " Find perfect numbers up to n =? ";
unsigned int n;
std :: cin > > n;
A
2
Program 20:
-
{|
,
& (
*
! * €
, "
* €
, * , * *
6 * 1
* !
, ) ! ! * H - !
!
!
6
*7
) &
; %( *
+ *
#
A ! IH ;
%
+(
)
$ A **
!
$ A +
*
*
C
,
!
&
1
H A
* 1
*
!
A
} { | // POST : return value is true if and only if i is a
//
perfect number
bool is_perfect ( unsigned int i )
{
return sum_of_proper_divisors ( i ) == i;
}
*
"
$
}
2
> * !
+
A
; & 1 %
( H A H + ; * ! %( " + 6 " *
A
!
! A * ? H B 1 * ' 1 ** ' H
* 6 ) 1 *
? ! ,
* * ! 1 7 ! *
> 7 * B
* 1
B
H unsigned int sum = 0;
for ( unsigned int d = 1; d < i ; ++ d)
if ( i % d == 0) sum += d ;
return sum ;
G *
, 7
-
A 1
!
!
: !"
, H H
! * A {
3.1.7 Arrays as function arguments
// PRE : a [0] ,... ,a[n -1] are elements of an array
// POST : a [i ] is set to value , for 0 <= i < n
void fill_n ( int a [] , int n , int value );
// PRE : a [0] ,... ,a[n -1] are elements of an array
// POST : a [i ] is set to value , for 0 <= i < n
void fill_n ( int * a , int n , int value );
10
fill_n
fill_n
6
69
5
74
6
8
7
4
5
74
6
4
79
$
*&
*
!
!
*
!
*
*
H
!
E
D
H
H
E
!
*
*
*
D
A
pow
H
!
!
*
! * *
, * /
< H * / !
* *
* !
! %
*
, B
!
*
, !
*
1
/
*
1
pow
fill (a, a+5, 0)
! !
*
, ! ,
* ! , 1
/
1 * A
!
*
H - ! H !
*
!
*
! *
H
**
? H
H
B
!
"
H
€
&
H H , &
*
* (
D E * *
,
+
! *
;
*
,
H , 1 , *
C
D
E
!
*
++p
first
* *
!
,
!
H ,
* fill_n
++
H
* ! * ! * H A
1
*
;
H fill
,
* /
!
1 * )
!
-
fill
H
!
// PRE : a [0] ,... ,a[n -1] are elements of an array
// POST : a[i ] is set to value , for 0 <= i < n
void fill_n ( int * a , int n , int value ) {
// iteration by index
for ( int i = 0; i < n ; ++ i )
a[ i ] = value ;
}
fill
*
fill (a, a+5, 0)
a
*p
F
!
, A
&
+
!
*
1
!
H , ! &
1 F
! *
*
D
IH
E
- , 1
A
H A
*
! * 1
* D ,
E ?
{
**
/
*
-
A
7 &
H ,
, 6
* *
, *
! ! +
D * E H
!
*
* IH 1 A
* A
*
! 2
* /
*
HI
1
!
*
!
1
,
fill_n
, ?
!
* /
(
*
!
*
*
" <
2 "
!
Mutating functions.
+
* !
*
? !
}
{ { }{ { { { { {| } |
{ { {
# include < iostream >
p+1
fill
{0, . . . , n − 1}
! -
last
[first, last)
last-1
[first, last)
+
&
, 1 H ** ,
+
* *
, , H Program 21:
, %
H
H
1
!
*
"
int main ()
{
int a [5];
fill_n (a , 5 , 0); // a == {0 , 0 , 0 , 0 , 0}
fill ( a , a +5 , 1); // a == {1 , 1 , 1 , 1 , 1}
return 0;
}
1 &
, ! * !
*
B
// PRE : [ first , last ) is a valid range
// POST : * p is set to value , for p in [ first , last )
void fill ( int * first , int * last , int value ) {
// iteration by pointer
for ( int * p = first ; p != last ; ++ p)
*p = value ;
}
// Program : fill . C
// define and use two functions to fill an array
!
% , *
1 , ! ! H *
1
,
1 H !
H
* ( 6
€
+
1
"
H
7
*
IH !
, *
*
, ? ? , D
?
1 IH
first first+1
n
E
! ,
1 H !
+
'
' H !
1
* ;
C A ! ?* fill
fill_n
1
fill_n
pow
int a [5];
fill (a , a +5 , 0);
a
0
3.1.8 Modularization
€
69
5
74
6
8
7
4
6
5
74
6
4
79
E
*
* , !
!
0
<
!
pow ( 2.0 , -2) < < "\n " ;
pow ( 1.5 , 2) < < "\n " ;
pow ( 5.0 , 1) < < "\n " ;
pow ( 3.0 , 4) < < "\n " ;
pow ( -2.0 , 9) < < "\n " ;
2
Program 23:
//
//
//
//
//
outputs
outputs
outputs
outputs
outputs
return 0;
}
0.25
2.25
5
81
-512
int main ()
{
std :: cout
std :: cout
std :: cout
std :: cout
std :: cout
<<
<<
<<
<<
<<
pow
pow ( 2.0 , -2) < < "\n " ;
pow ( 1.5 , 2) < < "\n " ;
pow ( 5.0 , 1) < < "\n " ;
pow ( 3.0 , 4) < < "\n " ;
pow ( -2.0 , 9) < < "\n " ;
pow.h
pow.C
//
//
//
//
//
outputs
outputs
outputs
outputs
outputs
H
&
*
-
* *
*
! * * !
1 * ,
pow
pow.o
// Prog : callpow3 .C
// Call a function for computing powers .
# include < iostream >
# include " pow .h"
0.25
2.25
5
81
-512
H
&
* 12
*
*
*
pow.C
H
H
<!
,
&
*
H
! * 1 *
! !
*
7
* 1
* &
H
*
'
'
H
pow.o
2
1
A
main
*
B
{ *
+
,
H !
- * * & * 2
! * 1 1
* A <*
* A
*
1 , & $ H 1 ! * D
* E
! ;
!
*
?
* pow
H *
&
)*
1
6
* A * ,
*
? !
*
*
-
Separate compilation and object code files.
*
1 *
! +
,
H
<
- *
! * * ,
H
1 H *
!
& !
*
+
*
&
20
!
"
!
Header files.
H
pow.h
! H
// Prog : callpow2 .C
// Call a function for computing powers .
(
*
€ D
E
!
!
*
* H
*
&
!
# include < iostream >
# include " pow .C"
*
!
!
}
// PRE : e >= 0 || b != 0.0
// POST : return value is b^e
double pow ( double b , int e)
{
assert ( e >= 0 || b != 0.0);
double result = 1.0;
if ( e < 0) {
// b^ e = (1/ b )^( - e)
b = 1.0/ b ;
e = - e;
}
for ( int i =0; i <e ; ++ i ) result *= b;
return result ;
}
*
*
&
Program 22:
<<
<<
<<
<<
<<
"
!
-
} { | * A
, H
*
*
&
A
*
HI
1
,
!
!
*
-
H * *
*
* , 1
*
!
*
&
&
$
* * ! * & +
A
H
pow.C
{ | } { #include
E
%
} { | } { int main ()
{
std :: cout
std :: cout
std :: cout
std :: cout
std :: cout
pow
# include < cassert >
pow.C
pow.C
pow
pow
// PRE : e >= 0 || b != 0.0
// POST : return value is b^e
double pow ( double b , int e );
pow.h
69
5
74
6
8
7
4
6
5
74
6
4
79
+
pow
*
& A
! 1 * H
* *
1 !
6
* & 7
1 * , math.h
ifm
math.C
&
1
1 1
, +
* H A
! , , 1
* 12 !
& *
,
1
!
)*
1
7
6
*
!
H
* 1
*
1
$
+
H 1 * ,
1 12
*
* &
A 1
1 ! +
)
*
1 1
* 2 * H & &
H * -
* 1 7
H
&
*
* 1 ! *
! *
!
1 1 2
* *
&
* !
*
* 1
6
* 1
!
*
, ,
A
1
*
1 &
)*
*
1 1 , 3
1
2
*
* &
* ,
, H ! ! * 3
H
!
* 12
+
*
! *
1
*
H * !
!
*
! * *
!
*
H
H * (
1
1
)*
* H
1
!
*
* !
*
&
? *
-
!
H
1
1
!
*
* *
*
* *
%
! 1
)*
! 1
* <
H *
*
*
!
math.h
A
!
*
)*
1
)*
math.C
!
F 1 H
* 1
* *
1
! 1
1
1 1 2 *
! * &
*
* &
IH
H
& 1 H !
&
? )
*
math.C
* +
Centralization and namespaces
A
A
1
!
!
*
%
*
, !
+
* &
!
(
* math.h
* !
*
*
*
H
+
F
! * H
*
*
* ! )* 1
2
*
1 *
! &
&
!
* 12
pow
6
* !
*
* 7
& *
*
- H
& H * 12
*
&
*
! *
)*
!
1 !
+ *
* * * 1
math.o
#include
* ,
*
math.C
* math.C
!
*
-
pow
- A 1
! ! * * !
*
H * Libraries.
*
callpow3.o
pow.o
1
*
*
- H )* ,
* 6
A 7
!
*
!
1 &
!
*
*
*&
pow
1
? H
2
0
<
!
"
!
}
,
1
1
*
;
*
!
, 1 *
1
*
*
return 0;
!
,
+
!
, *
!
, !
(
! A * *
A , *
0 ! G
* H
?
+
! D* * E
, A
* H !
! * !
*
A
A H & <
& B ! callpow3.o
pow
! ! , F * * % H ! H B 7 !
! ( A , *
* ? * * H * *
&
1 A
1 ! ! IH * ! *
* &
( A *
! * H €
1
& 1 ! * H
0 1
! * A
6
,
* * *
A H
,
& B
* !
* 1
? *
*
*
*
&
* !
1 *
!
H *
, ? 1 *
1 ! * * !
A 1 * , *
H
callpow3.o
main
pow.o
H
! *
H H ! !
* ! *
&
1 * ! *
* & HI * 1
*
callpow3.o
$
* !
H ,
1
!
*
*
*
pow.C
+
*
Availability of sourcecode.
*
, *
* * * 1
, !
*
*
!
* * ) HI *
! , !
! *
H ,
* , * *
, !
!
*
*
!
!
1
IH
The linker.
pow.o
,
Program 24:
#
, *
: / * !
!
* *
, * *
1 *
&
}
math.C
math.h
libmath.a
math.o
iostream
"
69
5
74
6
8
7
4
6
5
74
6
4
79
69
5
74
6
8
7
4
*
*
callpow4.C
math.h
callpow4.o
{
math.o
assert ( e >= 0 || b != 0.0);
// PRE : e >= 0 || b != 0.0
// POST : return value is b^ e
double result = 1.0;
if ( e < 0) {
// b^e = (1/ b )^( - e )
b = 1.0/ b;
e = - e;
}
for ( int i =0; i < e ; ++ i ) result *= b ;
return result ;
{| } { *
*
6
5
74
6
4
79
math.C
}
2
"
!
<
+
*
"
IFM
ifm::pow
! *
2 "
!
math.h
// Prog : callpow4 .C
// Call library function for computing powers .
} { | } { namespace ifm {
// PRE : e >= 0 || b != 0.0
// POST : return value is b ^e
double pow ( double b , int e );
}
Program 25:
/ "
&
// math .h
// A small library of mathematical functions.
}
{ Figure 17:
callpow4.C
& H F callpow4
* *
1
!
*
A
!
Program 26:
libmath.a
2 A
{
*
, {
{{
}
# include < iostream >
# include < IFM / math .h >
int main ()
{
std :: cout
std :: cout
std :: cout
std :: cout
std :: cout
<<
<<
<<
<<
<<
ifm :: pow ( 2.0 , -2) < < "\ n" ;
ifm :: pow ( 1.5 , 2) < < "\ n" ;
ifm :: pow ( 5.0 , 1) < < "\ n" ;
ifm :: pow ( 3.0 , 4) < < "\ n" ;
ifm :: pow ( -2.0 , 9) < < "\ n" ;
//
//
//
//
//
outputs
outputs
outputs
outputs
outputs
0.25
2.25
5
81
-512
return 0;
}
0
2
!
<
"
!
&
1
, ! ,
* %*
H
! *
+
pow
H
double
,
!
std::pow
e
be
std::pow
double pow ( double b , int e)
{
+
H < * * 1
*
! ) * !
namespace ifm {
!
*
A
3.1.9 Using library functions
@
*
# include < cassert >
# include < IFM / math .h >
Program 27:
| } { // math .C
// A small library of mathematical functions.
<* !
<
}
€
*
A ! !
, +
H
// PRE : [ first , last ) is a valid range
// POST : * p is set to value , for p in [ first , last )
void fill ( int * first , int * last , int value = 0) {
// iteration by pointer
!
7
€
* (
6
1
*
A
5
H
H
* 1
!
*
* !
1
!
*
H
,
+
1
A
1
H , H
H
+
*
H H
)
F
H
*
*
!
A
6
H 7
,
1
, *
1
!
*
* *
!
,
* !
*
-
, * 1
+ ! *
6
, !
H * !
, ) -
* * , 1 , ,
!
C
H
A
*
;
* ! ,
C 1
H
1
!
*
!
H
H 7
*
, H
, €
* (
!
<
*
std::sqrt
11
(unsigned int)(std::sqrt(121))
11 121
!
&
! !
- 1 1
& !
*
)*
,
*
! ! !
!
6
* ( !
€ " 7 - C G
! * * * *
*
+
+ 1
+
1 H !
1 *
B 1
*
n
)
F
!
*
, D
, E
H 7
!
*
G
*
std::sqrt
C B * C H !
, 1
1
G
! 1
*
*
d > bound
bound
H
fill
(
Default arguments.
!
*
fill
!
1
!
*
%
!
;
*
!
1
!
*
*
H * * , 1 A !
1
, , @
F
!
) , H std::sqrt(121)
int
!
!
1
, !
*
F IH * * !
1
H # include < iostream >
# include < cmath >
? !
int main ()
{
// Input
unsigned int n;
std :: cout < < " Test if n >1 is prime for n =? ";
std :: cin > > n;
-
// Program : prime2 .C
// Test if a given natural number is prime .
cmath
*
- B
1 1 ,
& ! ,
)
H *
1
*
!
, , 6
, // Computation : test possible divisors d up to sqrt (n)
unsigned int bound = ( unsigned int )( std :: sqrt (n ));
unsigned int d;
for ( d = 2; d <= bound && n % d != 0; ++ d );
double
n
10.99998
10
121
! ,
H
,
#
" *
B , +
* *
*
! H
! * 1
*
!
// Output
if ( d <= bound )
// d is a divisor of n in {2 ,... ,[ sqrt (n )]}
std :: cout < < n < < " = " < < d < < " * " < < n / d < < " .\ n";
else
// no proper divisor found
std :: cout < < n < < " is prime .\ n ";
n
*
n
! ! * *
return 0;
*
* H
H
*
! n }
,
2
H
*
1
{2, . . . ,
d
n
H
!
!
d
d <= bound
Program 28:
"
*
! - *
, * ! H ! ,
1
1 1
H !
*
H
€ H std::sqrt
!
{ {{ { {| } { | } { {
n = dd
!
*
}{
2
-
{ {
{
n
cmath
n % d != 0
for ( unsigned int i = 0; i < n ; ++ i)
crossed_out[i ] = false ;
std :: fill ( crossed_out , crossed_out + n , false );
#include <algorithm>
3.1.10 Details
0
$
$
69
5
74
6
8
7
4
6
5
74
6
4
79
!
, A
!
*
B
1
1
, +
1
1
H
* !
*
1
A
* !
*
&
H "7
A
!
*
HI
!
*
B
?
*
1
*
7
! € B !
*
$
!
*
!
fill_n
7
D
B
E
*
7
A
!
'
'
* B
* &
!
*
)
€7
B
1
)
!
!
*
'
'
1
7
*
6
H %
!
* !
! * H
*
, * * *
!
&
,
!
!
IH A
*
%
- ! *
1
* * *
*
1
HI 1
!
!
1
, !
!
*
1 IH !
! * 1
, 1 * H *
+
!
*
1
1
* !
:
!
*
,
1
*
!
? -
1 *&
H
*
A
* *
! % , ,
1
1 !
! G 1
HI !
*
!
*
*
-
cmath
!
A
!
pow
Operational.
7
6
Dispositional.
10
, 3
H
HI
,
* ,
,
fill
std::abs
std::sin
std::cos
std::tan
std::asin
std::acos
std::atan
std::exp
std::log
std::log10
std::sqrt
B
6
!
*
%
+
1 *
H * *
* H &
1 7 !
float double
3
€
*
* ! ! , *
* 6
1 C !
* ,
1
IH ! * * (
A
* €
7
Mathematical functions.
)
6
*
,
-
k
, !
*
*
3
7
7
& 1 &
* 0 ?
* 3 ! * * 1 *
H & * !
B * , !
! * * IH
& 1 IH *
" B 1 double pow ( double , int );
7
6
1
int a [5];
fill (a , a +5);
// means : fill (a , a +5 , 0)
fill (a , a +5 , 1);
,
3
* ! H ,
!
*
*
, &
H
// PRE : [ first , last ) is a valid range
// POST : * p is set to value , for p in [ first , last )
void fill ( int * first , int * last , int value = 0);
7
6
H
H !
+
!
*
* IH
Function signatures.
k
3
* 1
*
Function declarations and definitions.
,
*
!
C
, math.h
i, i + 1, . . . , k
H
i
E
!
H !
+
! 1 ! * ! A
H * % ! ! * H !
*
! &
" *
& }
6
! *
B 7
!
*
!
, IH A
*
-
HI 1
! !
,
, D & * !
?
E ?
*
D i−1
!
*
* for ( int * p = first ; p != last ; ++ p)
*p = value ;
long double
|x|
(x)
(x)
(x)
−1
(x)
−1
(x)
−1
(x)
ex
x
x
x
3.1.11 Goals
fill
€
$
$
69
5
74
6
8
7
4
6
5
74
6
4
79
69
5
74
6
8
7
4
$
$
6
5
74
6
4
79
7
2
3
6
€3 7
6
#
int main ()
{
int i ;
std :: cin > > i;
h (i );
17
return 0;
# include < iostream >
double f ( double x )
{
return g (2.0 * x );
}
double inverse ( double x)
{
double result ;
if ( x != 0.0)
result = 1.0 / x;
return result ;
}
bool g ( double x)
{
return x % 2.0 == 0;
}
17
bool is_even ( int i)
{
if ( i % 2 == 0) return true ;
}
void h ()
{
2
"
!
0" #
!
6 /
3 €7
6 3 7 #
!
(
#
$
2 0 #
0 "
!
7
6
}
7
1
!
4
!
!
!
!
void h ( int i)
{
std :: cout < < g(i ) < < "\ n";
}
Exercise 82
3 7
int g ( int i)
{
return i * f (i ) * f( f(i ));
}
double g ( int i , int j )
{
double r = 0.0;
for ( int k = i ; k <= j ; ++ k )
r += 1.0 / k;
return r;
}
Exercise 80
6 1
" !
" !
" ! !
#
#
0
" !
"
4
int f ( int i)
{
return i * i ;
}
3 7
6
3
0" #
#
!
1
!
7
int f ( double i , double j , double k)
{
if ( i > j )
if ( i > k)
return i;
else
return k;
else
if ( j > k)
return j;
else
return k;
}
0
2
* * *
6
3 7
1
1
!
H
, # include < iostream >
3.1.12 Exercises
Exercise 79
, A
!
1
, ! * ,
A
1 1
B A IH A
6
"3 7
Exercise 81
i
Exercise 88
sort_array.C
sort_array2.C
$0
3
7
6
"
!
#
# 0 #
"
" 2 1
"
!
3
7
6
5
69
6
8
7
4
6
5
74
6
4
74
79
!
! "
$
$
be
$ 2
)*
"
%
#
.
|e|
#
/
5
# swap.C
.
0
2
#
0" # 2 # $
$ ! ! !
#
2 <
"
# e
! #
"
pow
int
bi
"
i
!
#
!
b2
0
4
#
3 7
6
$
i=0
∞
Y
! Exercise 87
" $
" $
,
be =
#
$
/5
i=0
∞
X
$ #
/
#
"
!
return 0;
// POST : return value is true if and only if n is prime
bool is_prime ( unsigned int n );
!
#
$
$ #
!
Exercise 86
9
e=
"
}
!
Exercise 85
2
int main ()
{
double result = f (3.0);
h ();
"
8
x.
$
6 3 7
!
$ $
3
/
0
$ s(x)
unsigned int
1/2
0
!
+
# #
$
!
(
0
0
0 $
3 0 # $ std :: cout < < result ;
#
"
2
double x
x
7
%
ε,
(
0 0 !
! "
! 2
2 - 0 ! #
! #
"
!
ε
!
{2, . . . , 10000000}
(i, i + 2)
#
$
)
3
#
4
x
std::sqrt
6
#
0
|s(x) − x|
x
-
std::sqrt(
!
"
Exercise 84
!
-
8
Exercise 83
std::pow
! $ (
# ! !
# #
$
3
#
2 - ! # $
2 ( $ . $
$
/ $ #
2 - ! " }
e
bi 2 i ,
.
# include < iostream >
// your function definition goes here
int main () {
// input
std :: cout < < "i =? " ;
int i ; std :: cin > > i ;
std :: cout < < "j =? " ;
int j ; std :: cin > > j ;
// your function call goes here
// output
std :: cout < < " Values after swapping : i = " < < i
< < " , j = " < < j < < " .\ n ";
}
return 0;
i =? 5
j =? 8
Values after swapping : i = 8 , j = 5.
F Q
Q = {0, 1, 2}
. #
**
" 3
2 ! 2
" b
0
(
#
!
.
* 2
# 0 %(
ab
,
9999
0
! *
! 6
7 * €" 7
;
1 , 1
!
! ! *
&
IH
6 3 7 * !
!
*
&
%
*
6
€
7
&
7
# -
9(
.
!
$ # " # 2 ! #
# $
2
#
$# $
! 0 !!
#
! 6 3€ 0
7 1 $
0
&
! 3 0 0
$ 3
/
#
*
2 $ "
$
2
2
2
1
-
#
0
(
6 3 "
7
2
2
2
" # $
! 2
0
3
$
$
2 -
!
" 1
#
! (
!
!
$
(
int
!
a, b < 100
!
#
9(
$ 2 - (
" - 2 : $ #
(
" 1
3
$ 0
$
#
! #
0 0 "
#
5 5 5 2 $
, . # $ !
! 2 2 $
0 ! $ " #
" (
1 #
$
!
#
0 # " #
- $
" 2
76
6
3 7
3
&
% " $
, 2 $
!
#
$ #
" /
!
"
# " %
% "
math.h
0
Σ = {0, 1}
a
0
δ(q, σ) = q
( " 2 - 1
2
22
!
0
3
) $ * # *
) )*
) *+
(
0 0
$ $ #
0 0
3
$ 0
(
# 0
$ 2 .
$ 2 "
math.h
Q
2 -
Σ
s
$
q
Σ → Q.
!
*
& ! a, b
.
power_cross_sums.C
4 0
$ 2
!"
- 2
< .
Exercise 92
http://projecteuler.net/
q
δ:Q
!
Exercise 93
2 .
" 1
(
#
/
!
!
*
2
.
#
2 3
$
0
3 2
#
-
# 3
0
! !"
"
!
/ ! 1 ! $
$ #
(
#
$ 0
# $
#
double
(
4
# 3
2 - # (
! 0
#
$
Exercise 90
#
$ #
0 #
2 0
0
day =? 13
month =? 11
year =? 2007
Tuesday
"
"
$ )
+
0 ! " #
0
0 # #
! " !
"
.
3
$ 7
# 0 " 3 #
# 2 $ #
" $
" #
( !
) - 6 std::sort
7
76
2
$
0
$
#
" ! 3
$
$ 2 - 3
$ $
3
2 (
4
0 "
" "
0$
$ #
0 # 3
std::sort
include<algorithm>
0
"3
3
6
#
"
!
4
! !"
perpetual_calendar.C
!
" ;
2 $
)
)*
#
9(
$ 2 2
.
/ !
"
! "
3
3
"
#
3 0 ' 22
2
0 0
Exercise 89
!
!
$
++p
sort
sort
-
.
! " $
" !"
# !
# 2 3
#
!
-
// PRE : [ first , last ) is a valid range
// POST : the elements *p , p in [ first , last ) are
//
in ascending order
void sort ( int * first , int * last );
math.C
Exercise 91
// POST : return value is the integer nearest to x
int round ( double x );
math.C
round
1, 000
3.1.13 Challenges
Q
σ
$
$
69
5
74
6
8
7
4
6
5
74
6
4
79
L
"
n
$
!
3
! #
$
#
#
$
!
0
}
6
5
#
/
1
c
0 0
1
r
0
4
1
7 4
3 2
9
9 9
0
n
n
2
!
4
9
7 3
5
!
2
6 7
8
$
9
8
-
-
-
{0, 1} | w
2
{0, 1} | w
1
$
$
!
{0, 1} | w
#
7 3
6 5
5
/ Σ
2
.
$ 1 ! 2 #
3
!
2 !
" "
#
$ $ *
0 -
{0, 1} | w
0
2
3
0 0
"
$ /
$
$ " 0 0 "
/ $
#
! " 0
$
3
w =
w
4
/ L = {w
0
3
8
1 H !!
, (
IH !
, , +
1 1 H s
/
L = {w
σ
" w
#
0$
Exercise 94
3
9
/
L = {w
#
-
L = {w
0
)*
# #
!
0 ! !$
2 . 2
.
0 !
$ 0
q = δ(q, σ)
w
$
!
(
1
3
1 ,
2 - 2
2
)
"
&
0
/ (
$ !
1
/ 0
2
*
#
2
0
<
0
$ 4
0
{0, 1}
"
2
1
!
#
L
0
2
#
.
"
(
1
"
"
! 0, 0, 1, 2, 2
Σ
3
$
w
$
0
$
0
$ # ! # 2
$
4
! ! #
$
$ 0" "
/ $ $ $ ! !
* " * 2 (
4 2 * 0 2 . w
"
3
$
#
" :
/
!
" !
(
0
0 0 # (
0
2 #
"
! )
+
0
1
0
!
" q
0101
2
#
1
L
. 2 #
#
" #
0
$ F
#
"
# )
+
" 3 0 !
!
2 /
! "
)
2 +
w
"
2
(
# ! " 2 #
(
0 $ 3
5
1
!
0
2
Σ
0 2 + 1 2 + 0 2 + 1 2 = 5,
3
2
(
.
$ 2 - $ # 4 0
! 0 $ ! / $ !
0
( 0 0 $! 0101
0
q
3
Q\F
w
2
! # # - $
q
- #
$
1
!
"
Figure 18:
4 #
0 0$ "
! $ !
2
" #
0
0
0
!
0 2 - / !
(
0
}
}
}
9
1
n
0
n
n
€
$$
69
5
74
6
8
7
4
6
5
74
6
4
79
6
(
?*
fac
!
n 1
n>1
H
1
n
!
!
$
H !
?*
&
!
*
* :
13
0
$
" ! !
! 2 ( " !
0 0 "
# 2
$
0
! 0 0
!
1
$
# $ 2 0 4 !
0 / ! ( !
#
0
2
$ 0 #
#
# $ $
1
.
2
&
0
0
" 2
0
1,
n (n − 1)!,
H * *
* ! H ,
! *
* , * !
*
2 <
!
) *
*
* !
* '
'
*
* 1
, &
1 & ! &
F
)
H !
$
!
3
! # $! 2 .
" $ !
$ 3
0
!
9
3
( " !
" 3
!
0 $ #
0 0 #
0
"
!
4
# 9
!
*
HI
, *
, Infinite recursion.
fac(n-1)
)
* * , & !
* ! !
! * * , 1 A
*
H
B ! * H *
& * )* !
*
* 1
+
n! :=
)
n
H (r, c, n)
3 9 1
5 6 4
2 8 7
5 6 7
9 2 8
3 1 4
2 8 4
7 3 1
9 6 5
6 2 3
4 1 9
8 7 5
n!
€
<€
7
! * !
*
& *
* <
,
& ! * 8 5 1
6 7 3
2 4 9
4 7 9
5 2 8
6 1 3
7 4 6
1 3 2
9 5 8
1 8 5
4 9 6
7 3 2
0
0
3 9 2
8 5 7
1 4 6
3 0 0
0 6 0
0 0 0
$
0 0 7
0 2 0
0 0 4
0
0 0 4
7 3 0
0 6 5
"
!
.
0 0 0
0 1 0
0 0 5
8
1 $
!
13 8 #
1 $ ! 2
!
.
! ! $! $ 0( $ (
!
" (
! $ $! ! 0
"
8 0 0
0 0 0
0 0 9
/ $ !
" ! 2 #
!
"
4 0 0
0 2 0
0 0 0
" !
" #
!
2 " 3 0 $ 13 !
7 4 0
0 3 2
9 0 0
1 0 0
0 9 0
7 0 0
0
0
0 0 0
0 5 0
0 0 6
8
! 1 $ 0 !
$ $ 2
# ! " !$!
$
0
3
!
#
# ! $ $ !
$ # (
!
!
$ # ! 0
#
!
!
$
sudoku.C
! 0 2 1 !
!
2 #
$ $0 $
2 $ ! 0
$ ! Hint:
81
3.2 Recursion
3.2.1 A warm-up
n!
// POST : return value is n!
unsigned int fac ( unsigned int n)
{
if ( n <= 1) return 1;
return n * fac (n -1); // n > 1
}
€
€
€
69
5
74
6
8
7
4
6
5
74
6
4
79
*
Lemma 1
#
b>0
(a, b) =
H
(b, a
" &
*
(0, 0) := 0
&
*
b).
(a, b)
!
(n, 0) =
#
! 0
0$ "
fac(3)
, 1
*
<
*
,
!
*
*
F
!
*
,
<
-
!
* * ? ! ,
)
* * !
*
* !
,
!
fac(3)
n * fac(n-1)
n * fac(2)
n * (n * fac(n-1))
n * (n * fac(1))
n * (n * 1)
n * (2 * 1)
n * 2
3 * 2
6
1
*
*
.
3
#
!
n
!
H A
&
*
*
1
n: 1
* ;
!
HI
!
*
&
A
? A
0
,
!
A
1
*
*
C
*
*
7
,
6
1
A
A
!
-
H
* 12 * A D
!
E H * A * 12 ! D , E
&
f(3)
a, b
- 1
& !
&
H * *
1 , ! 1
, A
!
&
* IH
* A ! *
* , H ,
!
*
! < * 1
H
H * D 1 ,
E * H
*
, *
n: 2
n: 2
n: 2
n: 2
n: 2
fac
!
! *
H * ? A
A * , * ,
! *
, H 0
*
- * , *
! ,
* !
* * 1 * * *
,
! 0
A
←→
1
!
Greatest common divisor.
(a, b)
a
b
n>0
5 * Table 5:
*
f(n-1)
H
!
2
% 2
- B
, 1
+
* *
, H
!
fac(3)
1
n: 3
n: 3
n: 3
n: 3
n: 3
n: 3
n: 3
n: 3
n: 3
!
*
*
€
<
!
,
*
!
*
E
-
3.2.2 The call stack
$
D
! * ! )*
) *
H ) - * & ,
( , *
!
, ) H H H
!
n-1
H
< ;
3
H
*
H
!
* (
*
!
!
! *
!
*
* *
* D
, E
! !
* * *
+
!
, !
f()
$
-
fac(n-1)
A
*
6
7
1 ;
* *
!
D 1 E ,
* !
f()
H
!
* - * * ! *
, , , 1
!
*
* H 0 A * A
! ,
* H A H A *
,
*
A
3
* * 12
*
fac
/
* * A D
, E
! *
*
**
!
* !
* *
H !
1
2
*
A
f()
- 1
*
*
*
<
*
, ! * 1
* * 2
A !
*
A
H
*
n
return n * fac(n-1);
n * fac(n-1)
n
*
!
!
* * 1 B
*
!
*
6
* !
*
* !
% , H 7
f()
*
A H
n
1
f(3)
f(n-1)
f(n-1)
fac
#
void f ()
{
f ();
}
3
2
1
n
3.2.3 Basic practice
(0, n) = n
€
€
69
5
74
6
8
7
4
6
59
4
8
*
*
b)
b <b
return gcd (b , a % b );
(a, b)
gcd
(b, a
*
b>0
b =
- ! * b
gcd
!
* ! *
&
+
*
!
b<b
gcd
H
fib(n-2)
F45
// POST : return value is the greatest common divisor of a and b
unsigned int gcd2 ( unsigned int a , unsigned int b)
{
while ( b != 0) {
unsigned int a_prev = a;
(a, b) → (b, a
Fn
n
fib(50)
F48
fib(n-1) F47
,
!
*
C
!
HI
,
! * F
F 1 ** &
1 H HI *
* * *
, ! ' ! '
!
*
, F
C
F
H !
C * & , !
*
* *
C
! !
!
1
0
0, 1, 1, 2, 3, 5, 8, 13, 21, . . .
* * F
1
**
1
* 1 !
, *
H H
n = 30
+!
F0 := 0,
F1 := 1,
Fn := Fn−1 + Fn−2 ,
*
1
* HI
, !
Fibonacci numbers.
G
HI
*
* 1
?
H
F46
gcd
!
, *
,
! *
, H * ,
1 ,
*
H
- @
- ,
* *
* * 1
& ! *
1
2
*
, b a
b
+
.
k
k
* !
*
,!
! @ H * *
!
, ) *
* A
H
* *
* H !
)
* // POST : return value is the greatest common divisor of a and b
unsigned int gcd ( unsigned int a , unsigned int b)
{
if ( b == 0) return a ;
return gcd (b , a % b ); // b != 0
}
6
*
IH
* , & IH
H
*
6
* !
7 F
b)
* * A 6
*
7 *
A
)
* A , 1
, B
*
1 - , b
, !
, a
b
*
a
! * 1 - ? * *
, * *
1 H * 1 ,
, !
.
(
*
b
unsigned int
,
!
k
1
*
!
*
b
* a
!
*
HI
!
,
,
a
H
b <b
gcd(b, a % b)
b) =
(a, b)
!
!
0 0
*
b=0
k
gcd
, '
!
b
b
* Correctness and termination.
'
a
(b, a
!
*
*
+
IH , * ,
! H !
) C * , , !
! (a, b)
a
k
unsigned int
,
H **
*
* IH
k
⇔
*
k
!
b)b + a
F
,
*
!
*
, * H
*
-
b/k
*
H 1
* &
* *
!
b
*
b
!
fac
! H 7
7 1
!
, , !
*
!
!
H !
A
* < &
0
A
6
b
b)
1 H
H *
A
6
a
= (a
k
*
*
a = (a
;
&
b
, 3
HI *
*
!
*
*
1
H
, * * , , *
!
6 *
, 7 1
!
1
n!
b
(b, a
* *
F
!
* H
*
F
* H *
, , ( *
* a
k
a
*
a
*
Proof.
n > 1.
// POST : return value is the n - th Fibonacci number F_n
unsigned int fib ( unsigned int n)
{
if ( n == 0) return 0;
if ( n == 1) return 1;
return fib (n -1) + fib (n -2); // n > 1
}
n = 50
Fn
1
Fi , i < n−1
3.2.4 Recursion versus iteration
b)
"
€
€
69
5
74
6
8
7
4
6
59
4
8
H
! *
fib(50)
Fi , i
f
fib2
f
f
unsigned int f ( unsigned int n)
fib
fib
!
-
fib2
fib2
n
fib2(50)
F50
fib
fib2
3.2.5 Primitive recursion
m
n

 n + 1,
A(m, n) =
A(m − 1, 1),

A(m − 1, A(m, n − 1)),
A
20, 000
A(n, n)
A
!
A(4, 1)
A(4, 2)
A(4,3)
*
, *7 +
! H (a, b)
?
3 A(m, n)
265536 −3
,
*
1
, !
*
H * +
, * * , ! , * A
! '
'
!
! *
H +
H ! 1
* ,
, 1 * !
*
, *
, H
2
!
, *
!
*
H *
@
1 ! , , ! 1 * ! , 1
*
!
H B ! )
* ( ,
€ 1
H f(n) = 1
m=0
m > 0, n = 0
m > 0, n > 0
1
1 !
* 3 # , *
H ?
? A
)
! *
* 1
, *
* 1
*
* * )
*
6 * * , 20
!
, *
, !
*
*
+
b
%
, Fn−2
!
&
1
Fn−1
, *
7 % 1 !
*
*
, 1 H * , * *
1 , C
'
* '
!
*
!6
&
A
+
, // POST : return value is the n - th Fibonacci number F_n
unsigned int fib2 ( unsigned int n )
{
if ( n == 0) return 0;
if ( n <= 2) return 1;
unsigned int a = 1; // F_1
unsigned int b = 1; // F_2
for ( unsigned int i = 3; i <= n ; ++ i ) {
unsigned int a_prev = a ; // F_ {i -2}
a = b;
// F_ {i -1}
b += a_prev ;
// F_ {i -1} += F_ {i -2} - > F_i
}
return b ;
}
Fi
a
*
, *
1 !
,
*
!
*
!
*
, F
1
1 *
*
1
! ? *
, H
1
Fn
,
1 ! *
* + 6 * , ) !
*
@ F IH
1
" 1
! ; 1 )
H *
*
@
1 * *
*
$
"
*
A
* 1 C A * !
,
B
6 !
*
H * 7 H , H 1 -1
7 * "
* ,
* ! *
D A , %*
1 E A
!
, H * 1 ! H ?
H *
1 , ,
1
1 < & * ! H *
* %
, *
!
Fi−1
i
fib
f
unsigned int
+
* H * *
,
- 7 1
* 1 * !
* H *
/
H !
,
$ *
* ,
*
H
& )* ! *
, ! 6 *
* ! !
? *
Fi−2
!
a
!
!
*
f
}
return a ;
,
' '
! * A
H
*
* F !
) *
H ! * * !
* *
1
! *
a = b;
b = a_prev % b ;
{
if ( n == 0) return 1;
return f (f(n -1) - 1);
}
}
n
f
Fn
A(m, n)
A
// POST : return value is the Ackermann function value A (m , n)
unsigned int A ( unsigned int m , unsigned int n ) {
if ( m == 0) return n +1;
if ( n == 0) return A (m -1 ,1);
return A (m -1 , A(m , n -1));
}
m=4
€
€
69
5
74
6
8
7
4
6
59
4
8
B
// PRE : [ first , last ) is a valid range
// POST : the elements *p , p in [ first , last ) are in ascending order
void minimum_sort ( int * first , int * last )
{
*
0
#
n
!
!
!
, !
*
& *
)* ! IH
1
& !
* 1
+
while
n−2
** +
HI *
)
*
H
+
H
* *
? ? )* *
! **
*
1
*
)*
,
*
D
, !
* H
!
! * H - * *
!
!
1
* 1 , A A ) C
*
H # *
6 H
)
*
€ 7
@
A
!
, * C
*
H !
E
! ;
? !
A
*
*
,
H
I
C !
*
&
*
!
%
!
*
1
B
) *
1
A
!
-
, !
minimum_sort
C
1
H &
H , *
H
A
1
)
*
+
HI
*
1
1
, H !
!
*
H
, 1
!
*
(
C
*
, !
F 6
) 1
IH
)
* IH * & *
, 7
*
*
1 ! * ! ,
1
* 1
! B , !
* *
! *
*
,
!
* 12
*
!
*
1
-
std::iter_swap
1
2
#
*q < *p_min
.
minimum_sort
* !
!
n
1
*
*
H
1 + 2 + ...n − 1 =
0
!
$ minimum_sort
n
p_min = q
++q != last
F 1
!
!
1
H
*
C
!
* *
?
!
, Observation 1
*
c1 , c2
c1 n(n − 1)/2
n
! * *
* &
C ! *
! 6 *
) H ;
* *
? !
,
1
*
*
*
* * ,
!/ 3
#
n+3
1
)
*
B
, * H 7
, )
C ! C !
IH *
* ) *
1
! ! A
H *
) , !
1
1 +
* &
*
F 1 ** ) 7
#
2
.
[first, last)
!
D
, 1
E
* (
€
* (
€
H
, !
3.2.6 Sorting
*
*
C
B
!
!
1
6
..
2| 2{z } −3
/ −3
1
0
HI
* ,
65536
€$
4
8
€
69
5
74
6
8
7
4
6
59
n
n+1
n+2
2n + 3
2n+3 − 3
*
, 3 C ) * * , ! -
6 A *
H 7 A
!
1 & 1
4 13 65533 265536 − 3 22
&
Table 6:
3
4
5
9
61
-
&
! C C
* *
1 ; H H < 1
2
3
4
7
29
H
*
C
! 1
2
3
5
13
m
0
1
2
3
! *
H
1
* %
1 C
! *
*
1
1
n
0
1
2
3
5
for ( int * p = first ; p != last ; ++ p ) {
// find minimum in nonempty range described by [p , last )
int * p_min = p ; // pointer to current minimum
int * q = p ;
// pointer to current element
while (++ q != last )
if (* q < * p_min ) p_min = q;
// interchange * p with * p_min
std :: iter_swap ( p , p_min );
}
}
std::min_element
minimum_sort
n
n−1
while
n(n − 1)
2
int* q = p
n(n − 1)/2
n(n − 1)/2 + n
c1 n(n − 1)/2 + c2 n
c2 n
#
Gcomp=
merge
// PRE : [ first , last ) is a valid range
* ,
!
*
!
* * 7
1
A !
/
*
IH A
H
!
1
1
!
7
@
HI
*
H
*
1
1
"
H 1
A
IH 1
1
A
H
*
?
1
1
H
1
, !
*
, 1
H
* %
*
* - !
1 ! * 1
* !
) 1
C
*
C
H
1
- *
H
!
,
1
1 ! 6 ! * * 3 7 !
* , 3 * 1 *
!
IH !
*
Time
C
!
1
, 3
A
H H 1 H * 1
F H HI *
,
, ! n
1
- ,
C
H
& * 1
!
! 3
*
6
H ? 7
A
HI *
*
10−9 n(n − 1)/2 Time
sec/Gcomp
H
* 1
1 , "
1
1 1 < !
1
6
, -
A
+
#
?
!
#
*
! * 1
C
H ! *
1 (
*
* !
* * ! 1
1
!
!
1 H !
<
-
<
<
"
€
< €
<
<
H €
"
H H
H
€ €
H € H n
Gcomp
Time (min)
sec/Gcomp
1
, * A
H
H
*
+ ! H 7 * A
1
1 *
* , , 6
* 1 A ? ? *
Table 7:
, 1
,
@
IH
!
, 1
H
A
D *
)
*
E H
2 1 C !
* % *
Tcomp
!
,
*
F
$
A
A !
0 1
1
A
* - * A *
*
, A !
*
) *
Tcomp
A
Merge-sort.
!
* // sort into ascending order
minimum_sort ( a , a+n );
// create sequence : 0 , n -1 , 1 , n -2 ,...
for ( int i =0; i < n ; ++ i)
if ( i % 2 == 0) a[ i ] = i /2; else a[i ] = n -1 - i /2;
// is it really sorted ?
for ( int i =0; i <n -1;++ i )
if ( a[ i ] != i ) std :: cout < < " Sorting error !\ n";
*
* * A * A
!
!
< *
* IH <
* H A , ! * ! 1
A ! * ! B 1 -
6 1 *
A &
* , A * ! A ;
std :: cout < < " Sorting " < < n < < " integers ...\ n";
delete [] a;
*
* B 7
6 int main ()
{
int n = 100000; // number of values to be sorted
int * a = new int [n ];
return 0;
H
n
" 1 * 1
'
'
H
!
,
*
* C * !
* ! ,
,
!
main
0, n−1, 1, n−2, ...
}
,
!
Ttotal
*
C
+
n Gcomp
IH 1
!
? 3
109
, minimum_sort
0, 1, . . . , n − 1
1
6 * H 7 ** F
, ,
1 , !
H
!
1 *
H
1
B **
D*
, 1 E
! Tcomp
%
%
-
n
*
Ttotal
Gcomp
sec/Gcomp
€
€
€
69
5
74
6
8
7
4
6
59
4
8
*
T (0) = T (1) = 0
T (2) = 1
1
*
!
0
A
* , !
*
* H
&
*
!
* 1
!
*
* A 1
! * C
! *
merge
0
1
n
#
n
+ * * A
!
, 1 7 !
*
1
6
* !
!
! , *
* !
merge
)
*
* * *
A ! * ) A * ;
1 !
H *
1
&
! !
! 6
* 7
1 *
*
,
H
%
! %
*
H *
F " 1 H * ! 1
!
H H 1
A 1
%
!
** 1
H C * *
! 1
1
!
1 *
*
C
//
//
//
//
1
1
3
* right ++;
* left ++;
* left ++;
* right ++;
*
!
2
merge_sort
=
=
=
=
A
#
*
A * A
+
H & ,
* * *
A / ) ; *
! !
! !
#
3
*d
*d
*d
*d
* 1
/
* A
* 1
A
H 7 A 6
! * &
**
- 1
!
*
* if
( left == middle )
else if ( right == last )
else if (* left < * right )
else
n
T (n)
2
(n − 1)
0
.
B
Analyzing merge-sort.
6
&
Theorem 2
Proof.
n/2
0
5
* 1
A * C
*
!
merge
HI *
,
*
!
*
!
9
" "
4
3
2
1
!
x
!
*
[first, last)
n/2
x x
// PRE : [ first , middle ) , [ middle , last ) are valid ranges ; in
//
both of them , the elements are in ascending order
void merge ( int * first , int * middle , int * last )
{
int n = last - first ;
// total number of cards
int * deck = new int [ n ]; // new deck to be built
x
// POST : the elements *p , p in [ first , last ) are in ascending order
void merge_sort ( int * first , int * last )
{
int n = last - first ;
if ( n <= 1) return ;
// nothing to do
int * middle = first + n /2;
merge_sort ( first , middle ); // sort first half
merge_sort ( middle , last );
// sort second half
merge ( first , middle , last ); // merge both halfs
}
int * left = first ;
// top card of left deck
int * right = middle ; // top card of right deck
for ( int * d = deck ; d != deck + n ; ++ d )
// put next card onto new deck
H n/2
4
6
merge_sort
! C
*
F ) * H *
*
*
H H
* * !
!
! ! H " 1 H +
x
[middle, last)
x x
*
3
5
+!
4
6
n/2
merge
n/2
[first, middle)
2
3
5
!
*
n
H
H E7 H !
1
H
C D 6 C
6
E7 D
Figure 19:
*
+
+! % ,
1 H ) 1
, *
, C
H
*
H 1
&
1
, 1
4
6
*
2
3
5
H
!
*
1
4
6
H
A * A
*
* IH A
* , ,
& 1
! ,
+
!
1 ,
! * 1
2
1
3
2
1
left deck is empty
right deck is empty
smaller top card left
smaller top card right
// copy new deck back into [ first , last )
int * d = deck ;
while ( first != middle ) * first ++ = * d ++;
while ( middle != last ) * middle ++ = * d ++;
}
delete [] deck ;
n
n
n−1
n
2
1
€
€
69
5
74
6
8
7
4
6
59
4
8
&
1
,
* A 1
! - H 1 * < H H
H " H H €
€
!
* * 1
& +
* IH ! ;
3 * -1 H 1
!
H
sec/Gcomp
2
n
6
minimum_sort
sec/Gcomp
n
n = 51, 200, 000
Σ = {F, +, −}
Σ
F + F+
Σ
P:Σ→Σ
P
*
2
* *
*
(n − 1)
?
n = 1, 600, 000
minimum-sort
merge_sort
n
*
merge_sort
(n − 1)
2n
minimum_sort
n
* minimum_sort
n
!
F
1 1 1
H *
,
H
* 1
H
*
1
*
* 1
H
H
1
1
*
1
* !
* 1
*
C
* *
;
* ! *
*
2
n(n − 1)/2
, A !
1 -
!
,
! - +!
* 1 H H A
1
, H * * ! * !
1 ! n
(n − 1)
!
, ?
*
!
1
#
#
!
7
6
* -
Table 8:
! * 1
IH
1
* 6 A 1
*
* !
3*
H
:* 1 7 !
*
7
H
F
H
<
!
*
*
, *
/ C
! A
*
!
* A ! * 7
* +
! * ! H * * *
*
7
H 1 ,
* *
) 6 , &
* 1 1 & A -
* "
H " " "
€ €
H " H
$ H € " "
H €$
" €
H n
Mcomp
Time (sec)
sec/Gcomp
merge_sort
) *
* ! H
* H A *
* H
*
, * *
? ? merge_sort
!
* B H ! 1
1 H
! 1
* ,
! *
3*
! * 1 * ) + 1 77
!
6
n = 1
C
6
HI ! *
* C
*
- !
1 ! * *
C
! ? )
1
2
6
6
*
, 7
*
, 1
- ! !
1 6 1 H !
*
* ! 1
!
*
n
&
*
* 7
)
*
&
!
7
T (3) = 2
H 7
6
1
*
1
* H *
*
H * * !
! 1 !
;
n/2 , n/2
+
n=1
F
)
T (n)
merge_sort
n−1
! 1
*
,
HI *
1 1 !
+
!
1 T (n)
H
H
! !
2
€
H
* *
, ,
*
!
!
* , (
*
F "
1
, ?
2
n
n
T(
) + T(
) + n − 1.
2
2
H 7
**
H
n
)
* * * A
! 1
n
n
T(
) + T(
)+n−1
2
2
n
n
n
n
(
− 1)
+(
− 1)
+n−1
2
2
2
2
2
2
n
n
(
− 1)(
− 1)(
2 n − 1) + (
2 n − 1) + n − 1
2
2
n
= (n − 2)(
n
−
1)
+
n
−
1
n
=
+ n2
2
2
(n − 1)(
n
−
1)
+
n
−
1
2
= (n − 1)
2n .
6
*
, 7
F
HI
, F
T ( n/2 )
merge
106
Time
*
*
T ( n/2 )
*
! !
!
1
,
2
C
H *
*
1
!
T (1) = 0 = (1 − 1)
n
2
{1, . . . , n − 1}
!
1
%
T
:
*
!
T (n)
1
merge_sort
Mcomp
Mcomp= 10−6 (n−1)
*
( * * * 1
* , A
* /
!
1
T (n)
H
2
n
% ! 0
)
1 H H
H & &)
*
0
) &
!
*
*
,
! 6
!
)
,
! * ,
1 , 0 ! ,
& * * * 1
!
C
!
! 1
H IH )
!
* 1 )
*
A7
*
T (3)
3
n
n
n
n
n
n
n
1, 600, 000
n(n − 1)/2
50
minimum-sort
3.2.7 Lindenmayer systems
Σ
Σ
"
€
€
69
5
74
6
8
7
4
6
59
4
8
* - , F
* * €
6
1 ! C H 7
* * !
1
0
!
*
Turtle graphics.
F +
−
F €
6 !
7 - * * !
H F
90
{F, +, −}
, * A
{
−
p
+
90
F + F+
// POST : the word w_i ^F
void f ( unsigned int i )
if ( i == 0)
ifm :: forward (); //
else {
f(i -1);
//
ifm :: left (90);
//
f(i -1);
//
ifm :: left (90);
//
}
ifm
1
forward left
# include < iostream >
# include < IFM / turtle >
is drawn
{
F
w_ {i -1}^ F
+
w_ {i -1}^ F
+
turtle
€7
6
H
!
P i (+) = +, P i (−) = −
&
1
? !
1
1
6
? !
1
1 H 7
- - * * *
)
(
!
+ → +, − → −
// Prog : lindenmayer.C
// Draw turtle graphics for the Lindenmayer system with
// production F - > F +F + and initial word F .
!
A
* 1
HI -
σk
i
!
1
) H ? !
, 1
! ) !
1
**
* +
! w2 = wF2 = F + F + +F + F + +
!
wi−1
P(σ) = σ1
right
*
* 6 * ?A F
F
H
wσi
&
wσi
1
σ1
Σ
1
k
wσi−1
, . . . , wσi−1
wσi
σ
- *
!
1
H 1
* *
* *
!
σ
(
*
!
-
σ
D
, E
*
, H
+.
−
wFi−1
+
)
*
!
" !
#
#
.
σk
!
=
wFi−1
1
!
$€ H !
A !
1
) !
*
σ1
wFi
F + F+, + → +, − → −
+
1
wσi = wσi−1
wi =
H *
*
!
!
1 0
H *
, <
*
wi
-
s=F
wσi
+
!
*
H
*6
7 *
* A
* 7
F + F+
*
p
F,
F+F+
F + F + +F + F + +
F + F + +F + F + + + F + F + +F + F + + +
wi−1
1
Σ
s = w 0 , w1 , . . .
: H
!
)
$
! $
- (
*
) ! s
- F
& H
C ) &
*
!
* 0
= (Σ, P, s)
,
wi−1
H 1
1
H )
+
→ P(σ)
→ F + F+
→
+
→
−
H E
=
=
=
=
..
.
+
w0
w1
w2
w3
)
D
-
σ
F
+
−
* H
1
Figure 20:
p
} { | } *
* A ! E - 1 H
H
- * ! *
H E D
1 C ,
* *
! * D !
C * * H E
* *
A A
1 IH H
1 1 ! ) D
* ) *
H ! ?
*
!
Recursively drawing Lindenmayer systems.
σ
i
w+
i = +
i
σk
(i − 1)
k
wσi−1
.
wσi
wi
s
F →
€
€
69
5
74
6
8
7
4
6
59
4
8
{ { {
}{
// draw w_n = w_n ( F)
f(n );
return 0;
<
$
6
€
7
, !
!
!
*
-
0
*
90
n=5
is drawn
{
F
w_ {i -1}^ F
w_ {i -1}^ F
++
w_ {i -1}^ F
w_ {i -1}^ F
int main () {
std :: cout < < " Number of iterations =? ";
unsigned int n;
std :: cin > > n;
&
!
) *
G A ! 1
/
!
Additional features.
α
!
w∞
# 1 #
<$ ,
-
w∞ = w ∞ + w ∞ + .
// POST : the word w_i ^F
void f ( unsigned int i )
if ( i == 0)
ifm :: forward (); //
else {
f(i -1);
//
ifm :: right (60); //
f(i -1);
//
ifm :: left (120); //
f(i -1);
//
ifm :: right (60); //
f(i -1);
//
}
}
{
C
-
!
H
* n=∞
// Prog : snowflake.C
// Draw turtle graphics for the Lindenmayer system with
// production F - > F -F ++F -F , initial word F ++ F ++ F and
// rotation angle 60 degrees .
# include < iostream >
# include < IFM / turtle >
{ { { }{ { { {{ { {| } { | } , H
1 * !
*
*
B
% H * n
%
{
!
2
"
H
*
n = 14
F
!
Program 29:
{
}
// draw w_n = w_n ^ F ++ w_n ^F ++ w_n ^F
f(n );
// w_n ^ F
69
5
74
6
8
7
4
€
€
€$
6
59
4
8
int main () {
std :: cout < < " Number of iterations =? ";
unsigned int n;
std :: cin > > n;
{
{ {{ {
{|
}
// POST : w_i ^ X is drawn
void x ( unsigned int i ) {
Lindenmayer systems.
#
1
! %
!
1
* H 7 6 1 * 1
7 1
*
2
"
<
"
!
Program 31:
* !
H A
,
H
26
!
int main () {
std :: cout < < " Number of iterations =? ";
unsigned int n;
std :: cin > > n;
}
*
1
0
H
H
H F
)
*
1 G
)
1
H
*
* )
B 1
!
1
! F
)
H -
2
<
3
0
"
!
} { |
| } { | {
{ { { }{ { { {{ { {| } {
7
6
H
//
//
//
//
//
// necessary: x and y call each other
f
]
if ( i > 0) {
x(i -1);
ifm :: left (90);
y(i -1);
ifm :: forward ();
ifm :: left (90);
}
// Prog : dragon .C
// Draw turtle graphics for the Lindenmayer system with
// productions X - > X+ YF + , Y - > - FX -Y , initial word X
// and rotation angle 90 degrees
# include < iostream >
# include < IFM / turtle >
F H
90
0
X
6
2
1
*
A
H 1
7
+
* *
Σ
void y ( unsigned int i );
w14
"
Program 30:
*
return 0;
0
* * 6
* ?
*
7
, !
*
}
{ Σ = {F, +, −, X, Y}
++
w_n ^ F
++
w_n ^ F
}
X → X + YF +
Y → −FX − Y
//
//
//
//
0
$6
€
=
$
7
$
"
| ifm :: left (120);
f(n );
ifm :: left (120);
f(n );
w_ {i -1}^ X
+
w_ {i -1}^ Y
F
+
}
// POST : w_i ^ Y is drawn
void y ( unsigned int i ) {
if ( i > 0) {
ifm :: right (90);
// ifm :: forward ();
// F
x(i -1);
// w_ {i -1}^ X
ifm :: right (90);
// y(i -1);
// w_ {i -1}^ Y
}
}
// draw w_n = w_n ^ X
x(n );
return 0;
[
F
3.2.8 Details
€
€
€
€
€
69
5
74
6
8
7
4
6
59
4
8
-
(
-
(
-
(
(-
4
mccarthy(99)
mccarthy(91)
mccarthy(101)
mccarthy(100)
#
2
#
*
)
1
"
<
$
:*
#
0" $A F * 2 #
#
n > 100
n 100.
9
! #
mccarthy
#
"
n − 10,
M(M(n + 11)),
0 #
#
M(n) :==
=
3 <
0 3
#
"
0 #
#
7
6
*
*
7
, A
, !
*
!
6
3 7
B *
, !
*
*
H *
, !
!
1
*
6
"3 7
, !
*
,
!
*
B
*
*
*
3
7
6
, !
*
, !
B
*
, 6
3 7
17
&
? !
*
&
'
'
!
*
, !
*
*
B *
!
*
*
* ,
,
3
6
€7
B *
, 6
3 7
*
, !
*
B
&
?
*
!
, 3
7
6
1
1
H
* +
7
3
€7
6
0
*
!
"
#
#
0" #
$
0
# 2 $
/
!
" #
!
# %
*
3
2 2 2$
!
€7
B * !
*
, !
*
*
1
7
Exercise 96
=<
Exercise 97
!
3.2.10 Exercises
%
void g ( unsigned int n )
{
if ( n == 0) {
std :: cout < < " *";
return ;
}
g(n -1);
g(n -1);
}
bool f ( int n)
{
if ( n == 0) return false ;
return ! f (n -1);
}
2
1
!
Exercise 95
" 7
Operational.
-
!
*
7
B ! *
*
!
*
H
A
&
H %
Dispositional.
-
17
*
7
3.2.9 Goals
unsigned int h ( unsigned int n , unsigned int b ) {
if ( n == 1) return 0;
return 1 + h ( n / b , b );
}
unsigned int
unsigned int f ( unsigned int n)
{
if ( n == 0) return 1;
return f( f(n -1));
}
// POST : return value is the Ackermann function value A (m ,n )
unsigned int A ( unsigned int m , unsigned int n ) {
if ( m == 0) return n +1;
if ( n == 0) return A (m -1 ,1);
return A(m -1 , A(m , n -1));
}
unsigned int f ( unsigned int n , unsigned int m)
{
if ( n == 0) return 0;
return 1 + f (( n + m ) / 2 , 2 * m );
}
€
€
€
€
69
5
74
6
8
7
4
6
59
4
8
2 -
" // PRE : [ first , last ) is a valid nonempty range that describes
//
a sequence of denominations d_1 > d_2 > ... > d_n > 0
// POST : return value is the number of ways to partition amount
//
using denominations from d_1 , ... , d_n
unsigned int partitions ( unsigned int amount ,
unsigned int * first ,
unsigned int * last );
2
# 1
)
!
0
8
<
!
0
$
partition
Exercise 102
x
"
0 #
3 7
6
!
/
#
3
2
#
!
!
3
$
!
"
!
!
-
4
" 2 .
!
"
2 "
# !
!
# !
"
"
! /
2
#
!
0$ d
n
#
2 $
4 $ #
$ 6 3 7 6 0
3 7
!
0
#
k=1
#
0 $
# # Exercise 101
d=2
2 - 0
"
!
"
#
n<k
n=k
k=0 ,
n > k, k > 0
2
3
$
/ $ # " " #
" # 0
0
$
!
!
2 0 9
n<k
n k, k = 0
n k, k > 0
#
=<
=
Exercise 100
1
1
.
#
0" #
# (
#
n!
n
:=
,
k!(n − k)!
k
#
$
, n, k
.
4
Exercise 98
7
#
2
7
3 7
6
3
6
#
# 2
$
"
0
$
6
)
0
// PRE : [ first , last ) is a valid range , and the elements *p ,
//
p in [ first , last ) are in ascending order
// POST : return value is a pointer p in [ first , last ) such
//
that * p = x , or the pointer last , if no such pointer
//
exists
int * binary_search ( int * first , int * last , int x)
{
int n = last - first ;
if ( n == 0) return last ;
// empty range
if ( n == 1)
if (* first == x)
return first ;
else
return last ;
// n >= 2
int * middle = first + n /2;
8
8
<
<
# 0
$
3 7
# < 8 0$
$
!
"
!
!
$
9
$ !
< )
# $ " 9
<
$ * )
/5
! 0$
0
n
3
6
7
3 7
6
3
6
-
n
k
0 " (
0$
0$
# ! $
# 2 " #
$
! ! " 0
$
0 ! 4
#
$ "
,
( !
$
0
" !
"
#
0
-
n−1
k−1
(20), (10, 10), (10, 5, 5), (5, 5, 5, 5)
2
"
$
!
!
!
+
( $
(
3 3 - 0
$
) $0
2
. 2 $
2 ) !
2 ( < " - 8
2 . ) ) #
< 8
2
)
k

 0,
n
1,
:=
 n−1
k
0
! /
#
Exercise 99
# $
!
# 0 #
"
n−1
k k−1

 0,
n
1,
:=
 n
k
M(n)
k
d
k
12 13 14 15 16 17 18 19 21 31 41 51 61 71 81 91
There were 16 possible codes .
unsigned int f ( unsigned int n)
{
if ( n <= 2) return 1;
return f (n -1) + 2 * f(n -3);
}
100
-
"
€
€
€
€
69
5
74
6
8
7
4
6
59
4
8
X → X + Y + +Y − X − −XX − Y +
Y → −X + YY + +Y + X − −X − Y.
n=5
n
n = 555
%
3 7
6
2 n = 2
$
!
.
$
!
#
$ #
8
3
3
2
.
0
2
-
(
0
0" #
!
0
$
" !
3
!"
2
#
"
.
. 3
#
3
!"
$
" "
3 $ 3
!
2
/
#
# !"
#
"
$ (
.
!
!
! ) !"
" 0 " #
3 # 0" 2
3 .
3 0
"
!"
#
0
!
#
.
8
0
.
#
2
n=3
!
1, 2, 3
(
!
1
!
#
#
2
n
2
3 5
'
+
! " 3 # !
!
3 !
(1, 2)(1, 3)(2, 3)
$
!
# $ !
$ 2
!
<
" 3
# #
$
(1, 2)
(
# 4 # !
" " 3# !
!" 2
/
/ ! " 3#
! "!
#
3
!" !"
#
( (
- 2 /
1
3
0
! "
!
$
2
hanoi.C
n
$
Exercise 106
'
Figure 21:
3
2
4
$ " ! !
# " 3
$
4
}
$
$ " " ! ! " 3 2 8 0 ! # $
3
"
! " 3 $ 3 $ $ 3 3
$ 2
:
" 6 0
3 7 "
#
#
"
0 !
"
!
Exercise 105
2 - 4 #
$ 2
" (
# ! $3
"
!
" 3
# Y
P
#
X
F → FF + F + F + F + F + F − F.
, 1
(Σ, P, s)
"
α = 60
Σ = {F, +, −} s = F + F + F + F
"
!
n − 1.
, 1
2
2
H
Σ = {X, Y, +, −} s = Y
H
H
=
2
$ $
/ 5# ! ! #
)
2 - !
#
#
"
#
! 2 ( $ 6
"3 7 $ .
T (n)
4
n
2
n
n
4 " ! " # 3
!
# 5 '
! +
0 "
3
2 8
0 0!
#
!
!
" 3
! #
$ (
X → Y+X+Y
Y → X − Y − X.
$
2
T (n)
6
"3 7
x
H
!
E
F
:
n
2
7
Exercise 104
D
2
,
17
Exercise 103
A
0
7
H 17
1
*
*
if (* middle > x ) {
// x can ’t be in [ middle , last )
int * p = binary_search ( first , middle , x );
if ( p == middle )
return last ; // x not found
else
return p ;
} else
// * middle <= x ; we may skip [ first , middle )
return binary_search ( middle , last , x );
3
(1, 3)
(2, 3)
2
P
3.2.11 Challenges
763
€
€
€
€
69
5
74
6
8
7
4
6
59
4
8
2
jump
ifm :: forward (2);
ifm :: save ();
ifm :: left (45);
$
f
2
0 2 " [
]
! $
/
#
0
p
FF[+F][−F]
0
3 0
3 0
3 ! "
$
0 0
! 0 "
!
3 " " !
# "
0" # ]
Σ = {F, +, −, [, ], f}
" 2 !
&
3
2
#
0 $
"
$ ! !
!
!$
!
! "
" $ #
(
$ #
!
!
$#
!
" -
#
2
! ( .
$
forward
0
"
!
0 ! $ # " $
: # 2
0
# !
0
$
$
:
-
(
0
3 0$ 0 $ " ! # " ! $ "
$
2 2
& # ! $ ! 0
$
$
3 8
7
4
5
6
69
€
€
€$
6
59
4
8
!
74
ifm :: forward ();
ifm :: restore ();
ifm :: save ();
ifm :: right (45);
ifm :: forward ();
ifm :: restore ();
2 - 0
$
# " 0$
# 0 0
(
!
# " 0 $
0
0
8
$ #
0
# / 0" " 2
0
[
# !
!
3 #
*
# A
0
! # $ !
$
2
.
3 $
!
#
2 3
.
Σ = {F, +, −, [, ], f}
$
# ! " #
2 2
"
"
0 /
FF[+F][−F]
0
0
$
$ #
" !
#
.
$
!
3
3
$
!
#
" "
0
!
0
! $
/
.
45
turtle
restore
save
f
F
22
# 0 "
!
{F, +, −}
2!
#
2
/
# 0
F → FF + [+F − F − F] − [−F + F + F]
F
#
0
0
Figure 22:
0
#
$ 3 Exercise 107
# !
2
2 " #
8
f
0
Note:
1
amazing.C
€
! *
/ H *
* , *
! !
%
*
HI * ,
, +
, H
* ! 1
! * ! 1 IH 1 , 1 ,
'
! '
, int
H E 1
* H
1
D
!
*
-
1
1
1
*
* *
H
1
!
!
H
(
2
,
!
3
" 0$
!
$
! 0 ! 0
! 0
1 0
! *
# $ "
2 !
0 0
0$
<
5
'
!
"
!
#
*
)
+
- +
=
=<
(
Compound Types
)
n
* <
* 1
H !
& 1 !
, 1
? * G
d
1
*
n
7
!
*
A ) 6
* (
* €
"
7
B +
H
! * A
! * !6
'' *
1
n/d
&
!
*
* A
A
1
2
1
1
!
" !$
1
Chapter 4
*
,
/
'
'
$#
( !
!
$ # $ ! - 1
# %
'
1
% ' '
1 ""
"
%
4.1 Structs, or Plain Old Data
d
// input
std :: cout < < " Rational number r :\ n";
rational r ;
std :: cin > > r;
std :: cout < < " Rational number s :\ n";
rational s ;
std :: cin > > s;
// computation and output
std :: cout < < " Sum is " < < r + s < < " .\ n" ;
€
€
8
7
6
8
7
9
5
45
4
!
int
#
* &
H
A
A
H
!
&
1
*
1
!
!
!
0
A
!
!
-
H *
1
! * A
A B
H )
0
!
A
* ?
1 * ,
*
B !!
*
6
(
*
H H 7
, ! !
}
*
!
C
, ! 1
( *
!
,
* H *
1 * !
*
1
{ H
,
!
(
*
H
,
,
*
C
H
A
+
'
'
*
H
&
c
+
*
& ,
<
!
) &
1
!
!
& * * ! * ' '
, 1 A
rational
(n, d)
&
H
int
*
-
&
,
1
rational
1
1 H
,
,
1
* 1
A
, H !
1 ! &
,
$ !
1
!
d
**
1
!
n
!
A +! !
*
, *
!
6 *
!
1 7 *
1 * 2 ! !
* 6
1 ** *
*
1
7
D
E
A struct defines a type, not variables.
rational
int
B
d
// output
std :: cout < < " Sum is " < < t.n < < "/" < < t.d < < " .\ n ";
// computation
rational t = add (r , s );
* < H &
* & IH 1 H *
!
* !
- std :: cout < < " Rational number s :\ n";
rational s;
std :: cout < < " numerator =?
" ; std :: cin > > s.n ;
std :: cout < < " denominator =? " ; std :: cin > > s.d ;
return 0;
&
H * D
7
E
1 !
H 1 ! !
*
* 6
H *
!
}
!
int main ()
{
// input
std :: cout < < " Rational number r :\ n";
rational r;
std :: cout < < " numerator =?
" ; std :: cin > > r.n ;
std :: cout < < " denominator =? " ; std :: cin > > r.d ;
{| } { | {
// POST : return value is the sum of a and b
rational add ( rational a , rational b)
{
rational result ;
result . n = a.n * b .d + a.d * b.n;
result . d = a.d * b .d;
return result ;
}
<
2 "
{{
// the new type rational
struct rational {
int n;
int d ; // INV : d != 0
};
Program 32:
!
| } { | {
{ { { }{ { {
// Program : userational. C
// Add two rational numbers .
# include < iostream >
int int
n/d
d=0
int\{0}
// INV:
add
struct rational {
int n ;
int d ; // INV : d != 0
};
int n ;
int d ; // INV : d != 0
n
add
74
79
€
€
5
7
8
7
6
8
9
5
45
4
7
65
9
>
/
ti
* 1
*
!
n=
$
! , 7
!
7
*
!
H
* *
; *
!
A
1 1 ! !
& C
! 1
1
* 2
B * !
*
H !
*
!
!
*
*
H
!
*
*
&
*
- * ) <
H * , 1 * &
struct extended_int {
// represents u if n == false and -u otherwise
unsigned int u ; // absolute value
bool
n ; // sign bit
};
−u
{−U, −U + 1, . . . , −1, 0, 1, . . . , U − 1, U}
7
!
* *
H
!
*
<
*
!
* !
*
!
!
! * ! * !
*
-
H ! ) * * 7 ( 1
* 1 6
, ,
1
H
* struct rational_vector_3 {
rational x;
rational y;
rational z;
};
*
n=
0, 1, . . . , U
int
, ,
!
1
&
IH
1
, 3 ? &
F
, 1 H ?)
E
D
) rational
u
*
H
*
, *
! *
3
%
{0, 1, . . . , U} {
(u, n)
*
,
!
%
1 !
!
) &
< H +! A
)
* , < ! 7
H H 7
6
3
?
, +
H ,
!
7
-
6
7
*
, 1 1
* , 1
1 * A F
)
H &
H
H
6
H ! $
" - $ , !
7 7!
7!
7
&
7 7
...
H
unsigned int
ti
!
(t1 , t2 , . . . , tN )
N
!
*
1 7 !
* ! * ) ** )
-
1 6 , 6
, ,
! , !
* / & < ,
, D
H
E
H
! !
1
A
6
! * !
H !
7
! *
) *
6
* 1
&
1 H
H 7
6
H €
$
H 7
7
H * (
B
B6
6
7 B
7
B
7
7
struct
,
&
* C *
! * *
& 1
*
, H 7
&
, ! 1 ) * IH
, ) 1
+
&
A
!
*
H 4.1.1 Struct definitions.
struct S {
};
struct T {
};
void foo ( S s ) {}
int main () {
S s;
T t;
foo ( s ); // ok
foo ( t ); // error : type mismatch
return 0;
}
3
struct rational_vector_3 {
rational v [3];
};
4.1.2 Structs and scope
struct
}
4.1.3 Member access
"
€
€
7
8
7
6
8
9
5
45
4
7
65
5
74
79
9
// 1
0
extended_int
? (0,
1
H
, t = (t1 , . . . tN )
)
- <
A
1
2/3 = 4/6
rational
(n, d)
t = (t1 , . . . , tN)
K = 1, . . . , N
& *
*
F
,
!
7
H
+ )
! !
!
C
==
1
H
€
7
+
)
1 H
? ,
!
add (r, s)
int int
* *
*
! ! ,
H
!
*
IH *
? 1
!
*
!
add
, 1 )
*
* +
,
*
t
H A
! * * H , H !
*
tK = t K
rational
What about other operations?
!
*
! !
! ,
6 !
7
1 2 * B
,
!
s
H
t
C
! H ! 1
*
! <* !
A
r
,
H 1
? C
!
1
,<
, *
1
D E H add
IH
H
* , 1
**
" "
!
! ! a
b
add (r, s)
return
*
+
!
6 1 !
H 7 !
q
* , *
1
,
1
)
(0,
t=t
1
'
'
F *
*
H H , #
! ! 1 B ! 1 ! &
H & !
IH ? ! !
? ! ? 1
6 +
H !
*&
*
1
*
H
F
HI *
*
&
C
7
)
1
%
$
-
6
!
A
H A
*
* 6
* 1
!
* 1
7 * 2
*
* *
*
DA E
!
)
+!
?
?
*6
*
> H H
! H 7
7 , H &
**
1
,
>
*
!
< < q. coord [1] < < "\n" ;
# include < iostream >
;
* (
rational
4.1.4 Initialization and assignment
7
n/d
- 1
*
t.n
/
A
* 0
*
** € ) €
" 1 ! H € , # , !
1 ** !
1
, H 1
r
C
t
K
) * *
H
!
$
t
tK
struct point {
double coord [2];
};
!
,
?€ !
C 1
!
(t1 , . . . , tN)
point q = p;
std :: cout < < q. coord [0] < < " "
int main ()
{
point p;
p . coord [0] = 1;
p . coord [1] = 2;
H
€
,
H * 1
!
C
H * +
add (r, s)
1 )
!
+
!
*
* 12
!
*
,
H
2
A
!
(n, d)
*
t.d
/
6 *
!
* *
6
7 +
&
*
H !
A , !
,
!
)
! * 1
?
H .
7 add (r, s)
!
.
1
1 1
(
*
!
,
* !
D
!
A
) E
t = (t1 , . . . , tN )
N
// 2
}
return 0;
point q = p
rational t ;
t = add ( r , s );
int
t
rational t = add (r , s );
!=
n/d
0=0
74
79
€
8
7
6
8
7
4
9
5
45
€
7
65
5
9
!
*
rational
// POST : returns a * a
rational square ( rational a );
// POST : returns a * a
extended_int square ( extended_int a );
* *
a
// POST : return value is -a
rational operator - ( rational a)
{
a .n = - a .n;
return a ;
}
B
!
*
'
'
!
!
A
*
E
D
1
!
*
*
*
!
!
*
H !
-
!
1
* &
!
1 C 1
1 B 1 ,1
* IH
* ) *
H €
*
*
1
+
,
IH H
1 A 6 * 7 1
A
G
?
1
+
H
H
1 * ! * !
€
;
&
!
*
*
HI
!
*
HI
1
C
add
*
1
*
1
? ?
,
@
F
*
,
& , !
H H 1
, # !
! ,
?
* * !
1 0
H
*
*
H
A ) & +
!
+
H *
* ! ! !
1 * 1
! 1
A
H operator
H
-
// POST : return value is the quotient of a and b
// PRE : b != 0
rational operator / ( rational a , rational b );
double
1
*
, <
*
rational t = p * q - r * s;
r + s
- B
*
1 *
* 1 ! ! H * * & ! )
B * * * * /
H
H
* , H H * ! *
, ! !
, * *
* -
rational t = subtract ( multiply (p , q ) , multiply (r , s ));
,
, D
E
A
<
A ! !
6
- 1
$
%
* )!
!
* 7
1
A add
*
, rational t = r + s ;
F
,
rational t = add (r , s );
)
,
add
!
, 1
H ,
H
1
6 & 7 D * * E ! !
* +
!
H
1
,
*
* !
#
" +
1
! * , , H * IH ! )
H
<
* * A
* ) & 1
! * *
* +
, ,
H 1 *
* , H *
! *
1 ! A
H
*
*
+
// POST : return value is the product of a and b
rational operator * ( rational a , rational b );
int
H
extended_int
,
,
*
t := r + s
H * * * & ! * <
* !
*
!
* *
!
square
!
*
H
4.1.5 User-defined operators
rational
// POST : return value is the sum of a and b
rational operator + ( rational a , rational b)
{
rational result ;
result .n = a .n * b.d + a.d * b. n;
result .d = a .d * b.d ;
return result ;
}
rational t = r + s ; // equivalent to rational t = operator + ( r , s );
// POST : return value is the difference of a and b
rational operator - ( rational a , rational b );
€
€$
7
8
7
6
8
9
5
45
4
7
65
5
74
79
9
*
*
B
unsigned int
1
double
B
/
H
1
*
?
1
double
!
* , ,
?*
,
*
!
+
*
,
1
,
*
) ,
!
*
* 1
6
H & * * H
1
* !
* *
1 *
" H 1 H * *
7
HI
H
H 6
!
*
1
*
!
*
%
g
-
7
& ! * €7
H
* ! ! !
*
*
6
A
C
*
H
,
* < * !
, *
! *
* 6 A *
*
1 * $ ! *
*
1
+!
*
f
* float
*
1
&
,
* H
+
* *
,
!
7
!
*
*
!
! * !
*
%
*
H
!
*
g
int
*
& ?
! *
6
&
f
C
*
*
C
,
1
A
*
!
?
-
67
&
* ,
? !
& *
+!
" &
,
* !
*
* !
! * ! * * *
* H *
1
*
*
1
*
,
*
,
* !
, * ,
! !
*
67
!
-
*
!
6
€7
HI A ! !
, )
! float
H HI
*
1
*
6
! * H
1 * &
! ! *
! * !
* * * * * ! 1 ! * * !
*
! ? +
int
1
*
1
*
H 7
*
+
* HI
*
! *
1
!
f
/
Dispositional.
%
+!
H
H <<
bool
*
* A
7
*
H
f
*
!
Overloading resolution.
H
!6 ) *
A
4.1.6 Details
* *
*
*
!
*
*
&
std
€7
!
*
* *
F
Resolution: Finding the best match.
*
1 * *
C !
*
& )
& - !
! !
)& Argument-dependent name lookup (Koenig lookup).
std::sqrt(2.0)
H ! *
1 * H , !
! , C
!
A
* 1
!
f
-
* * add (r, s)
7
A
'
'
!
*
*
1
,
// POST : return value is true if and only if a == b
bool operator == ( rational a , rational b)
{
return a .n * b.d == a.d * b .n;
}
double
g
f
g
void foo ( double d );
void foo ( unsigned int u );
float f = 1.0 f ;
foo (f );
unsigned int
int i = 1;
foo (i );
4.1.7 Goals
*
7
6
*
-
!
!
€
€
€
7
8
7
6
8
9
5
45
4
7
65
5
9
74
79
+
) 1
H C
z
{0, . . . , 6}
x=y+z
x−y
// POST : return value is the difference of a and b
Z_7 operator - ( Z_7 a , Z_7 b );
3 3 3
0 0 0
3 7
%
Exercise 112
extended_int
" #
$
#
0 "
! $
!
$ 2 !
1
0
" #
3 1
0
" #
$
2
!
$
%
extended_int
extended_int
int
6
3 € 7 6
3 7 !
$
"
0 #
*
H **
, !
*
,
*
**
!
*
? * , *
!
, &
*
7
6
+
1
1
H
* 5
5
6
0
1
2
3
4
#
&
B &
!
* B
3
4
4
5
6
0
1
2
3
,
, !
*
6
3
3
4
5
6
0
1
2
Exercise 110
rational
Exercise 111
rational
2
6 $
6
76 7 2
3
€ 7 !
3
€
3
6 0
"
0 #
!
3
€7
2
2
3
4
5
6
0
1
0
" " 2
! 6
3 7
1
1
2
3
4
5
6
0
3 # #
0
3 #
-
(
!
0
0
1
2
3
4
5
6
// POST : return value is the sum of a and b
Z_7 operator + ( Z_7 a , Z_7 b );
+
0
1
2
3
4
5
6
7
$
" ! !
#
!
%
2
( "%
!
# - #
$ # #
$ # $ 0 "
! !
$
2
!
" # ( ! !
# 6 / € 3 7 6 3 7 1
!
0
3 # '
// POST : returns x OR y
Tribool operator || ( Tribool x , Tribool y );
1 7
9
-
(
// POST : returns x AND y
Tribool operator && ( Tribool x , Tribool y );
$
/7
2
" $
4.1.8 Exercises
=
76 3 € 7 7
0
" !
3 3 # 3
0 0
0
∨
3
2
Z_7
#
∨
6
# # # #
0
0
Tribool
(
2 1
3 #
Tribool
"
!
$
-
∧
0 #
Exercise 109
!
*
* ∧
$ $ ! !
! " #
! Exercise 108
" Operational.
6
6
0
1
2
3
4
5
// POST : return value is the difference of a and b
rational operator - ( rational a , rational b );
// POST : return value is the product of a and b
rational operator * ( rational a , rational b );
// POST : return value is the quotient of a and b
// PRE : b != 0
rational operator / ( rational a , rational b );
// POST : return value is true if and only if a != b
bool operator != ( rational a , rational b );
// POST : return value is true if and only if a < b
bool operator < ( rational a , rational b );
// POST : return value is true if and only if a <= b
bool operator <= ( rational a , rational b );
// POST : return value is true if and only if a > b
bool operator > ( rational a , rational b );
Z_7
// POST : return value is true if and only if a >= b
bool operator >= ( rational a , rational b );
€
€
7
8
7
6
8
9
5
45
4
7
65
5
74
79
9
z = 0
1/
3
(x, y, z)
z
v = (vx , vy , vz )
!
-
! 0
" #
#
2
(
"
$
"
α
3
$
$! x
y
α
α
(x, y)
2
α −
α
*
(x, y)
!
- 2
$ #
3
0
(
!
2 #
! $
! $ $
!
$ #
" 1 " -
#
$ $
!
3
0
$ $
0 # "! " foo(1, 1u)
*
(x , y )
! " !
0
-
foo(1.0, 1)
=
x
y
"
!
4
# 2
4.1.9 Challenges
" # ! 0
1 - ! ! / ( !
2
" 2 2
$&
" ! $
$
$
$
$ #
! 0
"
!
0! $ !
0
3
0
!
"
!
"
foo(1.0f, 1.0)
.
foo(1, 1.0f)
0
1
3 # ! ! # 1
" $ ) " 2 3
#-
2
#
$
#
0" #
! " ! " 2 0
/ " 5
! #
$ #
(
.
-
/
void foo ( double , double )
{ ... }
void foo ( unsigned int , int )
{ ... }
void foo ( float , unsigned int ) { ... }
" 0 # ! / $ ! # 3 0
3 " !
!
0
/ 2 . $
0
Exercise 114
0
. ! ! " 2 #
2
#
"
0 #
<
Exercise 113
0 "
0 !
// POST : return value is the sum of a and b
extended_int operator + ( extended_int a , extended_int b );
// POST : return value is the difference of a and b
extended_int operator - ( extended_int a , extended_int b );
// POST : return value is the product of a and b
extended_int operator * ( extended_int a , extended_int b );
// POST : return value is -a
extended_int operator - ( extended_int a );
// function A
// function B
// function C
A, B, C
foo(1, 1)
foo(1u, 1.0f)
libwindow
.
z
74
79
"
€
€
5
7
8
7
6
8
9
5
45
4
7
65
9
r += s
H
1
, 1 * H ! * *
!
H
-
A
1/2
5/6
H
!
r
r
r += s
5/6
operator+= (r, s)
*
// 1/3
rational s ;
s.n = 1; s .d = 3;
// 1/2
)
rational r ;
r.n = 1; r .d = 2;
1
*
0
*
!
< !
+=
!
,
!
!
HI
2
2
! #
"
" " $ #
1 2 #
!
# !
! $ ! 2 " 0$ # ? 1$
! $ 1 0
# 3
0
! " #
$0
0
!
/
# #
"
$ $
!
!
! 2
1 .
# "
*
! 0
$
2
2 4
$
!
!
!
$
2 -
$
$#
(
!
! /
#
$ "
#
" ! #
p = (x, y, z)
operator+=
-
operator+=
b
H
s
a
, C 6
, , H
7
r
* (
@
!
vz = z
, 1
,
< $
z
.
vz − z
*
H
* !
1 ! * * !
!
*
!
.
t=
vz > 0
4.2 Type Variants
(x − t(vx − x), y − t(vy − y)),
4.2.1 Reference types
rational
rational operator += ( rational a , rational b ) {
a .n = a. n * b.d + a. d * b.n ;
a .d *= b .d;
return a ;
}
r += s;
std :: cout < < r .n < < "/ " < < r. d < < "\ n";
s
74
79
€
8
7
6
8
7
9
5
45
4
7
65
€
5
9
!
*
1 , !
B ! * !
* !
*
,
H
H *
*
! ?
4.2.2 Call by value and call by reference
int & increment ( int & i )
{
, r
// j becomes an alias of i
// k becomes another alias of i
r
%
*
!
1
!
*
*
H * * , !
-
// error : j must be an alias of something
// error : the literal 5 has no address
G
)
1
H 7 *
*
!
*
, !
++
, 6
H
,
!
12
*
!
IH
1
+ ! !
*
! 1 , !
* !
1 *
! H * , 1 $ ! 1
6 ! * <* 7 * *
, ! * !
H
!
B
*
? 1
? ,
+
!
- 1
* * * , 1
!
+! *
!
* !
*
* 1 C
, *
6
, )
*
i
-
rr
H
j = 6
i
r
i
* *
1
*
*
*
!
%
!
* !
!
*
C , !
*
H H
,
! 1 $ 1 #
?
+
* !
// changes the value of
// outputs 6
!
* 12
j = 6;
std :: cout < < i < < "\n" ;
+
!
, 1 * !
! *
* H
H *!
! *
, ! !
H 1
* // j becomes an alias of i
! )* * 1 $ *
H 1 * #
*
! ) * *
* !
H ! * 1
,
* * , * ) 6
! ! , * !
A
H
*
?
!
, * * , 1 6
* $
,
! !
* ( !
H
1 ! *
7
€
+!
< * !
!
* IH
*
*
1 ( * 7
€ ,
€ - +
A ) H
,
!
7
, 1
!
* !
, 1 12
H * !
int i = 5;
int & j = i ;
, int i = 5;
int & j = i ;
int & k = j ;
*
!
! A
* A
%
)
!
! !
* *
1 , A !
*
!
* !
1 int & j ;
int & k = 5;
1 H
<
j
A
! * !
# , < 1 * 2 - !
! 12
*
) 1
, H
*
6
!
1
* 7
#
-
7
,
*
1
,
*
$
#
*
!
6
* 7
7 7D
- !
/ *
* E
D
!
*
7
E
7
+
,
%
, ! 1 ! !
7 !
*
* * H 7
7
7
H 7
+!
Definition.
A
+
*
*
!
!
H
)
1
!
*
*
H +
!
*
*
,
!
* ,
! * !
* B (
* H ! *
*
!
operator+=
void increment ( int & i )
{
++ i;
}
int main ()
{
int j = 5;
increment ( j );
std :: cout < < j < < "\ n" ; // outputs 6
}
return 0;
4.2.3 Return by value and return by reference
increment
€
€$
8
7
6
8
9
5
45
4
9
7
67
8
7
D E H
! *
1 H H
% IH !
1 1 A *
*
A/B
3.4
H H
!
1
H
!
&
-
s
6 6
!
!
7 !
!
* ! 7
*
%
*
+
H H
! , 1
?
-
std::cin >> r.d;
1 A
!
* <
!
H std::cin >> r.n;
*
*
1
*
!
* 12
H * 12
,
!
&
* 12
H 1
,
!
)
!
*
!
<*
!
7
std::ostream
*
1
!
)
&
-
HI
*
1
A
* ,
!
!
*
3
)
H
, H
* *
1 <
&
, !
*
6 6
* (
1
1
7
+
!
!
)
€
)
!
* ! !
*
A
-
!
1
- H *
!
*
*
<<
F 1
, , +
!
* , 1 H
* ,
, !
* ( € ,
, %
!
H A
!
!
!
2
A
H A
A
H +
!
*
( H *
*
1
, < !
* ! *
)
!
!
*
1 2 * HI -
!
! 1 !
! $
*
1
* 2
*
0
add (r, s)
H
*
1
*
A
Rational numbers: input and output.
add
operator+
r + s
*
// POST : b has been added to a ; return value is the new value of a
rational & operator += ( rational & a , rational b)
{
a .n = a. n * b.d + a. d * b.n ;
a .d *= b .d;
return a ;
}
*
a
+
+=
!
* *
*
-
return ++ i;
* HI 1 1
A * A
1
0
4.2.4 More user-defined operators
C
increment
H
int i = 3;
int & j = foo (i );
// j refers to expired object
std :: cout < < j < < "\n" ; // undefined behavior
**
*
!
% *
H , *
! * * +
* ) &
!
!
!
1
1
!
Rational numbers: addition assignment.
rational
i
1
*
!
int & foo ( int i)
{
return i ;
}
H
!
H
<*
Reference Guideline:
1
*
-
- * * H * A
* ( H ?)
j
!
!
,
}
std :: cout < < " Sum is " < < t.n < < "/" < < t.d < < "\n" ;
std :: cout < < " Sum is " < < t < < "\ n";
// POST : a has been written to o
std :: ostream & operator < < ( std :: ostream & o , rational r)
{
return o < < r.n < < " /" < < r .d;
}
std :: cin > > r;
std::istream
// POST : r has been read from i
// PRE : i starts with a rational number of the form " n /d"
std :: istream & operator > > ( std :: istream & i , rational & r)
{
char c ; // separating character , e .g . ’/ ’
return i > > r.n > > c > > r.d ;
}
1/2
operator<<
€
€
€
8
7
6
8
9
5
45
4
9
7
67
8
7
&
< &
!
,
*
B & &
!
* *
H
& ? H
*
*
*
{|
{ {{ {
<
2
"
!
{
{
}
%
$
#
%
{
#
#
r + s
$
9
8
7
6
5
45
8
// POST : return value is the sum of a and b
rational operator + ( rational a , rational b )
{
rational result ;
result . n = a.n * b .d + a.d * b.n;
result . d = a.d * b .d;
return result ;
}
return 0;
// Program : rational .h
// Define a type for rational numbers , and declare
// operations on it .
# include < iostream >
2 // the new type rational
struct rational {
int n;
int d ; // INV : d != 0
};
}
!
// Program : rational .C
// Define a type rational and operations on it
// computation and output
std :: cout < < " Sum is " < < r + s < < " .\ n" ;
operator+
{
Program 34:
std :: cout < < " Rational number s :\ n";
rational s;
std :: cin > > s;
Program 33:
}
int main ()
{
// input
std :: cout < < " Rational number r :\ n";
rational r;
std :: cin > > r;
// POST : a has been written to o
std :: ostream & operator < < ( std :: ostream & o , rational a );
// POST : a has been read from i
// PRE : i starts with a rational number of the form " n /d"
std :: istream & operator > > ( std :: istream & i , rational & a );
{{ { {| } { | } { {| } { | } {
// Program : userational2.C
// Add two rational numbers .
# include < iostream >
# include " rational .C "
// POST : return value is the sum of a and b
rational operator + ( rational a , rational b );
*
) *
A
*
, H
1
1 1
*
1 * 1
1 , 1
) *
$
H
ifm
&
% !
B
&
H * rational
rational.C
userational2.C
rational
??
// the new type rational
struct rational {
int n ;
int d ; // INV : d != 0
};
"
D
*
E
! !
* ( *
+
rational.h
namespace ifm {
} { | &
! , * H
!
)* A
1
*
*
*
€
€
7
4
9
67
8
7
0
,
,
* !
*
A
main
int
// POST : b has been added to a ; return value is the new value of a
rational & operator += ( rational & a , rational b ) {
a.n = a .n * b.d + a.d * b. n;
a.d *= b.d;
return a;
*
1
rational
*
H B
1
,
H
*
!
*
* H ,
+
int
rational operator - ( const rational & a)
{
const
/
-a
a
, , )
<*
C
* 1
* !
1 H
!
*
+! * ? 1 ? ! *
1
*
* !
B H * )
* *
* * ,
<
2 "
!
+
H
1
* , A
*
, 1 D * * A
* ) E& ! 1
1 !
! , * , H * !
* 1 H
operator+
>
*
7
* (
A
1
H
, 1 &
*
H
,
* !
+
! !
*
! * ,
!
* * 0
HI
*
!
, B
* H 6
! ) 7
(
H H
* !
,
H
H H
* A
A
!
) H
*
! ) 1
, , ! *
1
, !
1 *
H
! * *
HI
,
1
!
*
!
!
*
*
, 1
6
*
7
1
HI
<
operator+
*
!
!
a + b + c
)
} { | {
{ { { }{ {
rational
? ,
HI '
'
*
6
0
* *
! * * 1
A
* !
, 1
! 1 !
* ! * H %
?
*
4.2.5 Const-types
*
* * , !
%
// POST : a has been written to o
std :: ostream & operator < < ( std :: ostream & o , rational a )
{
return o < < a.n < < "/" < < a.d ;
}
>?
D ,
** 1
!
*E H
1
* ,
/ /
*
* !
,
!
// POST : return value is the sum of a and b
rational operator + ( rational & a , rational & b )
{
rational result ;
result .n = a .n * b.d + a.d * b. n;
result .d = a .d * b.d ;
return result ;
}
!
Rational number r:
1/2
Rational number s:
1/3
Sum is 5/6.
A
<
// POST : a has been read from i
// PRE : i starts with a rational number of the form " n/d "
std :: istream & operator > > ( std :: istream & i , rational & a)
{
char c ; // separating character , e.g . ’/ ’
return i > > a.n > > c > > a. d;
}
! ,
* )
1 * * * A
H !
! 6 A 7 *
* ,
+
1
, * ? ? 1 ! 1
! *
H * , H , , ! !
, !
1 ? * ) 1
H A *
* * 2
* ? 1 ? , IH
1 ? *
* 0
Program 35:
, *
)
}
a, b, c
operator+
rational
// POST : return value is -a
rational operator - ( rational a)
{
a .n = - a .n;
return a ;
}
rational operator - ( rational & a)
{
a .n = - a .n;
return a ;
}
"
€
€
8
7
6
8
9
5
45
4
9
7
67
8
7
"
!
#
"
*
!
*
, !
, * *
7
+
!
F ) 7
H * !
*
H const
A , < 1
!
!
*
1 ;
! * C , 1
* *
F
const int & i = 3;
? ! !
, *
,
! !
! !
1 ?
+ !
7
const
const
H 7
*
76
H ! , )
i
%
H 7
+!
6
7
*
*
"
&
!
*
1
% IH H
1 ! * A
A
! $
$
$
* H
, ; 1
**
' '
! 7
1 2 *
H 1 H 1 * 2 , 6
*
*
H * )
7
!
! *
7
? C * & A ! ? , * * A * < 1 , ,
A
? * A
6 !
- B
A
*
* 7 * C
) 1
* 2
A
,
1
* H
A
H
)
7
!
*
!
6
* 12 7
)
!
&
1
7 *
*
F
* !
*
!
, H
A ,
*
6 1 G
*
IH
, 1
, 1
A
1
*
*
+
,
A * !
) & H 1 *
1 * A * ? 1 ? , H
* HI ? 1
6 ** 7 ,
* !
1
* 2
1
,
1 1
! &
* 12
H 1
*
* *
<
const int n = 5
7
7 *
* H 1
*
1 # / !
6
!
7
!
7
H
7
!
$
" 7
* const
*6
? 1
//
//
//
//
!
H ,
!
!
*
A D *
! , E
0
*
< 1
*
!
)
+!
$ , 7 !
- -
*
6
A ? C
/ * &
,
IH
* 7!
B 7 ) )
* * ! ? ,
H * !
*
* * * A int n = 5;
const int & i = n ;
int & j = n ;
i = 6;
j = 6;
&
*
,
!
!
*
*
a
n
*
const int n ; // error : uninitialized constant
const int
!
*
*
n
4.2.6 What exactly is constant?
-
n = 6
H
const int n = 5;
n = 6;
*
const
,
7
H *
*
,
const
? < IH
* ? ,
!
* 6 * * H ! * 1
!
! * ? F
! * ? H 7 A
6
. *
? !
* ? 1
const
const
}
1
!
,
*
0
operator+
#
Definition.
a .n = - a .n ; // error : a was promised to be constant
return a ;
n
const int n = 5;
int i & = n ; // error : const - qualification is discarded
i = 6;
const
i becomes a non - modifiable alias of n
j becomes a modifiable alias of n
error : n is modified through const - reference
ok : n receives value 6
const
4.2.7 Const-references
operator+
// POST : return value is the sum of a and b
rational operator + ( const rational & a , const rational & b ) {
rational result ;
result .n = a .n * b.d + a.d * b. n;
result .d = a .d * b.d ;
return result ;
}
b
€
€
8
7
6
8
7
4
9
9
5
45
67
8
7
! *
result
*
! G
*
1 6 (* 1
,
H 7 !
!
1 *
! ! +
* !
*
* H !
? !
H *
* & &
1
! *
, * 1
;
* 6 1 , *
A * ! ,
H 7 *
*
+
! IH
*
)*
<
A / * !
H
const
A
H
*
7
!
! *
!
*
const
A
*
/
7
H
,
*
HI !
! 7
* * , @ * 7
! * !
IH , 1
7
7 H ! 7 < , !
*
,
* 12 ! +
A
/ *
* * * ,
! * 1
A
*
+
/
−
!
+ ! 7
A
*
! 1
* *
C const
7
/ *
* H H
! !
* A
1
! *
* /
* < !
1
-
6
,
% H !
6 ! *
,
12
*
, 1 B 7 ! *
H ! *
H A
(
*
€
1 € 7
* *
, ? 1
!
Const Guideline:
!
H 1
! * ! *
? H +
A
H * !
! !
H !
*
< ! ?
+! * ?
H
! * ! * ! * !!
* *
) * H 2
) ! A
*
, +
operator-
* * * ?
<
& !
* 6 , * ! ! *
* * !
*
1 &
* * H
H 7 const rational & operator + ( const rational & a , const rational & b ) {
rational result ;
result .n = a .n * b.d + a.d * b. n;
result .d = a .d * b.d ;
return result ;
}
operator+
!
const
&
1
A +
*
const
const rational&
? !
const
rational
!
A ! ! ,
* HI * ! 7
1
7
B
A H
,
6
!
A
H
* ,
,
H 7
!
*
* IH
* ? 1
? , , A
* HI ! , A
D *
,
E H
!
?
!
*
!
,
1
H A
1
,
6
* ! ,
<
! * ) A 7
, ! A
* ! !
* ! * 1 !
+
6
H * 7
1 * ! *
? 1 * ? !
*
A
% ! H ,
H
A *
* A
1
*
? C
, * &
1 H *
*
H
*
!
7
+!
, C
IH ,
*
7
/
,
const
% *
* < ) *
H *
A 1
* * 1 *
! H *
H 7
*
1
) , / 12 $
* A
, * H !
)
; !
# <
* #
HI *
1 * H 7 , H
H 7
!
!
1
,
1 ,
* A * * ? 6
7
1 ? , !
!
!
1
6 * ( !
€ * 12
*
? !
*
!
*
;
*
HI
*
*
* 12
,
H
*
! , A
! ) ; ( ,
* 1 , * 2
H € !
7 * 6
C
- * 1 &
* ? 7
!
6
6 C , *
! * *
!
* , 7
! *
1
* *
* , & ! 1 *
1 A
* ,
!
,
1
* , ? ?
7
! - A
! 6 , ! * ! 3
12
*
! * € )
7 1
!
!
*
? *
!
? *
?
!
*
? % 3
*
! * H ,
6
*
) *
? !
*
7
+
)
*
1
return
const
i
result
const
4.2.9 When to use const?
4.2.8 Const-types as return types.
const
const
const
const
"
€
€$
8
7
6
8
9
5
45
4
9
7
67
8
7
) .
(
9
#
" #
#
2 - 2
const int&
!
$
int int&
const int&
int const int int&
%
#
#
0" #
// POST : return value indicates whether the linear equation
//
a * x + b = 0 has a real solution x ; if true is
//
returned , the value s satisfies a * s + b = 0
bool solve ( double a , double b , double & s );
€3 7 1
76
6
2
#
# ! 2
gcd
1
#
#
0
.
? **
*
?
?
&
! ! *
* * B * 3
7
6
!
*
7
6
!
6
!
7
* 3
€
B
*
!
7
6
#
! 1
#
0
4
2 2
1
* +
H
$ ! !
!
0
2 / ? !
*
B ,
, !
)*
? ! !
* *
3
3
? *
7
B
!
*
1
*
!
* #
$
$
/
€7
*
3
*
!
1
!
*
*
B
7
H %
H
)
F
1
0
!
0
6
€7
3
2
#
#
4
int
€3
3
2 -
3
"
# $0 $
# (
0
int
2
Exercise 118
0
$
7
6
Exercise 117
!
&
! * * ? * B * 3
Exercise 116
Hint:
$ # $
4.2.11 Exercises
! &
* 3 , 7
6
4.2.10 Goals
T foo (S i)
{
return ++ i;
}
#
#
$ #
"
0 #
<
Exercise 115
" 0
7
3
Operational.
76
1
76
-
6
3 7
3
3
" #
2 #
" !
!
#
!
,
F
* & * 1
**
IH * *
? ! !
* , IH
1 *
*
*
2
! , *
* , 1 ,
1
1
!
*
? H
*
H
*
/
1
A
A
const rational
!
$
t
#
$
$ / 7#
! $ 9
#
0 0 2 " -
Dispositional.
,
1
*
1
* H %
-
* # 0 "
" $0
# #
0 1 / $ ! $ #
7
" 0 9#
2 H
* HI
* 1
% ) A
* 6
A
7
< € /
*
1 *
* )
!
const
foo
int a = 5;
int b = 6;
// here comes your function call
std :: cout < < a < < "\n" ; // outputs 6
std :: cout < < b < < "\n" ; // outputs 5
21
−14
−3
.
2
// POST : r is normalized
void normalize ( rational & r );
// POST : return value is the normalization of r
rational normalize ( const rational & r );
"€
€
"
€
8
7
6
8
9
5
45
4
9
7
67
8
7
int main ()
{
int i = 5;
const int & j = foo ( i );
}
int main ()
{
const int i = 5;
int & j = foo ( i );
}
!
/
2
)
std::complex<double>
std::complex<double>
#include <complex>
r
$
$
$
!
#
" !
"
0
"
! 0 * $
?
!
/! $ (
0
.
=<
=
Exercise 121
double
$ 1
! $ 2 ! /! " i
$
" !
3
7
6
*
+-
(
)
" %
"
%
-
-
(
-
" %
"
%
+
*+
)
" %
"
%
-
(
-
-
"
"
+ -
%
(
%
-
"
"
)
%
%
-
(
2
7
-
- #
0 0 "
$ !
"
! $ $ #
! 7
$ 1 (
#
1 ! #
3
6
€7
{(2, 1), (0, 2), (0, 0), (3, −4)}.
! '
0
!
2 . - /! ! !/ !
" !
/ 0
! 0 3
3 # /
/ !
2
/ ( 2 #
!
0 $ # "
! #
0
2 #
' # !
!
" (
$
2
22
- 1 $ # const int & bar ( int & i ) {
return i += 2;
}
8
main
int foo ( int & i ) {
return i += 2;
}
Exercise 120
$ 3 2 ! # " 3
(
#
2 $
#
#
0 " 0 "
0 !
"
#
/
! 0$
const
-
Exercise 119
!
#
"
!
#
.
$ #
(a, b)
int main ()
{
int i = 5;
const int & j = bar ( foo ( i ));
}
int main ()
{
int i = 5;
const int & j = foo ( bar ( i ));
}
int main ()
{
int i = 5;
const int j = bar (++ i );
}
4.2.12 Challenges
std :: complex < double >(r ,i ); // r and i are of type double
std::sqrt std::abs std::pow
double
// POST : return value is the number of distinct ( complex ) solutions
//
of the cubic equation ax ^3 + bx ^2 + cx + d = 0. If there
//
are infinitely many solutions ( a=b= c=d =0) , the return
//
value is -1. Otherwise , the return value is a number n
//
from {0 ,1 ,2 ,3} , and the solutions are written to s1 ,.. , sn
int solve_cubic_equation ( std :: complex < double > a ,
std :: complex < double > b ,
std :: complex < double > c ,
std :: complex < double > d ,
-
"
€
"
€
8
7
6
8
9
5
45
4
9
7
67
8
7
,
rational r
*
0
!
? %
- HI H
1
*
, ! !
, 1
?
!
1
1/2
!
,
H %
* *
(
r
,
*
. " 3
IH 7
!
% - ,
, 1
,* 1 !
6 , *
!
*
*
H 7
! /
*
)
, , (
* - H , 7
!
*
* *
6
?
1
H
!
*
6
* !
C
+
, ,
*
* IH
, 6 &
! * 1 7
* ,
rational
;
2
A H ,
)
! *
* 1
+
rational
??
B
! *
*
B
*
A
*
1 %
rational
Issue 1: Initialization is cumbersome.
r
,
1
$ # 0 # #
/
" 2
2 0 0 ! #
! !
$
3
#
.
2
# 2
#
"
2
1
.
# ) !
0 #
/
! #
!
" -
*
3
)* (
&
4
9
2 A
!
$
0
!
/
$ 3
$
$
-
/
#
$
!
$ $
2
"
!
4
& 3
0
0
"
# # $ 0 0 !
! 0 " $ &
3
ax3 + bx2 + cx + d = 0
2
Hint:
/ ! $ #
! ( 0
"
:
$ std :: complex < double >& s1 ,
std :: complex < double >& s2 ,
std :: complex < double >& s3 );
4.3 Classes
4.3.1 Encapsulation
rational r ; // default - initialization of r
r.n = 1;
// assignment to data member
r.d = 2;
// assignment to data member
r
r
""
€
"
€
8
7
6
8
9
5
45
4
9
7
67
8
7
H
*
A
* ,
,
6 7 A
* <
* , 1 1
.d
H
,
*
!
*
!
*
!
, ;
,
!
! 1
, 1 -%
6
,
! < -% , €
1
,
H 7
*
, * *
* !
* +
* G
&
1
*
rational.C
H
!
!
)
.n
.n
* % ,
! A !
C * 1
A H A " A
* * 7 !
H
1
!
H
H
, ,
!
?
!
*
*
H ,
" $ !
A
< )
*
! !
1 !
1
! int
*
!
1
< *
* !
*
*
,
!
&
!
rational.h
* -
H
*
!
bool
**
6 1
7 1 ! 1
IH
1
* *
%
A
% * - A 1 1
C
; % -A * @
!
unsigned int
bool
%
, !
, extended_int
@ ,
* H * , 1
! ,
€ ,
,
* G
%
!
,
!
, C
*
-%
H !
1
1
! ! * @
,
, + 1
!
,
H
1
Issue 3: The internal representation cannot be changed.
%
&
*
B
H !
*
H
H
1
!
*
* *
1 A B
, 1
!
)
€
&
+
!
H %
!
!
H ? 1
*
;
!
*
H
* , ) 1
H 7
1
&
, 6
1
7
+!
*
*6
r.d
,
*
create_rational
H A
, *
* ; &
1 !
* !
*
*
1 0
!
!
*
create_rational
!
H ! * 7 A
6
* rational r = create_rational (1 , 2);
!
?
// PRE : d != 0
// POST : return value is n/d
rational create_rational ( int n , int d ) {
// somehow check here that d != 0
rational result ;
result .n = n ;
result .d = d ;
return result ;
}
?
H
*
* 6
r.d = 0
, * * 1 *
H *
* < 7 *& * 1
-% ** ,
*
*
6
, %
A
,
%
-
!
*
H
@
1 A
1
1
* *
1
! <
)
% !
* ! 1
rational r ;
r.n = 1;
r.d = 0;
H -
H
)
F
!
*
! , !
rational
*
rational r;
H 1/2
r
@
,
1 rational
int
rational
, 1 (
*
* A !
*
*
- A
!
! *
* * H 1 1
* * , r.d
, *
1
+
!
*
H ,
* Issue 2: Invariants cannot be guaranteed.
, ! !
H 1 1
!
6
1 1
1
7
1 !
+
H 0
rational_vector_3
r
struct rational {
unsigned int n ;
// absolute value of numerator
unsigned int d ;
// absolute value of denominator
bool is_negative; // sign of the rational number
};
create_rational
rational r ;
r.n = 1;
r.d = 2;
rational r = create_rational (1 , 2);
.d
"
€
8
7
6
8
7
4
9
99
4
9
5
45
"
€
8
#
!
1
.
H
?
!
*
1
)
*
, * %*
1 * &
! * ,
* 1
* 1 !
*
*
*
!
*
H
)
H
-
!
*
1
,
1
!
+!
rational
**
1
// get numerator of r
// get denominator of r
H
,
/
*
* H ?
* A
!
*
,
!
!
*
!
)
H *
!
*
!
*
1
!
7 * <
$
* (
d
The implicit call argument and *this.
public
int n = r. numerator ();
int d = r. denominator ();
A
H
,
H
class rational
1
*
* 1
* & !
*
1 + 0
!
*
!
* H
1
!
*
H 1
!
! 1!
* <
*
*
/
* ! !
!
*
1 ! ,
*
* !
H &
B ! D
! E
1
1
*
!
6 *
A *
' !
' H
A
*
, 1 1
!
*
HI
1
?)
+
*this
+
!
*
HI
*
* r
1
G
)
1
B H *
*
H ! * !
* *
1
1
* !
- A !
! ** 1
<
*
!
public:
!
r.d = 0
rational r ;
r.n = 1;
// error : n is private
r.d = 2;
// error : d is private
int i = r. n ; // error : n is private
*
< private
numerator
,
* C 6
1
* *
H % * , + !
* ! , 1 7 H 1 1
** 1 !
! *
1 * IH 1
H
!
+
class rational {
private :
int n;
int d ; // INV : d != 0
};
!
* H 1
n
* " !
, *
H
) 1 6
& 1 #
H
+! 1
, H
1 *
1 1
H * *
*
1
*
* * 1
H
private:
H
H
% *
& H 1
A
! !
* !
& *
1 +
! !
struct rational
&
!
+!
+
* 7 * H * !
* * &
*
6
& H (
* 1 $
1
1 1
* !
1 ! , *
1 ! +
!
*
class
class rational
, 1
**
public
*
+
* * < H
* *
1
*
1
6
* 1 H 7
1
, 6
7
struct
operator+
public:
class
, * A
1 * * !
**
! * *
1 HI # !
*
4.3.2 Public and private
! * * 7
! class
rational
-
%
* * 1
# ) ,
!
4.3.3 Member functions
const
class rational {
public :
// POST : return value is the numerator of * this
int numerator () const
{
return n;
}
// POST : return value is the denominator of * this
int denominator () const
{
return d;
}
private :
int n;
int d ; // INV : d != 0
};
denominator
€
"
€$
8
7
6
8
7
4
9
99
4
9
5
45
8
class rational {
public :
// POST : return value is the numerator of * this
1
)*
H 1
-
H
7
6
H 7
6
H
+
1
*
*
!
!
6
H H
7
6
H
H
6 H H 7
<
67
6
7
7
76
7
7
!
!
*
- * (
) 7
!
*
*
&
,
*%
, * ) 6 , *
*
! , IH
*
H &
#
*
! *
, !
*
!
C
H >
*
*
+
- *%
*
! * * *
* 1
*
!
*
,
! * !
H > *!
,
)
H
H 6
7
6
)
1
!
*
*
!
-
6
*
*
&
H ! *
7 *
*
! &
A
*
!
*
!
!
%
!
! !
1
1 1
7 ! !
*
*
* * *
1 ,
!
* 6
1
,
# !
rational.C
)
H
* H H +
H )
* H H
!
*
6 !
* , * !
1
* IH !
*
H *
* 1
**
ifm
!
1
HI
H public:
!
) <* !
! * 7
! 1
!
* 1
* !
*
1 ** !
1
f
!
1
,
1
.
H H
,
* 1
* ,
1
IH
*
H , H
!
-
6
1
HI
*
1,
)
+ ) *
!
A *
!
H
1 1
!
*
*this
<
1
!
)
*
*this
numerator
ifm
!
)&
B
*
H ! * *
1 *
! !
* .
1
n
H
)
r.numerator()
r
H
!
* (
Member functions and modularization.
1 ! * !
* ** 1
1
!
*this
+
*
Access to members within member functions.
**
* ! 1 1 1
C **
1
! * 1 * ! 6
A
*
, * - 7
%
H *this
private:
! *
! *
&
, !
!
*
H
1
!
!
1
* !
*
!
1
n
*this
!
!
:
*
Member function call.
const
,
*
A A
1
! !
*
* HI )* &
IH
1 ! * *
& Const member functions.
*
* !
!
*
H
, 1
*
*this
* H 1
1
* * * *
* +
IH +! rational.h
!
* * & < 7 &
H
!
*
1
C
&
* 6 * +
*this
int numerator () const ;
// POST : return value is the denominator of * this
int denominator () const ;
private :
int n;
int d ; // INV : d != 0
};
rational.h
int rational :: numerator () const
{
return n ;
}
int rational :: denominator () const
{
return d ;
}
4.3.4 Constructors
8
7
6
8
7
4
9
99
4
9
5
45
€
€
€
8
!
*
*
*
! ? 0$
, +
*
!
1 *
*
* * 6
* H * H 7
F
0
rational
H
double
rational double
operator double
// POST : return value is double - approximation of * this
operator double ()
A
*
D ?
&
, * ,
* 1 !
* ,
* 1
-
*
! ) *
H E
int
, *
double
rational
double
int
* H
*
B
!
*
*
, , !
A !
* *
H * , !
)
,
*
rational
rational
rational
int
* *
1 ? 6 ? A &
* ! , IH
1
H 7 1 * !
int
*
H
F
1
* *
< *
* *
rational
, ! 7
?
*
&
,
*
,
* A
1
1
'
'
1
*
H
?*
*
&
,
1
* !
7
1
* )
* H H
6 1
*
*
*
*
- *
*
H
H 6
7
7
6
* H +
!
'
'
7
&
* *
*
H
6
* '
'
!
*
* )*
1
!
)
*
,
1
* ! *
*
H * !
! ? * * !
* 1
B !
*
1
- H
* !
*
*
,
1
!
*
*
*
HI
*
* (
*
* * * *
! * )* <
H
rational
*
!
A
4.3.5 Default constructor
rational
6 rational
-
rational r = rational (1 , 2);
rational r ;
F
) * H
!
* *
, *
!
A
!
* 7 *
1 *
) !
* * *
H * 1
!
)
6 !
* !
rational(1,2)
,
* (
H
, *
#
1 +
r
*
,
* ,
*
C
, &
H 1
+! * A
A
*
rational r (1 ,2); // initializes r with value 1/2
6
1 * ! * 1 H 7
H 7 * 1
1
* <
*
!
7
7 +
D
* E
IH
#
!
? HI
!
!
*
€
- * ! 1
*
*
,
* *
*
*
1
* )* , 1
< +
!
*
H ,
- !
A * *
* ! !
! *
H **
* * // PRE : d != 0
// POST : * this is initialized with numerator / denominator
rational ( int numerator , int denominator)
: n ( numerator) , d ( denominator)
{
// somehow check that d != 0
}
- * , IH
! A
H
! ,
) H * 1
*
1 ,
1 !
*
;
H ,
!
*
!
* * *
H C
*
*
*
A
* !
,
H
F
H
!
1
*
*
rational
*
1
* ,
H
! !
?
!
+
1 *
*
1
// POST : * this is initialized with 0
rational ()
: n (0) , d (1)
{}
rational r
rational r;
4.3.6 User-defined conversions
// POST : * this is initialized with value i
rational ( int i)
: n ( i ) , d (1)
{}
double
8
7
6
8
7
4
9
99
4
9
5
45
€
€
8
H 7
H 7
,
!
6
!
b
a
!
*
1
operator+
operator+
operator+
2 + r
1
H
H
!
r
+!
2 + r
H
r + 2
r + 2
*
*
*
*
!
,
* 1
1
H * 1
H * !
!
H ? & * H ,
1
A 1
,
@
Prefer nonmember operators over member operators.
class rational
H
A
)
!
,
rational
1
rational
)
*
int
+
!
,
operator+=
*
* HI
denominator
,
H
H
operator+
+
, *
IH * 1 * , * *
,
1 *
! *
*
operator+ operator+=
A
* !
* H
0
A
&
D E // POST : b has been added to a ; return value is the new value of a
rational & operator += ( rational & a , rational b)
{
return a = a + b ;
}
+
*
! IH
*
! *
,
!
, *
A ) * **
;
!
1 *
* !
< 6
7 1
H
! % H
? 7
! 1 ! ) +
** H
1
*
A H A *
6 H 1 *
! * 1 * *
* *
? ?
,
1
!
,
D
1
E
, !
*
%
<
!
H
*
* *
*
* , ! 1
!
*
,
* !
,
struct rational
operator+
!
**
*
1
,
!
*
H
1
**
1
* H
1
+
!
*
1
H 9
* * ,
A
9
, *
9
&
*
,
? *
}
*
, 1
!
*
! * %
)
1 H H 1 *
* * *& 1 !
* // POST : return value is the sum of a and b
rational operator + ( rational a , rational b)
{
int rn = a. numerator () * b. denominator () +
a. denominator () * b. numerator ();
int rd = a. denominator () * b. denominator ();
return rational ( rn , rd );
}
!
*
HI
1
*
1
)
*
D
&
E
*
!
A
H 4.3.7 Member operators
A &
* <
* 1
!
*
,
! *
+
* *
* ! ,
*
1 // POST : b has been added to * this ; return value is
//
the new value of * this
a
*this
A
2. operator + ( r )
a + b
numerator
operator
!
A
1
*
* * 6
1 C * *
1 * H 7
A
* +
! &
* *
H *
?
operator+
2 + r
% 1 ,
) *
1 * A
1 !
*
6
return double (n )/ d;
!
A
{
rational & operator += ( rational b)
{
n = n * b.d + d * b. n;
d *= b.d ;
return * this ;
}
operator+
// POST : return value is the sum of a and b
rational operator + ( rational a , rational b)
{
return a += b;
}
operator+
// POST : * this is initialized with value i
rational ( int i );
operator+=
rational
r + 2
r. operator + (2)
int
8
7
6
8
7
4
9
99
4
9
5
45
"
€
€
8
HI
1
E
*
!
! 1
!
7 *
% H 1 !
*
! * & ,
*
* (
!
!
$
*
H 1 !
*
&
H
* !
*
& H
1
%
1
*
H 1 1 *
& * % * , H & * * * * ! 1
!
* *
IH H
private:
%
H 7
A
1
*
! &
-
6 C
*
!
7
B
7
!
2
!
*
F
H * &
, 1
*
+
6 **
7 ? ! *
! ' '
*
*
, H
H +
H !
* !
*
* - H !
* *
* ! * A !
1 * 7 * 6
, H
I
* H A 1 H rational
int
H
* 1
& *
!
* *
/ ! ,
* * 6 !
* ( +
€ * 7 ? 1
%
& * &
6 !
public:
, D
!
−
! 1 2
* * , &
!
1
*
H
*
1 * 1
+ A
! +
A
, 1
1 H * C *
*
! !
!
-
*
+
* * ) +
+
* !
H , 1 * H *
1
<* , $
1
HI
!
*
* )
!
*
#
!
$ %
Typedef declarations.
1
Class Scope.
!
*
, rational::rat_int
int
−
typedef int rat_int ;
*
*
!
// 1
// 2
*
1
! * &
D1
E
1 * ! H
1
1 ! * 0
& * +
&
H
* A
class rational {
public :
// nested type for numerator and denominator
typedef int rat_int ;
...
// realize all functionality in terms of rat_int
// instead of int , e.g .
rational ( rat_int numerator , rat_int denominator ); // constructor
rat_int numerator () const ;
// numerator
...
private :
rat_int n;
rat_int d ; // INV : d != 0
};
typedef ifm :: integer rat_int ;
+!
!
1
)
* H *
1 ? % , 1 1
1
!
! *
% *
1
* H
)
H 1
1
H
* H /
1
*
H 6 !
)
!
*
* (
€
€
$ 7
* *
* - * *
H * !
1 ! denominator
+
*
int
G
,
typedef rational :: rat_int rat_int ;
rational r (1 ,2);
rat_int numerator = r. numerator ();
rat_int denominator = r. denominator ();
,
! , !
A * F ) H H * , ! ! )
* 1
*
,
1
, +!
numerator
1
numerator
)*
1
4.3.8 Nested types
rat_int
denominator
4.3.9 Class definitions
class
€
€
8
7
6
8
7
4
9
99
4
9
5
45
8
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
!
*
2
"
!
H
H
H
}{
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
!
H
1
&
1
!
&
1
H
C !
C
*
* ?
{
{ {{ { {| } { | } H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
%
{
H
H 7
* *
H - * *
* 1
**
, *
a, c, m, x0
, )
C
*
H
*
!
*
1
,
* IH
+
*
*
, H
* 1 H * ! "
** ) & * H
H * ! ! 1
*
*
,
< *
"
*
?
ifm
!
1
1
*
* (
*
'
* '
B
H 1
*
, , ! * ) * €€ !
* *
H , *
*
* !
*
* 1 1
* 1
? 1 *
&
!
*
random
)
H
Program 36:
operator()
!
*
-
H
H
H
H
H
H
H
H
H
H
H
H
H
1
1
operator()
1 ! 7 *
6
, A
# * * A
operator()()
- H
1
$
1 ! %
* )
+
H ! H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
*
* ;
1 !
! 6
*
,
!
!
*
, 7
;
A H B ! * 1
,
1 A 6
! ! *
? 7 A
+
rational
* H
H
H
H
H
H
H
H
H
H
H
H
1
!
x0 = 0.
!
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
knuth8
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
*
a
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
!
1
"
3
* , !
C
*
H
x0
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
m = 28 = 256,
H
H
H
H
H
H
H
H
H
H
H
H
i > 0.
H
H
H
H
H
H
H
H
H
H
m,
H
H
H
H
H
H
H
H
H
m
H
H
H
H
H
H
H
!
Linear congruential generators.
H
H
H
H
H
H
H
H
x1 , x2 , . . .
H
H
H
H
H
c = 187,
H
H
H
H
H
H
H
H
)
xi = (axi−1 + c)
H
H
H
H
H
a = 137,
H
H
H
%
c
H
H
187 206 249 252 151 138 149 120 243 198 177 116 207 130 77
240 43 190 105 236 7 122 5 104 99 182 33 100 63 114 189 224 155
174 217 220 119 106 117 88 211 166 145 84 175 98 45 208 11 158
73 204 231 90 229 72 67 150 1 68 31 82 157 192 123 142 185 188
87 74 85 56 179 134 113 52 143 66 13 176 235 126 41 172 199 58
197 40 35 118 225 36 255 50 125 160 91 110 153 156 55 42 53 24
147 102 81 20 111 34 237 144 203 94 9 140 167 26 165 8 3 86 193
4 223 18 93 128 59 78 121 124 23 10 21 248 115 70 49 244 79 2
205 112 171 62 233 108 135 250 133 232 227 54 161 228 191 242 61
96 27 46 89 92 247 234 245 216 83 38 17 212 47 226 173 80 139
30 201 76 103 218 101 200 195 22 129 196 159 210 29 64 251 14
57 60 215 202 213 184 51 6 241 180 15 194 141 48 107 254 169 44
71 186 69 168 163 246 97 164 127 178 253 32 219 238 25 28 183
170 181 152 19 230 209 148 239 162 109 16 75 222 137 12 39 154
37 136 131 214 65 132 95 146 221 0 187
H
x1 , x2 , . . .
H
C
-
*
C
!
6
H * ,
1
H
F
4.3.10 Random numbers
m
[0, 1)
xi
// Prog : random .h
// define a class for pseudorandom numbers .
namespace ifm {
// class random : definition
class random {
public :
// POST : * this is initialized with the linear congruential
//
random number generator
//
x_i = ( a * x_ {i -1} + c ) mod m
//
with seed x0 .
random ( unsigned int a , unsigned int c ,
unsigned int m , unsigned int x0 );
// POST : return value is the next pseudorandom number
//
in the sequence of the x_i , divided by m
double operator ()();
private :
const unsigned int a_ ; // multiplier
const unsigned int c_ ; // offset
const unsigned int m_ ; // modulus
unsigned int xi_ ;
// current sequence element
};
} // end namespace ifm
€
€$
8
7
6
8
7
4
9
99
4
9
5
45
8
#
unsigned int
/
!
1
1
*
1
1
)*
1 :
2
<
"
!
{|
!
1
,
H
6 ! ,
1 7
7
$ 1 *
[0, 1)
m = 248 ,
pi
pi = 1/6
i
)
+
HI
1
* *
&
H !
@
*
* *
F 1
H
@
*
!
1
1 /
1 1
+! H 1 / 1 1
H
F
€
!
6
$ ! C
,
) *
!
1
* !
*
* )
H 1 H
1
* * , * !
+!
1
H
H 6
67
!
*
*
! *
+!
C
1
!
- 6
6
1
! !
H
* 1 1
7
1
+
! , A 6
1 !
H * !
A
H , 1 !
A
!
7
xi
1
operator()
# include < IFM / random .h >
@
,1
IH
! C <
* A ) 1 1
1 ,
1 H *
// Prog : random .C
// implement a class for pseudorandom numbers .
1
@
;
)
) H *
*
€ , * , ! * ) 1
! ,
!
*
C
The game of choosing numbers.
/
* 1
* H
*
1 &
1 ! 1 * * C
namespace ifm {
// class random : implementation
random :: random ( unsigned int a , unsigned int c ,
unsigned int m , unsigned int x0 )
: a_ ( a ) , c_ (c ) , m_ (m ) , xi_ ( x0 )
{}
random
*
1 *
7 1
1 *
/
&
,
* 6
1
!
*
* ! &
6 *
6
1 !
H 7
* *
* , ?
*
double random :: operator ()()
{
// update xi acording to formula ,...
xi_ = ( a_ * xi_ + c_ ) % m_ ;
// ... normalize it to [0 ,1) , and return it
return double ( xi_ ) / m_ ;
}
} // end namespace ifm
} { | } { random
{0, . . . , 232 −1}
double
F
drand48
1
*
+
* 1
*
)
H ? A
**
x_i
!
1
!
H *
Program 37:
c = 11,
operator()
// Prog : loaded_dice.h
// define a class for rolling a loaded dice .
# include < IFM / random .h >
{ | } { }
#
m
a = 25214903917,
random
1
*
)
A
!
*
<
67
6
7
1
!
*
H H
67
76
operator
!
, 1
knuth8
i
{1, . . . , 6}
loaded_dice
[0, 1)
namespace ifm {
// class loaded_dice : definition
class loaded_dice {
public :
// PRE : p1 + p2 + p3 + p4 + p5 <= 1
// POST : * this is initialized to choose the number
//
i in {1 ,... ,6} with probability pi , according
//
to the provided random number generator; here ,
//
p6 = 1 - p1 - p2 - p3 - p4 - p5
loaded_dice ( double p1 , double p2 , double p3 , double p4 ,
€
€
€
8
7
6
8
9
5
45
4
9
99
4
7
8
!
-
)
6 77
- * 1
*
H
H
=0
7
6
p_upto_6 = 1
p_upto_1
p_upto_5
operator()
i
x < p1 + . . . + pi .
p_upto_0
i
F
0.12
7
) *
H
€
*
& H
1
# include < iostream >
# include < IFM / loaded_dice.h >
/
*
!
2
<
"
Program 39:
* H &
* 1
1
*
! * 1
,
i
p6
{ { {
{
}{
6
pi
* + !
1
H
!
* 1 1
* 1
1
,
, 6
2
"
!
{{
{| {
// Prog : loaded_dice.C
// implement a class for rolling a loaded dice .
# include < IFM / loaded_dice.h >
namespace ifm {
// class loaded_dice : implementation
loaded_dice:: loaded_dice
( double p1 , double p2 , double p3 , double p4 , double p5 ,
ifm :: random & generator)
: p_upto_1 ( p1 ) ,
p_upto_2 ( p_upto_1 + p2 ) ,
p_upto_3 ( p_upto_2 + p3 ) ,
p_upto_4 ( p_upto_3 + p4 ) ,
p_upto_5 ( p_upto_4 + p5 ) ,
g ( generator)
{}
{ {{ { {| } { | } { {
operator()
6
1
+!
[0, 1)
p5
p 1 , . . . , p 5 p6 =
p4
H
private :
// p_upto_i is p1 + ... + pi
const double p_upto_1 ;
const double p_upto_2 ;
const double p_upto_3 ;
const double p_upto_4 ;
const double p_upto_5 ;
// the generator ( we store an alias in order to allow
// several instances to share the same generator)
ifm :: random & g;
};
} // end namespace ifm
* *
1
[0, 1)
, A 1
1
1 )*
H
p1 + p 2 + p 3
!
pi
H 7
1
1
*
* -
{ | {
{ { { }{ { {
return value is the outcome of rolling a loaded
dice , according to the probability distribution
induced by p1 ,... , p6
int operator ()();
} { 1
1
x
p3
p1 + p 2
x
0
p2
6
A
)*
1
p1 + . . . + pi−1
1
i
x
H
p1
i=1
* %
H
,
!
*
!
*
, !
* ? *
Program 38:
$
-
,
H ,
<
P5
,
// POST :
//
//
unsigned
1
, H - !
* * ! *
! !
* !
!
pi
, ,
H 1 , C !
1−
double p5 , ifm :: random & generator);
unsigned int loaded_dice :: operator ()()
{
double x = g ();
if ( x <= p_upto_1 ) return 1;
if ( x <= p_upto_2 ) return 2;
if ( x <= p_upto_3 ) return 3;
if ( x <= p_upto_4 ) return 4;
if ( x <= p_upto_5 ) return 5;
return 6;
}
} // end namespace ifm
// Prog : choosing_numbers.C
// let your loaded dice play against your friend ’s dice
// in the game of choosing numbers .
€
€
8
7
6
8
9
5
45
4
9
99
4
7
8
B
H !
*
1
H
H
ifm::random
ifm::loaded_dice
?
B
F
* 1
H # H
H
**
! , *
1
+
*
* A * * !
* * 1
*
*
1
!
A
! !
* 1 *
# C
!
*
A
%
!
!
H ! *
1
6
* H 7
1
operator<<
*
H * # * H *
B '
'
*
IH
!
)
!
*
'
'
.n
−
%
&
!
H *
**
! * * 1 IH * A
*
!
, !
* 1 *
*
, !
? F !
* *
H 1 *
*
! * ! , *
H
! !
*
!
*
* !
*
!
+
* H A
? {| } { | {
operator<<
*
* * (
*
€
B 1 +!
1
H !
*
! ,
+
{{
?
!
*
**
1
HI
(
Friend functions.
1
Dispositional.
*
* 1 {
{
friend
1
}
7
// output the result :
std :: cout < < " Your total payoff is "
< < your_total_payoff < < "\n ";
,
*
// now simulate 1 million rounds ( the train may be very late ...)
int your_total_payoff = 0;
for ( unsigned int round = 0; round < 1000000; round ++) {
your_total_payoff += your_payoff ( you () , your_friend ());
}
€7
// your friend ’s strategy may be to stay more in the middle
// and use the distribution (1/12 , 2/12 , 3/12 , 3/12 , 2/12 , 1/12)
double q = 1.0/12.0;
ifm :: loaded_dice your_friend (q , 2* q , 3* q , 3* q , 2* q , ansic );
<
// your strategy may be to prefer larger numbers and use
// the distribution (1/21 , 2/21 , 3/21 , 4/21 , 5/21 , 6/21)
double p = 1.0/21.0;
ifm :: loaded_dice you (p , 2*p , 3* p , 4* p , 5* p , ansic );
2
"
"
}{
int main () {
// the random number generator; let us use the generator
// ANSIC instead of the toy generator knuth8 ; m = 2^31;
ifm :: random ansic (1103515245u , 12345 u , 2147483648u , 12345 u );
7
Program 40:
!
} { | } { | {
{ { {
// POST : return value is the payoff to you ( possibly negative ) ,
//
given the numbers of you and your friend
int your_payoff ( unsigned int you , unsigned int your_friend)
{
if ( you == your_friend) return 0;
// draw
if ( you < your_friend ) {
if ( you + 1 == your_friend ) return 2; // you win 2
return -1;
// you lose 1
} // now we have your_friend < you
if ( your_friend + 1 == you ) return -2; // you lose 2
return 1;
// you win 1
}
4.3.11 Details
operator>>
operator>>
private
rational
class rational {
private :
friend std :: ostream & operator < < ( std :: ostream & o , rational r );
friend std :: istream & operator > > ( std :: istream & i , rational & r );
int n ;
int d ; // INV : d != 0
};
.d
4.3.12 Goals
return 0;
"
€
€
8
7
6
8
7
9
5
45
4
9
99
4
8
7
ifm::wio
0 .
0 " 0 # % " !
2
0
" !
# "
" !
2
! 4
! 0
2
# "
3
€
ansic
#
$
0 "
/ # "
random_triangle.C
t
<IFM/window>
(0, 0) (512, 0)
(256, 512)
/
4
$ 2 < !"
!
Exercise 125
Exercise 124
"
+
Clock
6
3 7
!
17
* bool
!
!
$
#
#
#
c
!
" )
+
/5
!
$ 0
$
$
$ #
$ !
c
!
# # $ 2 ! 2 .
0 / $ $
6 #
3 7 6 3 7 # # (
2 / 5 ) ) 2 - 1
! (
#
double
!
$
!
$
- ! !
(
0 "
1 1
! 2 .
$
!
" $
c
#
!
! #
%
1
1
A
3
7
6
B
*
, *
1
!
*
B
!
*
6
3
7
6
3 7
c c c c c c c c cc c
4.3.13 Exercises
" !
2 ( # $ - !
$
/
!
/ # 2
! 0
3 7
6
#
is_bool() const
76
" 3
" -
-
Tribool
6
2
1
" 3 #
0 -
int
#
$ ! 0 "
!
" "
! 2
1 /
! # #
1
Clock
" Exercise 123
-
Exercise 122
3
# $ #
2 $
!
-
B
&
*
, 1
!
1
,
!
*
*
3
6
€7
B
&
*
* * , *
&
?
3
7
6
c
1
1
H
* +
Operational.
# include < iostream >
class Clock {
Clock ( unsigned int h , unsigned int m , unsigned int s );
void tick ();
void time ( unsigned int h , unsigned int m ,
unsigned int s );
private :
unsigned int h_ ;
unsigned int m_ ;
unsigned int s_ ;
};
Clock :: Clock ( unsigned int & h ,
unsigned int & m ,
unsigned int & s )
: h_ ( h ) , m_ ( m ) , s_ ( s )
{}
void Clock :: tick ()
{
h_ += ( m_ += ( s_ + = 1 ) / 6 0 ) / 6 0 ;
h_ %= 24; m_ %= 60; s_ %= 60;
}
void Clock :: time ( unsigned int & h ,
unsigned int & m ,
unsigned int & s )
{
h = h_ ;
m = m_ ;
s = s_ ;
}
int main () {
Clock c1 (23 , 59 , 58);
tick ();
unsigned int h ;
unsigned int m ;
unsigned int s ;
time (h , m , s );
std :: cout < < h < < " : " < < m < < " : " < < s < < " \ n " ;
}
return 0;
t
t
€
€
8
7
6
8
7
4
9
99
4
9
5
45
8
$
2
" '
http://www.inf.ethz.ch/personal/gaertner/cv/lecturenotes/ra.pdf
" !
3 # 7
1!
http://banach.lse.ac.uk/form.html
"
$ Hint:
$
"
#
" " $
!
#
4
6 3 " 7
#
"
#
(
. $ / 2 - 2
2
0
6 3 7 2 / 5 ! !
$ " !
<
! "
!
#
" %
"
!
0 2
$ 0 # #
unsigned int
"
1
0 ! 2 ( $
!!
/
/
!
1
$ ansic
6
"
1
#
Exercise 128
Exercise 127
! #
0
1 # !
/
Exercise 126
m = 231
4.3.14 Challenges
!
2
6 2
#
!
!
3 7
"
# #
) !
" ! €$
€$
8
7
6
8
9
5
45
4
9
99
4
7
8
p
ow
*
1
* !
* ? ? ! * * * *
* * *
* * &
? *
?
* * * **
**
1
1
p
ow
!
*
*
*
*
*
* 1 Description
€$
1
1
"
"
"
"
"
"
"
"
"
"
"
1!
=
=<
#
Table 9:
%
2 ! ! 1
)
!
!
* , !
1
* !
!
€
1 C C C
) 1
€
Assoc.
C
!
€
C
C++ Operators
)* * * "
€
Prec.
!
€
Arity
!
€
Operator
::
[]
()
()
.
->
++
-dynamic_cast<>
static_cast<>
reinterpret_cast<>
const_cast<>
typeid
++
-*
&
~
!
+
sizeof
new
delete
(
)
->*
.*
€
!
! !
1
* Appendix A
, 6
7
* €
€
€
€
€
€
€
€
€
€
€
€
! €
!
€
!
"
€
!
€
!
€
!
$
€
!
€
!
€
!
€
!
€
!
€
!
€
!
€
€
!
€
€
!
€
€
!
!
€
!
€
!
€
*
/
%
+
<<
>>
<
>
<=
>=
==
!=
&
^
|
&&
||
=
*=
/=
%=
+=
-=
>>=
<<=
&=
^=
|=
?
throw
,
6
8
5
5
9
7
8
4
€
€$
H H
€
%( + ?
*+ ? *
,
H
H 1
€
**
H IH
$€
*
2
H H
? !
?@1! IH IH
$
H)
* +( * !
? 1
,
H IH
$
H %
* 1 * 1
?
H €$
H H
€ H $€
, *
H
? !
*
1
H
1 H H
! ! ,
1 *
H € H %*
**
**
H $€
H !
H
€$
H )
$€
& * )
H H €
H $€
H
1
€
*
*
H7
*
*
6
H H
!
€
1
*
*
* A
IH H H
H A
1 , 1 , * * H H * H H * 1
! €"$ € & €"$ € !
$" $" *
*
H
H
€
€
H
\0
HI H
* %
* *
) , €$ H 1 6 HI
€
1 H H A7
?€
\n
€$
%
* , * ? H
A
H
H € H
€
%
* , Index
€$
€
*
**
*
*
* H ) " € € H
€
H
H €
€
*
!! !
* ,
H €
" H € 1
" H
H €
H * , * !
H H
€
H
1
;
€
*
*
H H
%
€
€
$ 6
=
$$
H $7
"
#
*
1 H 1 H A 1 1
?
?
* H * €€
* "
,
H
* * ?
? € H
1
*
,
H
* :
A
H IH
"$
€
1
* *
? ? C 3 !
1
* & !
H *
H
€
H €" H H $
€
€
*
*
?
-
IH
$
"
*
1 1 1
* H A *
*
IH
H €
€
1
! !
! * H
* H
1
H
€
H ? *
*
! ! # H
:
! %
H (
H H & * H *
* € H &
€
H H
€
€
€
! !!
*
H
H "
1
*
H
6
7
H !
*
H €
!
H €
**
*
H ,
*
*
*
!6 * A
!
H
€"
H 7
*
*
H
$
*
*
*
H
$
*
+
;
%(
* 1
G * ,
H A € H €
*
&
&
H H €
€ &
H €
*
1
*
*
*
H
1
*
H A *
* H H A
"
?
& H H H
H € "$ €
G
* H H *
*
* H H
)
*
*
,
*
*
H $ H , 1
*
H $
A
1
*
*
G H H
*
" H
*
H $
*
1
*
H
)
€
!
*
1
* * H
€ H € €
!
*
H *
€ H €
1
H
H
H €
H €
H €
? 1
1
; ; H "
H
, "
H "
! !
! H * *
H , €$ H
€
; ;
!
)
* IH H "" " H
"$
*
* * , , H H
*
* , 1
H H * € € 1 H H 1
€$ €
" "$
*
2
H H $
H
"
"
* H 0 1
H
$6 "
=
$$
H $7
€
1
?
* !
€ € *
H 1 1 1 1
H 1
H ? 1
€ ?
"
H H H € "
H "
*
* '
? H '
$ 1
, H
€
H
$
*
*
* * H A H !
, H * !
*
&
H H $
H )
H H H
1 " H
€
"
*
*
1 ! * IH H
€ H
1
1 ;
1
!
G
H €
! ! 1 ) *
! ) 1
* H H H
H €
H H H € $
$ €$ $
"
H 7
*
)
1
)
* H
6
*
! !
1 H 1
H
*
1 H
€$
"
! ! ,
! H
H " H "
*
H H
H *
! !
,
H
,
H
1 1
H
"
€$
€$
8
6
8
6
"
€
!
H ! H
! ,
€$ / * H IH "
# * * ( 3 H H
!
! !
H €
H H € !
!
*
*
H €
"
!
!
*
H
H
€
H
€
"
!
*
C
H
1
HI *
H
H !
!
$ *
*
H
)
€$
*
€
€
H H
1
, 1 1
, *
! H H €
€ * €
H
€
G
!
* H * H €
* & )
€ H H H €
€
€ , H
! !
*
H €
H
H *
, , !
C
* C
* H
C
€€ H
H $
;
H H
€ H H "$ H * € €$
$€
*
G
H
* &
H !
* , ,
C * H H &
& H H €
€
€
!
*
*
H
1
H H /
*
H
H !
€$
H !
$€
,
1
* H
H )
)
H H , H H €
G G
! * H )
H ! ,
* * C
*
H
"
1 !
* * H
!
*
H
1
€
H * H H
€
1 ? H H
H H
H $ H $
H ,
H ! H
€$ H
* / ! H !
*
H
*
H
)*
€ H
€
H
H
C
1
"
€
€
H
C
) &
* H
1
,
€
*
1
!
*
H
€
*
*
*
1
HI
€
*
!
H H
H *
€
!
* H
€
1
€
H
"
)
H
)
,
1
H
€
* /
!
6 )
* H €$
H7
€
! !
* %* A * H
€
H
€
"
H
! ! !
* !
*
* H
H $
€
! !
1 ? *
! H €
* *
H €
! H €$ "
& & F !
H
1
H ** "
G
1
1 IH
H €
"
1
H
H H
€ €
A
(
, &
! ! ! * * H
€
H
* / & !
H H H H
€ H $
F G
H %
* 3
H € H
$ "
G F
* %
* 3
*
!
* ,
) H € 1
H
H
1
€
H €
, ! H 0
*
* ! )
H 6
H
H €$
?
H 7
""
! !
* *
H ! !
H € H " €
H !
!
*
H €
H
IH
$
$
/ *
*
,
€ 1 C
*
€ H
"
)*
% $ * H
&
H
$
F
#
" H
H )*
H
,
* H €$
G G
, * H 1 ,
H
)
*
* ,
H
*
, )
* 6
*
, H €
7
H
H
€$
* !
H €
,
H !
$€
H !
!
€$
€$
8
6
8
6
"
1
H
,
H H * * * * * H H IH
€
?
*
H
€ H
€
* €
?@!
H 1
€
H
€
!
H
€
,
H
+
H
€
*
) H
€
H *
H * €
* **
* H
H +
H
**
1
€
€ H !
*
1
1
*
*
*
H €
H
H ?
, H €
€
H
$
1 ) * * IH
"
€
0 1 H H ) H A
H € " !
H H € €
G ) H H 1
* H " "
H H "
*
,
! * , H , " IH H
€
"
H
$
H H
" "
*
! 1
*
IH € H 1
+( + !
H €" * H €
H €
€
H €
,
<< !
H
"
&
1
! 1
? ? * *
H * &
€ H 1
*
*
,
H
*
H H
,
H
*
H
*
, * , H 1
€ H "
* 1
A
HI "
*
H
I
H
) H € H A
H
€
IH €
:
* * IH
"
H
C
,
H
C
H , $ H *
€ H !
$
B
* *
H € " H
$€
H
€
H
€
*
€
€
F
*
H
H
$A
:*
H €$ H
! G
* * € 1
H
€
€
€
0 * 1 * A H * %
H $ 6
€
=
$
$€
H 7
€
: 1 H H €
"
*
HI H
H
"
€
H H
*
€
0
H
$
"
H 1
*
:
H
H
€
"
, ?
?
,
*
,
H
1 ? !
* " , H
! ! , 1
* ? 1 H , H €
H
€ H (
H *
H
! !
! H *
H $€
€$
* ! H 1
€$ H €
"
*
! IH
* €
* € H
€
* H
*
H €
€
!
"
€
)
H
! H & ! * *
H H 1
€ H
"
H "
1
H H $€ H H * € €
" *
G
€
* &
H
€
*
+
€
1
HI
$
, H
!
H
€
H
$
H
H
* A H *
$ 1 H € H €$
H $
2
H HI ! )
€
€
*
1
€ HI
1
*
* A ?
:
?
H
€ 1
H
H
$€
& &
* H $ H
€
3
1 H * H ? F
* H A €
&
1 ) * *
H
€
H
H & H
?
$
H €
! *
* , H H H €
H ? *
1
# , * * 1
H
H (
$
"
, H
H
€
€$$
8
6
8
6
H "
H
*
"
€$
G
1
€
*
H H
*
*
H
"
!
*
H €
"
H
* H
H
H * , H
H H H 1
*
C
H
,
H H
€
€
H
"
€
! * 0 H
*
*
H €
€
!
)
& * H H
€ H
€
!
*
H H
€
,
!
*
*
H €
*
*
H
€
A
,
H
€
H
€ H
!
!
* *
H H H * ? * ? * H )
)
H
H ? * H
: 0
* )
H)
H ( H " €
H €
6
H
H $€
* 1
H (7
1
H *
H
!
* ? H H €
€$
1 1
* *
H
$ H H H
HI *
)
H €
H
H
IH
H H H
€
, A H H
H
IH
H * H * H ( ? @?!
!
1
* /
IH "
€
H)
H H * , €
* H 1
H $
H
H H H H
* * ?
* * ! H H *
,
! G
, H
!
*
H
1 1 1
H H
H
*
* ! !
H ! G
)
*
€ H
H
€$
1
?
,
! H
* H H H € $
*
H H €
H "
€
*
!
! ! 1
! * *
HI IH H € €
$€ $ , H ! !
!
H * € H
€
! !
!
,
H 1
, * , < , H H €$" *
1
H
H
€$
"
€
H
$
C
&
& H " H €
" €
"
€
? ! H * €
,
, H H $
H *
H
*
H €
H
)
* H H "
$
H
G
H ?
!
:# H ! H ? *
%
* H ( € * H
H "
H
H H €
$
H
"
$
"
"
, ? H * € H H
€
H $
! !G * H IH
1
H
6 "$
H "$
H 1 7
IH
H €$
1 *
1
H
<* 1
H
€"$
, H
H
* 1 ? ?
H H "
C
H * ? *
H H ?
*
? , *
H H * H " H H
* 12
*
H
12
H *
€
* H H €
! H ,
€
8
6
8
6
<< *
<< *
H €
€
* H
!
*
HI
$
H
1 H € "
, H H ,
*
1 1 IH H ? H € H " €
H *
*
* H ,
H - - - H H " *
! % *
6$ H $
H € H
=€ "
€
"
$
H 7
$
"
(
* *
:
" * &
H "
, H "
< <
C
H $
H
1 H
,
G
€ *
IH
* * * H H
$
*
1
!
H
*
H
€
H * * * H * H H
H *
,
H € H
€
1
,
H
H €
H $
A *
€
*
IH
H €
"
* H , 1 €
? :
H 1 H ? € H €
" € H H
<< *
H H € €
* H , H
,
H
H)
"
€
<< *
!
€
( , ! * /
H €$
IH
*
IH
H
€ €
*
) H )
€
H €
*
€
* 1
€
H $
, *
H €
*
!
IH
!
€
€
?@!
* *
, 1
H A %
+(
H H H €$ € €
H *
* 1
* ( 1 1
H A * *
€ H H * 1 * 1
! H
H
* $€ * * H H € €
H $€
H
H
**
%
*
1
€
H €
,
C
1
C
"
A
(
<< << << C &
H H H
$
$ & &
* !
*
H € H " *
€
H €
C
! * & !
**
H * € H H
* & IH € €
€ "
H
H
€
€
1
H A H H $ $ H H €
*
H
" !
H IH
* H :
*
* ! !
! H H 1
* * * H
H * H ** H
$€
H €$ 6
H %
€$
H :7
1
*
! ? ! H ! H ) )* HI * H " HI $ $ H H
H H
H
2
* ! H H $ $
1 H
HI €
€
* H *
H A H " H
( C
/ *
& , H
€
€ (
/
* H
* , :
6
H =
$
"
H 7
? H € H
"
H H
H
H
H$
H
, !
*
H
€
! * * , *
H € * * , *
H H 1 " $
H H " $ H G
! ! * H $
1
H
<< *
H H € €
<< *
! !
* * H €$ H $
H €$
? ! * *
12 H IH H * H $ €
€ €
"
<< *
H €
H
1
! * & *
G H
$
! ! * * 3
H €$ H
€
H
$
€
8
6
8
6
&
"
H !
* H
*
H €
C
,
€
, ! G
H
* H H , *
H
H ! H ! H
"
€ € H $
& 1 1
, 1 H H € H $ "
,
, , ,
H 1 !
! 1 1
* * H & H
*
H
€ H
€
H
,
€
H ) !
* 12
€
H €
! H
,
*
*
H
$
,
H H
€
* G
* * H
H H €
€ €
H €
H $
$
,
1 H € G
!
H €
€
,
H !
€
H
1 H " $ H
H ! H
€
H
! !
H * H
$
*
€
H
,
6
* H7
€
! , H !
1 €
H €
,
! *
€
! !
! )
H
€$
! !
H 1 € H
H "
! * H
H
€$ €
! !
G H 1
H
H $
! !
,
) 1
H
€ H
$€
, ,
H
€
H
?
?
H $
!
* € H " €
?
* &
, * H ,
H
€
*
H H H
"
€
H
$€
H $ 1
H "
H !
* :
;
1
,
H
H €
&
*
H
H !
€
!
*
$
* H
$
€
H "
€
&
C
H *
! 1
IH * H H
"
"
H €
H €
H $
*
G G H 1 ? C H &
H €
"
8
6
8
6