latexc++ proposed exercises (chapter 2: the c++ programing language, fourth edition)
TRANSCRIPT
-
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 problemseasily.
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 #include 23 using namespace std ;45 i n t main ( ) {6 i n t data1 = 3.1415;7 i n t data2 {3 .1 4 15 } ;8 }
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 #include 2 using namespace std ;34 i n t suma1(unsigned in t& a , unsigned in t& b ) { return a+b ; }5 unsigned in t suma2( signed in t& a , signed in t& b ) { return a+b ; }6 i n t suma3( i n t&a , i n t&b ) { return a+b ; }78 i n t main ( ) {9 i n t a = 12;
10 i n t b = 9;11 cout
-
C. Scope
21. Before you compile the code, identify visually the error (if exist):
(a) Code 1
1 #include 2 using namespace std ;34 i n t var1 { 1 0 } ;56 void f (double data ) {7 cout
-
(e) Code 5
1 #include 2 using namespace std ;34 struct f {5 s ta t i c const in t var1 { 1 0 } ;6 void p r i n t (double data ) {7 cout