latexc++ proposed exercises (chapter 7: the c++ programing language, fourth edition) - solution -
DESCRIPTION
.TRANSCRIPT
-
Chapter 7 Solutions
A. Pointer and Reference
1. Pointer to double
1 # inc lude 2
3 i n t main ( i n t argc , const char * argv [ ] ) {4 double *data {new double { 1 2 . 5 } } ;5
6 std : : cout
-
5. Function Add
a., b., c.1 # inc lude 2
3 double *Add ( const double& data1 , const double * data2 ) {4
5 r e t u r n (new double {data1 + ( *data2 ) } ) ;6 }7
8 i n t main ( i n t argc , const char * argv [ ] ) {9 double data1 { 1 2 } ;
10 double *data2 {new double { 2 3 } } ;11
12 double *data3 {Add (data1 , data2 ) } ;13 std : : cout
-
B. Array
7. Array with pointers. Two different implementations (one is commented).
1 # inc lude 2
3 vo id print ( const i n t * x ) {4 std : : cout
-
9. Not implemented.
10. Not implemented.
11. Only provide answer, give an explanation.
a. 2.
b. 2.
c. 2.
12. Array of Int with default value equal to zero.
1 # inc lude 2
3 i n t main ( i n t argc , const char * argv [ ] ) {4 i n t my_array [ 1 0 ] ;5
6 std : : for_each (std : : begin (my_array ) , std : : end (my_array ) , ( [ ] ( i n t & x ) {7 x = 0;8 std : : cout
-
17 Matrix1 [ 2 ] [ 2 ] = 6 ;18 }
main.cpp
15. I will implement a matrix with vector, indexing the elements of the matrix. The exercise deals with squarematrix, then defining the size of the vector is trivial. Indexing by row. Consider the addition of two matrix Aand B.
A =
1 2 37 8 94 5 6
B =
11 12 12 3 513 89 7
1 # inc lude 2 # inc lude 3 # inc lude 4 # inc lude < f u n c t i o n a l >5
6 std : : vector< i n t > Add (std : : vector< i n t >& A , const std : : vector< i n t >& B ) {7 std : : transform (A .begin ( ) , A .end ( ) , B .begin ( ) , A .begin ( ) , std : : plus< i n t > ( ) ) ;8 r e t u r n A ;9 }
10
11 i n t main ( i n t argc , const char * argv [ ] ) {12 / * * * Mat r i x A s e t t i n g * * * /13 std : : vector< i n t > A={1 ,2 ,3 ,7 ,8 ,9 ,4 ,5 ,6 } ;14 / * * * Mat r i x A s e t t i n g * * * /15 std : : vector< i n t > B {11 ,12 ,1 ,2 ,3 ,5 ,13 ,89 ,7} ;16
17 std : : vector< i n t > Addition = Add (A , B ) ;18 }
main.cpp
C. Pointers and const
16.1 # inc lude 2 # inc lude 3 # inc lude < s t r i n g >4
5 i n t main ( i n t argc , const char * argv [ ] ) {6 / * * * var2 i s a p o i n t e r to double * * * /7 double * var2 ;8
9 / * * * Var3 i s a const p o i n t e r to a const f l o a t ( both equ iva len t ) * * * /10 / / f l o a t const * const var3 { n u l l p t r } ;11 const f l o a t * const var3 = nullptr ;12
13 / * * * Array o f const p o i n t e r to double * * * /14 std : : array my_Array ;15
16 / * * * Constant Array o f p o i n t e r to constant s t r i n g * * * /
-
17 const std : : string * const My [ 2 ] { new const std : : string ( " aa " ) ,new const std : : string ( " bb " ) } ;18 }
Some other example found in the web (this is important):
int * (pointer to int)
int const * (pointer to const int)
const int * (pointer to const int)
int * const (const pointer to int)
int const * const (const pointer to const int)
const int * const (const pointer to const int)
17,18,19,20 .
1 # inc lude 2
3 i n t main ( i n t argc , const char * argv [ ] ) {4 / * * * Exerc ise 17 (No e r r o r or warning message ) * * * /5 i n t * data ;6
7 / * * * Exerc ise 18 (No e r r o r or warning message ) * * * /8 const double * data1 ;9
10 / * * * Exerc ise 19 ( Er ro r message uncomment l i n e ) * * * /11 / / const s td : : s t r i n g * const ;12
13 / * * * Exerc ise 20 (No e r r o r or warning message ) * * * /14 const double * const data2 {new const double { 1 2 . 1 4 } } ;15 }
main.cpp
211 # inc lude 2
3 i n t * print ( i n t * data1 , i n t * data2 )4 {5 r e t u r n (new i n t { *data1 + *data2 } ) ;6 }7
8 i n t main ( i n t argc , const char * argv [ ] ) {9 const i n t * data1 {new i n t { 1 } } ;
10 const i n t * data2 {new i n t { 7 } } ;11
12 i n t * result {print ( const_cast < i n t * >(data1 ) , const_cast < i n t * >(data2 ) ) } ;13
14 std : : cout
-
D. lvalue & rvalue
Solution not provided for this section.