You are on page 1of 4

Proposed Exercises Chapter 6

In this document you can find exercises related to the content of C++ Programming Language (Fourth Edition).
Solution is provided. 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
Material (Part I) and chapter six, 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. Prove if proposed code behaves correctly in main().

A. Integral Types and Declarations


1. Declare two integers with value 12 and 15.
2. What is the size of the previous two integers. Compare the size with an uninitialized int.
3. Declare a constant integer equal to the sum of the integers defined in (1).
4. Declares a bool function that compares two integers values and print a message: "X is less than Y".
5. Declares a bool function that test if two integers are equal.
6. Repeat exercise 5 with char type.
7. Declares an integer function that behaves similar to exercise 5 (Hint: return zero if true and one if not).
8. Declare (but dont initialize) a constant int. Any error message ?
9. Try this code:
1
2
3
4
5
6
7
8

# include < iostream >


using namespace s t d ;
i n t main ( ) {
i n t data1 = 3 . 1 4 1 5 ;
i n t data2 { 3 . 1 4 1 5 } ;
}

Why do you get an error in line 7. Which approach is safer?


10. Comment line 7 in the previous exercise and print the value of data1 (Hint: Use cout).
11. What is the integer value of char: z and w.
12. What is the char associated to integers value: 0, 10 and 55.
13. Without using the IDE, check (visually) if the following code compile correctly:

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

# include < iostream >


using namespace s t d ;
i n t suma1 ( unsigned i n t & a , unsigned i n t & b ) { r e t u r n a+b ; }
unsigned i n t suma2 ( signed i n t & a , signed i n t & b ) { r e t u r n a+b ; }
i n t suma3 ( i n t &a , i n t &b ) { r e t u r n a+b ; }
i n t main ( ) {
int a =
int b =
c o u t <<
c o u t <<
c o u t <<
}

12;
9;
suma1 ( a , b ) << e n d l ;
suma2 ( a , b ) << e n d l ;
suma3 ( a , b ) << e n d l ;

Now use an IDE and compile. Compare you observations with the IDE output.

B. Arithmetic Type and Declarations


14. What type would you assign to pi (3.141592).
15. Declare a double equal to pi * 4.
16. Declare a function Add with two float input members and integer output. Does it has any sense?
17. Declares a function By2 with the following characteristics:
(a) Input type: double pointer.
(b) Output type: double pointer.
18. Repeat exercise 17 but with double (not pointer) output type.
19. Declare the function:
f (x) = ex

(1)

Where e is the Euler constant 2.71828(use this as an approximation of e). Now find the value of: f(2.5), f(5).
20. What is the maximum number that a float, double and int type can store (Hint:Check page 150).

C. Scope
21. Before you compile the code, identify visually the error (if exist):
(a) Code 1
1
2
3
4
5
6
7
8
9
10
11
12

# include < iostream >


using namespace s t d ;
i n t var1 { 1 0 } ;
void f ( double data ) {
c o u t << data var1 ;
}
i n t main ( ) {
f (12) ;
}

(b) Code 2
1
2
3
4
5
6
7
8
9
10
11
12

# include < iostream >


using namespace s t d ;
void f ( double data ) {
c o u t << data var1 ;
}
i n t var1 { 1 0 } ;
i n t main ( ) {
f (12) ;
}

(c) Code 3
1
2
3
4
5
6
7
8
9
10
11
12

# include < iostream >


using namespace s t d ;
i n t var1 { 1 0 } ;
void f ( double data ) {
c o u t << data var1 ;
}
i n t main ( ) {
i n t data1 { f ( 1 2 ) } ;
}

(d) Code 4
1
2
3
4
5
6
7
8
9
10
11

# include < iostream >


using namespace s t d ;
void f ( double data ) {
i n t var1 { 1 0 } ;
c o u t << data var1 ;
}
i n t main ( ) {
i n t data1 { f : : var1 } ;
}

(e) Code 5
1
2
3
4
5
6
7
8
9
10
11
12
13

# include < iostream >


using namespace s t d ;
struct f {
s t a t i c const i n t var1 { 1 0 } ;
void p r i n t ( double data ) {
c o u t << data var1 ; }
};
i n t main ( ) {
i n t data { f : : var1 } ;
c o u t << data ;
}

D. Initialization
22. Set the most appropriate type for the following:
(a) auto data = 123;
(b) auto data1 = a;
(c) auto data2 = 3.1415;
(d) auto data3 = numericl imits < int >:: max();
(e) auto data4 = 2147483648;
(f) auto data5 = "Mauricio";
(g) auto data6 = Mauricio;
(h) auto data7{12};
(i) auto data8{a};
How can these initialization be improved ?

You might also like