c++ proposed exercises (chapter 9: the c++ programing language, fourth edition) - solution -

Upload: mauricio-bedoya

Post on 04-Nov-2015

13 views

Category:

Documents


0 download

DESCRIPTION

.

TRANSCRIPT

  • Chapter 9 Solutions

    A. If Statement

    1. There are two forms to implement if else statements in C++:

    (condition)?(if true):(else)

    if (condition){.......}else{.......}

    2. (data < 5)

    1 # inc lude 2

    3 using std : : cout ;4 using std : : endl ;5

    6 bool less5 ( const i n t & data ) {7 r e t u r n (data < 5) ;8 }9

    10 i n t main ( i n t argc , const char * argv [ ] ) {11 i n t data { 1 2 } ;12 i n t data1 { 3 } ;13 cout

  • 4. data (5,10]

    1 using std : : cout ;2 using std : : endl ;3

    4 bool inRange_5_10 ( const i n t & data ) {5 r e t u r n (data > 5 && data

  • 6. data greater than 5.

    1 # inc lude 2

    3 using std : : cout ;4 using std : : endl ;5

    6 bool greater_5 ( const i n t & data ) {7 r e t u r n (data > 5) ;8 }9

    10 i n t main ( i n t argc , const char * argv [ ] ) {11 i n t data { 1 2 } ;12 i n t data1 { 3 } ;13 i n t data2 { 7 } ;14 i n t data3 { 5 } ;15 cout

  • 8. Function with if else statement.Return start to increase in size. Offer an alternative solution.

    1 # inc lude 2

    3 using std : : cout ;4 using std : : endl ;5

    6 bool inRange ( const i n t & data ) {7 r e t u r n ( (data > 2 && data < 10 && data%2 == 0) | | (data >= 20 && data

  • B. Switch Statement

    5 Cars and ColoursThis can be implemented also with if else statement. Exercise 1, 2, 3, 4 arent provided. If you provide afunction with an int argument to make the selection, as new colours and cars are available, you must assignvalues manually. For maintenance this must be avoided. Use the scope operator :: and forget about magicnumbers.

    1 / /2 # inc lude 3

    4 using std : : cout ;5 using std : : endl ;6

    7 enum class car {Ferrari , Corvette , Maserati } ;8 enum class color {black , red , white } ;9

    10 vo id seletion ( const car& car , const color& color ) {11 swi tch (car ) {12 case car : : Ferrari :13 swi tch (color ) {14 case color : : black :15 cout

  • 53 cout
  • 2. Fibonacci accumulated sum

    1 # inc lude 2 # inc lude 3

    4 using std : : cout ;5 using std : : endl ;6

    7 / * * * Recursive Ca l l * * * /8

    9 i n t fib ( const i n t & n ) {10 i f (n==1) {11 r e t u r n 1 ;12 }13 else i f (n==2) {14 r e t u r n 2 ;15 }16 else {17 r e t u r n fib (n1) + fib (n2) ;18 }19 }20

    21 i n t Fibonacci_Sum1 ( const i n t & n ) {22 std : : vector< i n t > result ;23 f o r ( i n t i = 1; i != n+1; i++) {24 result .push_back (fib (i ) ) ;25 }26 i n t acumm_sum { 0 } ;27 std : : for_each (begin (result ) , end (result ) , ( [&acumm_sum ] ( i n t x ) {acumm_sum += x ; } ) ) ;28 r e t u r n acumm_sum ;29 }30

    31

    32 i n t main ( i n t argc , const char * argv [ ] ) {33 i n t n { 4 } ;34 cout

  • 3. Fibonacci accumulated sum new versionFib(10) = {1,2,3,5,8,13,21, 34, 55, 89}.Fib(5) = {1,2,3,5,8}. Then Fib(5) Fib(10) - no need to generate data, only addition -.

    1 # inc lude 2 # inc lude 3

    4 using std : : cout ;5 using std : : endl ;6

    7 / * * * Recursive Ca l l * * * /8 i n t fib ( const i n t & n ) {9 i f (n==1)

    10 r e t u r n 1 ;11 else i f (n==2)12 r e t u r n 2 ;13 else14 r e t u r n fib (n1) + fib (n2) ;15 }16

    17 i n t Fibonacci_Sum1 ( const i n t & n ) {18 s t a t i c i n t last { 0 } ;19 s t a t i c std : : vector< i n t > result ;20 s t a t i c i n t start { 1 } ;21 i n t acumm_sum { 0 } ;22

    23 i f (n > last ) {24 last = n ;25

    26 f o r ( i n t i = start ; i != last+1; i++) {27 result .push_back (fib (i ) ) ;28 }29

    30 std : : for_each (begin (result ) , end (result ) , ( [&acumm_sum ] ( i n t x ) {acumm_sum += x ; } ) ) ;31 start = n+1;32 }33 else {34 std : : vector< i n t > : :iterator limit = begin (result ) + n ;35 std : : for_each (begin (result ) , limit , ( [&acumm_sum ] ( i n t x ) {acumm_sum += x ; } ) ) ;36 }37 r e t u r n acumm_sum ;38 }39

    40 i n t main ( i n t argc , const char * argv [ ] ) {41 i n t n { 4 } ;42 cout

  • 4. PalindromeThe first approach is to reverse the whole string and compare both result

    1 # inc lude 2 # inc lude < s t r i n g >3

    4 using std : : cout ;5 using std : : endl ;6

    7

    8 / * * * * Palindrome * * * /9 std : : string reverse ( const std : : string& myString ) {

    10 std : : string S ;11 f o r ( auto it = myString .crbegin ( ) ; it != myString .crend ( ) ; ++it ) {12 S += *it ;13 }14 r e t u r n S ;15 }16

    17 bool palindrome ( const std : : string& myString ) {18 i n t comparison {myString .compare (reverse (myString ) ) } ;19 r e t u r n (comparison == 0) ?1 :0 ;20 }21

    22 vo id print ( const std : : string& myString ) {23 cout

  • 10 std : : string sub_string , rev_sub_string ;11 i n t size { ( i n t ) (myString .size ( ) ) } ;12 i n t n {size % 2 } ;13

    14 i f (n == 0)15 sub_string = myString .substr (n , *myString .end ( ) ) ;16 else {17 size_t pos = (size+1) / 2 ;18 size_t len = (size1) / 2 ;19 sub_string = myString .substr (pos ,len ) ; }20

    21 f o r ( auto it = sub_string .crbegin ( ) ; it != sub_string .crend ( ) ; ++it ) {22 rev_sub_string += *it ;23 }24 r e t u r n rev_sub_string ;25 }26

    27 bool palindrome ( const std : : string& myString ) {28 std : : string S1 {reverse (myString ) } ;29 std : : string S2 ;30 i n t size { ( i n t ) (myString .size ( ) ) } ;31 i n t n {size % 2 } ;32

    33 i f (n == 0)34 S2 = myString .substr (0 ,n ) ;35 else36 S2 = myString .substr ( 0 , (size 1) / 2 ) ;37

    38 i n t comparison {S1 .compare (S2 ) } ;39 r e t u r n (comparison == 0) ?1 :0 ;40 }41

    42 vo id print ( const std : : string& myString ) {43 cout