latexc++ proposed exercises (chapter 2: the c++ programing language, fourth edition)

4
 Proposed Exercises Chapter 6 In this document you can nd exercises related to the content of C++ Programming Language (Fourth Edition). Solution is provided. Howe ver , if you follow the book carefully , you will be able to solve the proposed problems easily. Introduction material is very dense and that’s why no exercise material is elaborated. Don’t get panic If you have difculti es trying to solve these exercis es. The material provided in the Introduction Material (Part I) and chapter six, allow you to PROPOSE some solution. Some recommendations are: 1. T ry to mimic the Programming Technique  found in the chapter. 2. For now , don’t worry for efciency , make the program work. 3. Declaration implies denition (init ialization). 4. Prove if propos ed code behaves c orrectly in main(). A. Integral Types and Declarations 1. Declare two in tegers with v alue 12 and 15. 2. What is the size of the prev ious two integers . Compare the size wit h an uninitialized int. 3. Decl are a constant int eger equal to the sum of the integ ers dene d in (1). 4. Declares a bool function that compares two integers v alues and print a message: "X is less than Y". 5. Declares a bool function that tes t if two integers are equal. 6. Repeat ex ercise 5 with char ty pe. 7. Declares an integer funct ion that behaves similar to ex ercise 5 (Hint: return zero if true and one if not). 8. Declare (but don’t initia lize) a constant int. Any error message ? 9. T ry this code: 1  #include  <iostream> 2 3  usin g namespace  std ; 4 5  i nt  main () { 6  i nt  data1 = 3.1415; 7  i nt  data2 {3. 141 5}; 8 } Why do you get an error in line 7. Which approach is safer? 10. Comment line 7 in the previou s exer cise and print the value of data1 (Hint: Use cout). 11. What is the inte ger val ue of char:  z and  w. 12. What is the char associated to integ er’s value:  0 ,  10  and  55. 13. Without using the IDE, check (visually ) if the following code compile correctly:

Upload: mauricio-bedoya

Post on 05-Nov-2015

217 views

Category:

Documents


0 download

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