You are on page 1of 7

Proposed Exercises Chapter 7

In this document you can find exercises related to the content of C++ Programming Language (Fourth Edition).
Solution is provided to some problems. However, if you follow the book carefully, you will be able to solve the
proposed problems easily.
Introduction material is very dense and thats why no exercise material is elaborated.
Dont get panic If you have difficulties trying to solve these exercises. The material provided in the Introduction
section (Part I) and chapter seven, allow you to PROPOSE some solution. Some recommendations are:
1. Try to mimic the Programming Technique found in the chapter.
2. For now, dont worry for efficiency, make the program work.
3. Declaration implies definition (initialization).
4. Test your code in main().
5. Smile, you are becoming part of the C++ community.

A. Pointers and Reference.


1. Declares a pointer to double and print the address and value.
2. Declares a pointer to double that point to the address of pointer implemented in (1). Print both address and
values.
3. Identify visually if any error is invoked:
1
2
3
4
5
6
7

# include < iostream >


i n t main ( ) {
const i n t data = 3 . 1 5 ;
const i n t data1 { 3 . 1 5 } ;
const i n t data2 = new i n t ( 3 . 1 5 ) ;
const i n t data3 {new i n t { 3 . 1 5 } } ;

Now, use your IDE and compare results.


4. I forget to free memory in the previous exercises. Please do it for me.
5. (*) Create a function "Add" with the following characteristics:
(a) Two double values input. One is a pointer to double, the other is an lvalue.
(b) Output is a pointer to double.
(c) Declares a pointer to value equal to Add(x,y). Print the address and value of the pointer.
(d) Check visually for any error in the following code:

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

# include < iostream >


using namespace s t d ;
double Add ( double value1 , double value2 ) {
double r e s u l t = value1 + value2 ;
return & r e s u l t ;
}
i n t main ( ) {
double data1 {new double { 1 2 . 5 } } ;
double data2 { 1 0 . 5 } ;
void R e s u l t { Add ( data1 , data2 ) } ;
/ / Memory i s P r i n t e d
c o u t << &R e s u l t << e n d l ;
/ / Value i s P r i n t e d
c o u t << s t a t i c _ c a s t <double >( R e s u l t ) << e n d l ;
}

(e) Comment line 14 and compile again. Any change ?


(f) Any memory deallocation error in the previous code?
6. Prove the following code and search for any warning message in memory deallocation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# include < iostream >


using namespace s t d ;
i n t main ( ) {
double data1 {new double { 1 2 . 5 } } ;
double data2 { 1 0 . 5 } ;
void data3 = data1 ;
c o u t << data1 << e n d l ;
c o u t << data2 << e n d l ;
d e l e t e data1 ;
d e l e t e data2 ;
d e l e t e data3 ;
}

Make the necessary correction(s).

B. Array
7. Implement an array (3 elements) of pointers to int and initialize them with value: 1, 2, 3. Use for loop to print
the value in array. Remember to deallocate memory appropriately (Hint: use delete).
8. Repeat exercise 7 with int (not pointers).
9. Implement an array (10 elements) of pointer to char and initialice them with valur: a,b,c,d,e,f,.... . Use for
loop to print the value in array. Remember to deallocate memory appropriately (Hint: use delete).
10. Repeat exercise 9 with char (not pointers).

11. Test visually for any errors in the following code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

# include < iostream >


using namespace s t d ;
i n t main ( ) {
/ / S e l e c t t h e c o r r e c t one ( Both can be ok )
/ One /
/1/
i n t data [ 1 2 ] = { 1 . 1 , 2 . 2 , 3 . 3 , 4 . 4 , 5 . 5 } ;
/2/
double data1 [ 1 2 ] = { 1 . 1 , 2 . 2 , 3 . 3 , 4 . 4 , 5 . 5 } ;
/ Two /
/1/
char data2 [ ] = { " Hola " , " H e l l o " , " Halo " } ;
/2/
s t r i n g data2 [ ] = { " Hola " , " H e l l o " , " Halo " } ;
/ Three /
const i n t s i z e = 5 ;
/1/
i n t data3 [ s i z e ] ;
f o r ( i n t i = 0 ; i ! = 5 ; ++ i )
{
data3 [ i ] = i ;
}
f o r ( i n t i = 0 ; i ! = s i z e ; i ++)
{
c o u t << data3 [ i ] << e n d l ;
d e l e t e data3 [ i ] ;
}
/2/
i n t data3 [ s i z e ] = { n u l l p t r , new i n t { 2 } , n u l l p t r , new i n t { 4 } , n u l l p t r } ;
f o r ( i n t i = 0 ; i ! = 5 ; ++ i ) {
c o u t << ( ( data3 [ i ]==NULL ) ? ( 0 ) : ( data3 [ i ] ) ) << e n d l ;
d e l e t e data3 [ i ] ;
}
}

Comment the incorrect code and try to compile.


12. Declares an array of int with size 10 and default value equal to zero.
13. Change the default value of the previous array to 1.
14. (*)Declares a two dimensional array that behaves like a matrix. Prove the implementation with:

1 2 3
A= 7 8 9
4 5 6
15. (*)Define a function Addition with:
(a) Two input two dimensional matrix of the same size.
(b) Return a two dimensional matrix equal to de addition operations of the two matrix provided.

C. Pointers and const


A trick to use and differentiate pointers expressions, is to read the declaration from right to left.
16. Write the definition of the following declarations (example: int* data: data is a pointer to integer):
const int * var1

:
:
:
:
:

var2 is a pointer to double


var3 is a constant pointer to constant float
var4 is an array of constant pointers to double
var5 is a constant array of pointers to constant string

Figure 1: Constants & Pointer Declaration


17. Declare (no initialization) a pointer to integer. Any warning or error message?
18. Declare (no initialization) a pointer to a constant double. Any warning or error message?
19. Declare (no initialization) a constant pointer to a constant string. Any warning or error message?
20. Declare a constant pointer to a constant double with default value equal to nullptr.
21. (*)Declare tow pointers to constant integers with initial value 1 and 7. Implement a function called print that
add (1+7) with the following characteristics:
(a) Two pointer to integer as input.
(b) One pointer to integer as output.
Use std::cout to prompt the result (Hint: Use const_cast <> ()).
22. Check the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# include < iostream >


using namespace s t d ;
void p r i n t ( const i n t & data ) { c o u t << " Data v a l u e i s : " << data << e n d l ; }
void p r i n t 1 ( i n t & data ) { c o u t << " Data v a l u e i s : " << data << e n d l ; }
i n t main ( ) {
const i n t data1 {new i n t { 3 } } ;
const i n t data2 { 4 } ;
i n t data3 { 5 } ;
/ / Test w i t h p r i n t
p r i n t ( data1 ) ;
p r i n t ( data2 ) ;
p r i n t ( data3 ) ;
/ / Test w i t h p r i n t 1
p r i n t 1 ( data1 ) ;
p r i n t 1 ( data2 ) ;
p r i n t 1 ( data3 ) ;
d e l e t e data1 ;
}

Identify visually any possible error. If does, gives an explanation. Next, implement the code in your IDE and
compare results (Note: This is important).

D. lvalue & rvalue


23. In page 75 you can find: "So an rvalue is - to a first approximation - a value that you cant assign to, such
as an integer returned by a function call, and an rvalue reference is a reference to something that nobody
else can assign to." In my IDE (Xcode 6.3 - libc++ Standard Library) I can identify rvalue easily due to the
following error message:
Expression is not assignable.
Non-const lvalue reference to type int......
Cant take the address of an rvalue of ...
How do you identify this in your IDE. ?
24. In page 191, you find the following declaration:
1
2
3
4
5
6
7
8

# include < iostream >


using namespace s t d ;
i n t main ( ) {
double& d r { 1 } ;
const double& c d r { 3 } ;
}

/ / E r r o r : l v a l u e needed .
/ / OK

Now close the book and explain the difference of these two lines.
25. Try this:
1
2
3
4
5
6
7
8
9
10
11

# include < iostream >


using namespace s t d ;
i n t& f u n c t i o n 3 ( ) { return 1 4 ; }
const i n t & f u n c t i o n 4 ( ) { r e t u r n 1 5 ; }
i n t main ( ) {
i n t var1 = f u n c t i o n 4 ( ) ;
c o u t << var1 << e n d l ;
}

Why do you get an error in function3() and not in function4() ?

26. (*) Check the following code (class object was introduced in chapter 3 - dont get panic). The purpose is to
show how move (rvalue) and copy (lvalue) differs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

# include < iostream >


# include < v e c t o r >
using namespace s t d ;
class Obj {
public :
Obj ( const i n t & data =0) : data_ ( data ) { c o u t << " C o n s t r u c t o r \ n " ; }
v i r t u a l ~Obj ( ) { }
Obj ( const Obj& T ) {
c o u t << " Copy C o n s t r u c t o r \ n " ;
copy ( T ) ;
}
Obj ( Obj&& T ) : data_ ( 0 ) {
c o u t << " Move C o n s t r u c t o r \ n " ;
copy ( T ) ;
release (T) ;
}
Obj& operator =( const Obj& T ) {
c o u t << " Copy Assignment \ n " ;
i f (&T ! = t h i s ) {
copy ( T ) ;
}
return this ;
}
Obj& operator =( Obj&& T ) {
c o u t << " Move Assignment \ n " ;
i f (&T ! = t h i s ) {
copy ( T ) ;
release (T) ;
}
return this ;
}
private :
void copy ( const Obj& T ) {
data_ = T . data_ ;
}
void r e l e a s e ( Obj& T ) {
T . data_ = 0 ;
}
i n t data_ ;
};

The following code correspond to the main section. Try to identify which member function: constructor, copy
constructor, move constructor, copy Assignment or move assignment is invoked.
(a)
1 i n t main ( ) {
2
Obj A1 ( 1 2 ) ;
3 }

(b)
1 i n t main ( ) {
2
Obj A1 ( 1 2 ) ;
3
Obj A2 ( A1 ) ;
4 }

(c)
1 i n t main ( ) {
2
Obj A1 ( Obj ( 1 3 ) ) ;
3 }

(d)
1 i n t main ( ) {
2
v e c t o r <Obj > V ;
3
V . push_back ( Obj ( 1 ) ) ;
4
V . push_back ( Obj ( 2 ) ) ;
5 }

(e)
1 i n t main ( ) {
2
v e c t o r <Obj > V ;
3
V. reserve ( 5 ) ;
4
V . push_back ( Obj ( 1 ) ) ;
5
V . push_back ( Obj ( 2 ) ) ;
6 }

Explain the different behaviour of point d and e.