0% found this document useful (0 votes)
506 views7 pages

LatexC++ Proposed Exercises (Chapter 7: The C++ Programing Language, Fourth Edition) - Solution

This document contains C++ code examples related to pointers, references, arrays, and const correctness. It demonstrates different ways to use pointers to primitive data types and objects, pointer arithmetic, arrays of pointers, passing pointers to functions, and const qualifiers. Key concepts covered include pointers to values on the heap vs stack, pointer to pointer, returning addresses from functions, arrays, passing by reference vs value, const pointers and references, and the difference between lvalues and rvalues. The examples provide annotations and explanations for common errors and how to resolve them.

Uploaded by

Mauricio Bedoya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
506 views7 pages

LatexC++ Proposed Exercises (Chapter 7: The C++ Programing Language, Fourth Edition) - Solution

This document contains C++ code examples related to pointers, references, arrays, and const correctness. It demonstrates different ways to use pointers to primitive data types and objects, pointer arithmetic, arrays of pointers, passing pointers to functions, and const qualifiers. Key concepts covered include pointers to values on the heap vs stack, pointer to pointer, returning addresses from functions, arrays, passing by reference vs value, const pointers and references, and the difference between lvalues and rvalues. The examples provide annotations and explanations for common errors and how to resolve them.

Uploaded by

Mauricio Bedoya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 7 Solutions

A. Pointer and Reference


1. Pointer to double
1

# i n c l u d e < iostream >

2
3
4

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


double * data { new double { 1 2 . 5 } } ;

std : : cout << data << std : : endl ;


/ / Memory address
std : : cout << * data << std : : endl ; / / Memory v a l u e

6
7
8

d e l e t e data ;

9
10

main.cpp
2. Pointer to Pointer
1

# i n c l u d e < iostream >

2
3
4

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


double * data { new double { 1 2 . 5 } } ;

std : : cout << " P o i n t e r data \ n " ;


std : : cout << data << std : : endl ;
/ / Memory address
std : : cout << * data << std : : endl ; / / Memory v a l u e

6
7
8
9

std : : cout << " P o i n t e r data1 \ n " ;


double * data1 { data } ;
std : : cout << data1 << std : : endl ;
/ / Memory address
std : : cout << * data1 << std : : endl ; / / Memory v a l u e

10
11
12
13
14

d e l e t e data ;

15
16

main.cpp
3. I will state errors per line:
Line 4: 3.15 must be float or double not int. No error.
Line 5: 3.15 must be float or double not int. Error.
Line 6: 3.15 must be float or double not int. No error.
Line 7: 3.15 must be float or double not int. Error.
4. Not implemented.

5. Function Add
a., b., c.
1

# i n c l u d e < iostream >

2
3

