C++ Proposed Exercises (Chapter 6: The C++ Programing Language, Fourth Edition) - Solution
C++ Proposed Exercises (Chapter 6: The C++ Programing Language, Fourth Edition) - Solution
1. Declarations
1
2
3
4
5
main.cpp
2. Size of variables
1
2
3
4
5
6
8
9
10
11
std : : cout << " I n i t i a l i z e i n t e g e r s i z e : " << s_data1 << std : : endl ;
std : : cout << " I n i t i a l i z e i n t e g e r s i z e : " << s_data2 << std : : endl ;
std : : cout << " U n i t i a l i z e i n t e g e r s i z e : " << s_data3 << std : : endl ;
12
13
14
15
main.cpp
3. Assignment
1
2
3
4
5
6
7
8
std : : cout << data1 << " + " << data2 << " = " << data3 << std : : endl ;
9
10
main.cpp
4. Bool function with integers. The output is 1 if the condition is true and 0 if the condition is false.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
main.cpp
5. Bool function with integers. The output is 1 is the condition is true and 0 if the condition is false.
1
2
3
4
5
6
7
8
9
10
11
main.cpp
6. Bool function with char. Templates are used to make a function independent of the type (Generic Programming).
1
2
3
4
5
6
7
8
9
10
11
main.cpp
7. My personal bool
1
2
3
4
5
6
7
8
9
10
11
main.cpp
8. Constant variables should always be initialized. If you dont initialize a constant expression you will get an
error message. No example provided.
9. The purpose of this exercise is to start differentiate between compile and run errors. Search in google for
more information. Identify that when you compile data1 value will not be 3.1415.
10. Lets see the value of data1.
1
2
3
4
5
7
8
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/ * * * Option 2 ( D e f i n i t i o n ) * * * /
i n t data3 { ' z ' } ;
i n t data4 { 'w ' } ;
std : : cout << " L e t t e r z v a l u e i s : " << data3 << std : : endl ;
std : : cout << " L e t t e r w v a l u e i s : " << data4 << std : : endl ;
}
main.cpp
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/ * * * Option 2 ( D e f i n i t i o n ) * * * /
char data4 { 0 } ;
char data5 { 1 0 } ;
char data6 { 5 5 } ;
std : : cout << " I n t 0 v a l u e i s : " << data4 << std : : endl ;
std : : cout << " I n t 10 v a l u e i s : " << data5 << std : : endl ;
std : : cout << " I n t 55 v a l u e i s : " << data6 << std : : endl ;
}
main.cpp
2
3
4
5
6
c o n s t double pi1 { 3 . 1 4 1 5 } ;
std : : cout << pi1 << std : : endl ;
/ / Run Time c o n s t a n t
constexpr double pi { 3 . 1 4 1 5 } ;
std : : cout << pi << std : : endl ;
}
/ / Compile Time c o n s t a n t
7
8
9
10
main.cpp
15 Declaration
2
3
c o n s t double pi1 { 3 . 1 4 1 5 } ;
std : : cout << pi1 << std : : endl ;
double data1 { ( pi1 * 4 ) } ;
5
6
7
/ / Run Time c o n s t a n t
constexpr double pi { 3 . 1 4 1 5 } ;
/ / Compile t i m e c o n s t a n t
std : : cout << pi << std : : endl ;
constexpr double data2 { ( pi * 4 ) } ;
}
9
10
11
12
main.cpp
16 Be careful with types input and output when declaring functions.
1
2
3
4
5
6
/ * * * Global f u n c t i o n * * * /
i n t Add ( c o n s t f l o a t & data1 , c o n s t f l o a t & data2 ) {
r e t u r n ( data1 + data2 ) ;
}
7
8
9
10
11
12
f l o a t data3 { 3 . 1 4 5 } ;
f l o a t data4 { 9 . 1 8 9 } ;
i n t addition2 { Add ( data3 , data4 ) } ;
13
14
15
/ / ( 1 2 ) No sense a t a l l
16
17
main.cpp
17 Pointer input and output in functions
1
2
3
4
5
6
/ * * * Global f u n c t i o n * * * /
double * by2 ( double * data ) {
r e t u r n ( new double ( * data * 2 ) ) ;
}
7
8
9
10
11
12
13
d e l e t e data ;
d e l e t e result ;
14
15
16
main.cpp
18 Solution not provided. Reader can do it easily.
19 Function f(x)
1
2
3
4
5
6
7
8
/ * * * Global f u n c t i o n * * * /
double f ( c o n s t double& data ) {
c o n s t double e { 2 . 7 1 8 2 8 } ;
r e t u r n ( pow ( e , data ) ) ;
/ / pow f u n c t i o n i s i n math . h
}
9
10
11
12
13
14
15
main.cpp
20 Numeric Limits
1
2
3
std : : cout << " F l o a t max number : " << ( std : : numeric_limits< f l o a t > : : max ( ) ) << std : : endl ;
std : : cout << " Double max number : " << ( std : : numeric_limits<double > : : max ( ) ) << std : : endl ;
std : : cout << " I n t max number : " << ( std : : numeric_limits< i n t > : : max ( ) ) << std : : endl ;
}
5
6
7
8
main.cpp
C.
a. No error, var1 is global and is defined before function f.
b. Error, var1 is global but is defined after function f.
c. Error. f doesnt return anything is void.
d. Erro. f is a function.
e No error.
D. Initialization
a. int.
b. char.
c. float or double.
d. int.
e. int.
f. string.
g. Undefined type.
h. int.
i. char.
Follow items h and i as example of proper initialization.