double * Add ( c o n s t double& data1 , c o n s t double * data2 ) {

r e t u r n ( new double { data1 + ( * data2 ) } ) ;

5
6

7
8
9
10

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


double data1 { 1 2 } ;
double * data2 { new double { 2 3 } } ;

11

double * data3 { Add ( data1 , data2 ) } ;


std : : cout << " " << data3 << std : : endl ;
std : : cout << " " << * data3 << std : : endl ;

12
13
14
15

d e l e t e data2 ;
d e l e t e data3 ;

16
17
18

main.cpp
d. There are several errors: Result is local in scope Add and its address is returned (warning or error
here). No value is printed. Make the necessary corrections in this code. Start from the function
definition.
e. When I comment line 14, the code print the correct value of Add function.
f. Did not delete data1 pointer.
6. Two errors:
data2 is not a pointer and there is no need to call delete.
data3 address is equal to data2 address. Error when deleting data3.

B. Array
7. Array with pointers. Two different implementations (one is commented).
1

# i n c l u d e < iostream >

2
3
4
5

v o i d print ( c o n s t i n t * x ) {
std : : cout << * x << std : : endl ;
}

6
7
8
9

v o i d deleting ( c o n s t i n t * x ) {
delete x;
}

10
11

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

12

i n t * data1 { new i n t { 1 2 3 } } ;
i n t * data2 { new i n t { 2 3 } } ;
i n t * data3 { new i n t { 5 3 7 } } ;

13
14
15
16

i n t * My_Ptr_data [ 3 ] { data1 , data2 , data3 } ;

17
18

/ * * * Helper F u n c t i o n * * * /
std : : for_each ( std : : begin ( My_Ptr_data ) , std : : end ( My_Ptr_data ) , print ) ;
std : : for_each ( std : : begin ( My_Ptr_data ) , std : : end ( My_Ptr_data ) , deleting ) ;

19
20
21
22
23
24

//

25

//

26

/ * * * Lambda * * * /
s t d : : f o r _ e a c h ( s t d : : begin ( My_Ptr_data ) , s t d : : end ( My_Ptr_data ) , ( [ ] ( i n t * x ) { s t d : : c o u t << * x << s t d : : e n d l ; } ) ) ;
s t d : : f o r _ e a c h ( s t d : : begin ( My_Ptr_data ) , s t d : : end ( My_Ptr_data ) , ( [ ] ( c o n s t i n t * x ) { d e l e t e x;}) ) ;

main.cpp
8. Array of integers
1

# i n c l u d e < iostream >

2
3
4
5

v o i d print ( c o n s t i n t & x ) {
std : : cout << x << std : : endl ;
}

6
7

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

i n t data1 { 1 2 3 } ;
i n t data2 { 2 3 } ;
i n t data3 { 5 3 7 } ;

9
10
11
12

i n t My_data [ 3 ] { data1 , data2 , data3 } ;

13
14

/ * * * Helper F u n c t i o n * * * /
std : : for_each ( std : : begin ( My_data ) , std : : end ( My_data ) , print ) ;

15
16
17

/ * * * Lambda * * * /
std : : for_each ( std : : begin ( My_data ) , std : : end ( My_data ) , ( [ ] ( i n t x ) { std : : cout << x << std : : endl ; } ) ) ;

18
19

20

main.cpp

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

# i n c l u d e < iostream >

2
3
4

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t my_array [ 1 0 ] ;

std : : for_each ( std : : begin ( my_array ) , std : : end ( my_array ) , ( [ ] ( i n t & x ) {


x = 0;
std : : cout << x << std : : endl ; } ) ) ;

6
7
8
9

main.cpp
13. Array of Int with default value equal to two.
1

# i n c l u d e < iostream >

2
3
4

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t my_array [ 1 0 ] ;

std : : for_each ( std : : begin ( my_array ) , std : : end ( my_array ) , ( [ ] ( i n t & x ) {


x = 2;
std : : cout << x << std : : endl ; } ) ) ;

6
7
8
9

main.cpp
14. Matrix with arrays This is not efficient. If you index the member elements of the matrix, you can use a vector
instead of an array. This can be part of an interview question.
1

# i n c l u d e < iostream >

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t Matrix1 [ 3 ] [ 3 ] ;
/ * * * Set c o n t e n t o f M a t r i z * * * /
/ / Row 1
Matrix1 [ 0 ] [ 0 ] = 1 ;
Matrix1 [ 0 ] [ 1 ] = 2 ;
Matrix1 [ 0 ] [ 2 ] = 3 ;
/ / Row 2
Matrix1 [ 1 ] [ 0 ] = 7 ;
Matrix1 [ 1 ] [ 1 ] = 8 ;
Matrix1 [ 1 ] [ 2 ] = 9 ;
/ / Row3
Matrix1 [ 2 ] [ 0 ] = 4 ;
Matrix1 [ 2 ] [ 1 ] = 5 ;

Matrix1 [ 2 ] [ 2 ] = 6 ;

17
18

main.cpp
15. I will implement a matrix with vector, indexing the elements of the matrix. The exercise deals with square
matrix, then defining the size of the vector is trivial. Indexing by row. Consider the addition of two matrix A
and B.

1 2 3
A= 7 8 9
4 5 6

11 12 1
B= 2 3 5
13 89 7

1
2
3
4

#include
#include
#include
#include

< iostream >


<vector >
<algorithm >
<functional >

5
6
7
8
9

std : : vector< i n t > Add ( std : : vector< i n t >& A , c o n s t std : : vector< i n t >& B ) {
std : : transform ( A . begin ( ) , A . end ( ) , B . begin ( ) , A . begin ( ) , std : : plus< i n t > ( ) ) ;
return A;
}

10
11
12
13
14
15

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


/ * ** Matrix A setting ** * /
std : : vector< i n t > A = { 1 , 2 , 3 , 7 , 8 , 9 , 4 , 5 , 6 } ;
/ * ** Matrix A setting ** * /
std : : vector< i n t > B { 1 1 , 1 2 , 1 , 2 , 3 , 5 , 1 3 , 8 9 , 7 } ;

16

std : : vector< i n t > Addition = Add ( A , B ) ;

17
18

main.cpp

C. Pointers and const


16.
1
2
3

# i n c l u d e < iostream >


# include <array >
#include <string >

4
5
6
7

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


/ * * * var2 i s a p o i n t e r t o double * * * /
double * var2 ;

8
9
10
11

//

/ * * * Var3 i s a c o n s t p o i n t e r t o a c o n s t f l o a t ( both e q u i v a l e n t ) * * * /
f l o a t c o n s t * c o n s t var3 { n u l l p t r } ;
c o n s t f l o a t * c o n s t var3 = nullptr ;

12
13
14

/ * * * A r r a y o f c o n s t p o i n t e r t o double * * * /
std : : array< c o n s t double * , 3> my_Array ;

15
16

/ * * * Constant A r r a y o f p o i n t e r t o c o n s t a n t s t r i n g * * * /

const

17
18

std : : string * c o n s t My [ 2 ] { new c o n s t std : : string ( " aa " ) ,new c o n s t std : : string ( " bb " ) } ;

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 .

# i n c l u d e < iostream >

2
3
4
5

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


/ * * * E x e r c i s e 17 ( No e r r o r o r warning message ) * * * /
i n t * data ;

/ * * * E x e r c i s e 18 ( No e r r o r o r warning message ) * * * /
c o n s t double * data1 ;

7
8
9
10
11

//

/ * * * E x e r c i s e 19 ( E r r o r message uncomment l i n e ) * * * /
const std : : s t r i n g * const ;

12

/ * * * E x e r c i s e 20 ( No e r r o r o r warning message ) * * * /
c o n s t double * c o n s t data2 { new c o n s t double { 1 2 . 1 4 } } ;

13
14
15

main.cpp

21
1

# i n c l u d e < iostream >

2
3
4
5
6

i n t * print ( i n t * data1 , i n t * data2 )


{
r e t u r n ( new i n t { * data1 + * data2 } ) ;
}

7
8
9
10

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


c o n s t i n t * data1 { new i n t { 1 } } ;
c o n s t i n t * data2 { new i n t { 7 } } ;

11

i n t * result { print ( c o n s t _ c a s t < i n t * >(data1 ) , c o n s t _ c a s t < i n t * >(data2 ) ) } ;

12
13

std : : cout << " 1 + 7 i s : " << * result << std : : endl ;

14
15

main.cpp
22 print1 accept as parameter non constant members. Then, line 19 and 20 will generate an error message.

D. lvalue & rvalue


Solution not provided for this section.

You might also like