You are on page 1of 10

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

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8

b o o l less5 ( c o n s t i n t & data ) {


r e t u r n ( data < 5 ) ;
}

9
10
11
12
13
14
15

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t data { 1 2 } ;
i n t data1 { 3 } ;
cout << data << " i s l e s s than 5 ? : " << less5 ( data ) << endl ;
cout << data1 << " i s l e s s than 5 ? : " << less5 ( data1 ) << endl ;
}

main.cpp
3. data [5,10]
1

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8

b o o l inRange_5_10 ( c o n s t i n t & data ) {


r e t u r n ( data >= 5 && data <=10 ) ;
}

9
10
11
12
13
14
15
16
17

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t data { 1 2 } ;
i n t data1 { 3 } ;
i n t data2 { 7 } ;
cout << data << " i n [ 5 , 1 0 ] ? : " << inRange_5_10 ( data ) << endl ;
cout << data1 << " i n [ 5 , 1 0 ] ? : " << inRange_5_10 ( data1 ) << endl ;
cout << data2 << " i n [ 5 , 1 0 ] ? : " << inRange_5_10 ( data2 ) << endl ;
}

main.cpp

4. data (5,10]
1
2

u s i n g std : : cout ;
u s i n g std : : endl ;

3
4
5
6

b o o l inRange_5_10 ( c o n s t i n t & data ) {


r e t u r n ( data > 5 && data <=10 ) ;
}

7
8
9
10
11
12
13
14
15
16
17

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t data { 1 2 } ;
i n t data1 { 3 } ;
i n t data2 { 7 } ;
i n t data3 { 5 } ;
cout << data << " i n ( 5 , 1 0 ] ? : " << inRange_5_10 ( data ) << endl ;
cout << data1 << " i n ( 5 , 1 0 ] ? : " << inRange_5_10 ( data1 ) << endl ;
cout << data2 << " i n ( 5 , 1 0 ] ? : " << inRange_5_10 ( data2 ) << endl ;
cout << data3 << " i n ( 5 , 1 0 ] ? : " << inRange_5_10 ( data3 ) << endl ;
}

main.cpp
5. data (5,10)
1

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8

b o o l inRange_5_10 ( c o n s t i n t & data ) {


r e t u r n ( data > 5 && data < 10 ) ;
}

9
10
11
12
13
14
15
16
17
18
19

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t data { 1 2 } ;
i n t data1 { 3 } ;
i n t data2 { 7 } ;
i n t data3 { 5 } ;
cout << data << " i n ( 5 , 1 0 ) ? : " << inRange_5_10 ( data ) << endl ;
cout << data1 << " i n ( 5 , 1 0 ) ? : " << inRange_5_10 ( data1 ) << endl ;
cout << data2 << " i n ( 5 , 1 0 ) ? : " << inRange_5_10 ( data2 ) << endl ;
cout << data3 << " i n ( 5 , 1 0 ) ? : " << inRange_5_10 ( data3 ) << endl ;
}

Observation
inRante_5_10 function is not equivalent in exercise 3, 4, 5.

6. data greater than 5.


1

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8

b o o l greater_5 ( c o n s t i n t & data ) {


r e t u r n ( data > 5 ) ;
}

9
10
11
12
13
14
15
16
17
18
19

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t data { 1 2 } ;
i n t data1 { 3 } ;
i n t data2 { 7 } ;
i n t data3 { 5 } ;
cout << data << " i s > 5 ? : " << greater_5 ( data ) << endl ;
cout << data1 << " i s > 5 ? : " << greater_5 ( data1 ) << endl ;
cout << data2 << " i s > 5 ? : " << greater_5 ( data2 ) << endl ;
cout << data3 << " i s > 5 ? : " << greater_5 ( data3 ) << endl ;
}

main.cpp
7. data (2,10) [20,28]
1

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8

b o o l inRange ( c o n s t i n t & data ) {


r e t u r n ( ( data > 2 && data < 10) | | ( data >= 20 && data <= 28) ) ;
}

9
10
11
12
13
14
15
16
17
18
19

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t data { 2 2 } ;
i n t data1 { 1 } ;
i n t data2 { 7 } ;
i n t data3 { 3 5 } ;
cout << data << " i n ( 2 , 1 0 ) U [ 2 0 , 2 8 ] ? " << inRange ( data ) << endl ;
cout << data1 << " i n ( 2 , 1 0 ) U [ 2 0 , 2 8 ] ? " << inRange ( data1 ) << endl ;
cout << data2 << " i n ( 2 , 1 0 ) U [ 2 0 , 2 8 ] ? " << inRange ( data2 ) << endl ;
cout << data3 << " i n ( 2 , 1 0 ) U [ 2 0 , 2 8 ] ? " << inRange ( data3 ) << endl ;
}

8. Function with if else statement.


Return start to increase in size. Offer an alternative solution.
1

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7

b o o l inRange ( c o n s t i n t & data ) {


r e t u r n ( ( data > 2 && data < 10 && data%2 == 0 ) | | ( data >= 20 && data <= 28 && data%2 ! = 0) ) ;
}

9
10
11
12
13
14
15
16
17
18
19

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t data { 8 } ;
i n t data1 { 1 } ;
i n t data2 { 2 7 } ;
i n t data3 { 3 5 } ;
cout << data << " s a t i s f y c o n d i t i o n ? " << inRange ( data ) << endl ;
cout << data1 << " s a t i s f y c o n d i t i o n ? " << inRange ( data1 ) << endl ;
cout << data2 << " s a t i s f y c o n d i t i o n ? " << inRange ( data2 ) << endl ;
cout << data3 << " s a t i s f y c o n d i t i o n ? " << inRange ( data3 ) << endl ;
}

Listing:
9. Maximum number in a container (more than two solutions)
1
2

# i n c l u d e < iostream >


# include <vector >

3
4
5

u s i n g std : : cout ;
u s i n g std : : endl ;

6
7
8
9
10
11
12
13
14

i n t Max ( c o n s t i n t & data ) {


s t a t i c i n t data1 { 0 } ;
i f ( data > data1 ) {
data1 = data ;
}
r e t u r n data1 ;
}

15
16
17
18
19

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


std : : vector< i n t > Container { 1 , 3 , 2 , 5 , 6 , 3 , 8 , 9 , 1 0 , 2 2 , 1 1 , 7 , 4 } ;

20

/ * * * Option1 ( Use s t d : : max_element ) * * * /


i n t max { * std : : max_element ( begin ( Container ) , end ( Container ) ) } ;
cout << "Max number i n c o n t a i n e r i s : " << max << endl ;

21
22
23
24

/ * * * Option2 ( Lambda ) * * * /
i n t max1 { 0 } ;
std : : for_each ( Container . begin ( ) , Container . end ( ) , ( [ & max1 ] ( i n t x ) {
max1 = Max ( x ) ;
}) ) ;
cout << "Max number i n c o n t a i n e r i s : " << max1 << endl ;

25
26
27
28
29
30
31

B. Switch Statement
5 Cars and Colours
This can be implemented also with if else statement. Exercise 1, 2, 3, 4 arent provided. If you provide a
function with an int argument to make the selection, as new colours and cars are available, you must assign
values manually. For maintenance this must be avoided. Use the scope operator :: and forget about magic
numbers.
1
2

//
# i n c l u d e < iostream >

3
4
5

u s i n g std : : cout ;
u s i n g std : : endl ;

6
7
8

enum c l a s s car { Ferrari , Corvette , Maserati } ;


enum c l a s s color { black , red , white } ;

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
47
48
49
50
51
52

v o i d seletion ( c o n s t car& car , c o n s t color& color ) {


s w i t c h ( car ) {
case car : : Ferrari :
s w i t c h ( color ) {
case color : : black :
cout << " F e r r a r i i s Black \ n " ;
break ;
case color : : red :
cout << " F e r r a r i i s Red \ n " ;
break ;
case color : : white :
cout << " F e r r a r i i s White \ n " ;
break ;
default :
cout << " c o l o r f o r F e r r a r i n o t d e f i n e d \ n " ;
break ;
}
break ;
case car : : Corvette :
s w i t c h ( color ) {
case color : : black :
cout << " C o r v e t t e i s Black \ n " ;
break ;
case color : : red :
cout << " C o r v e t t e i s Red \ n " ;
break ;
case color : : white :
cout << " C o r v e t t e i s White \ n " ;
break ;
default :
cout << " c o l o r f o r C o r v e t t e n o t d e f i n e d \ n " ;
break ;
}
break ;
case car : : Maserati :
s w i t c h ( color ) {
case color : : black :
cout << " M a s e r a t i i s Black \ n " ;
break ;
case color : : red :
cout << " M a s e r a t i i s Red \ n " ;
break ;
case color : : white :

cout << " M a s e r a t i i s White \ n " ;


break ;
default :
cout << " c o l o r f o r M a s e r a t i n o t d e f i n e d \ n " ;
break ;

53
54
55
56
57

}
break ;

58
59
60

default :
cout << " Car and c o l o r n o t d e f i n e d \ n " ;
break ;

61
62
63

64
65

66
67
68
69
70

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


seletion ( car : : Ferrari , color : : white ) ;
seletion ( car : : Maserati , color : : black ) ;
}

How can you call the default value ?

C. For Statement
1. Accumulated Sum.
1

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8
9
10
11
12

i n t Accum_Sum ( c o n s t i n t & a ) {
i n t result { 0 } ;
f o r ( i n t i = 0 ; i <= a ; i++) {
result += i ;
}
r e t u r n result ;
}

13
14
15
16

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


cout << Accum_Sum ( 5 ) ;
}

main.cpp

2. Fibonacci accumulated sum


1
2

# i n c l u d e < iostream >


# include <vector >

3
4
5

u s i n g std : : cout ;
u s i n g std : : endl ;

6
7

/ * * * Recursive C a l l * * * /

8
9
10
11
12
13
14
15
16
17
18
19

i n t fib ( c o n s t i n t & n ) {
i f ( n==1) {
return 1;
}
e l s e i f ( n==2) {
return 2;
}
else {
r e t u r n fib ( n1) + fib ( n2) ;
}
}

20
21
22
23
24
25
26
27
28
29

i n t Fibonacci_Sum1 ( c o n s t i n t & n ) {
std : : vector< i n t > result ;
f o r ( i n t i = 1 ; i ! = n+1; i++) {
result . push_back ( fib ( i ) ) ;
}
i n t acumm_sum { 0 } ;
std : : for_each ( begin ( result ) , end ( result ) , ( [ & acumm_sum ] ( i n t x ) { acumm_sum += x ; } ) ) ;
r e t u r n acumm_sum ;
}

30
31
32
33
34

35

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


int n{4};
cout << " Accumulated sum o f F i b o n a c c i s e r i e u n t i l " << n << " i s : " << Fibonacci_Sum1 ( n ) << endl ;
}

main.cpp

3. Fibonacci accumulated sum new version


Fib(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
2

# i n c l u d e < iostream >


# include <vector >

3
4
5

u s i n g std : : cout ;
u s i n g std : : endl ;

6
7
8
9
10
11
12
13
14
15

/ * * * Recursive C a l l * * * /
i n t fib ( c o n s t i n t & n ) {
i f ( n==1)
return 1;
e l s e i f ( n==2)
return 2;
else
r e t u r n fib ( n1) + fib ( n2) ;
}

16
17
18
19
20
21

i n t Fibonacci_Sum1 ( c o n s t i n t & n ) {
s t a t i c i n t last { 0 } ;
s t a t i c std : : vector< i n t > result ;
s t a t i c i n t start { 1 } ;
i n t acumm_sum { 0 } ;

22

i f ( n > last ) {
last = n ;

23
24
25

f o r ( i n t i = start ; i ! = last+1; i++) {


result . push_back ( fib ( i ) ) ;
}

26
27
28
29

std : : for_each ( begin ( result ) , end ( result ) , ( [ & acumm_sum ] ( i n t x ) { acumm_sum += x ; } ) ) ;


start = n+1;

30
31

}
else {
std : : vector< i n t > : : iterator limit = begin ( result ) + n ;
std : : for_each ( begin ( result ) , limit , ( [ & acumm_sum ] ( i n t x ) { acumm_sum += x ; } ) ) ;
}
r e t u r n acumm_sum ;

32
33
34
35
36
37
38

39
40
41
42

43
44

45
46

47
48

49

i n t main ( i n t argc , c o n s t
int n{4};
cout << " Accumulated
<< endl ;
n = 3;
cout << " Accumulated
<< endl ;
n = 6;
cout << " Accumulated
<< endl ;
n = 5;
cout << " Accumulated
<< endl ;
}

char * argv [ ] ) {
sum o f F i b o n a c c i s e r i e u n t i l " << n << " i s : " << Fibonacci_Sum1 ( n ) -

sum o f F i b o n a c c i s e r i e u n t i l " << n << " i s : " << Fibonacci_Sum1 ( n ) -

sum o f F i b o n a c c i s e r i e u n t i l " << n << " i s : " << Fibonacci_Sum1 ( n ) -

sum o f F i b o n a c c i s e r i e u n t i l " << n << " i s : " << Fibonacci_Sum1 ( n ) -

main.cpp

4. Palindrome
The first approach is to reverse the whole string and compare both result
1
2

# i n c l u d e < iostream >


#include <string >

3
4
5

u s i n g std : : cout ;
u s i n g std : : endl ;

6
7
8
9
10
11
12
13
14
15

/ * * * * Palindrome * * * /
std : : string reverse ( c o n s t std : : string& myString ) {
std : : string S ;
f o r ( auto it = myString . crbegin ( ) ; it ! = myString . crend ( ) ; ++it ) {
S += * it ;
}
return S;
}

16
17
18
19
20

b o o l palindrome ( c o n s t std : : string& myString ) {


i n t comparison { myString . compare ( reverse ( myString ) ) } ;
r e t u r n ( comparison == 0 ) ? 1 : 0 ;
}

21
22
23

24

v o i d print ( c o n s t std : : string& myString ) {


cout << myString << " vs " << reverse ( myString ) << " .
myString ) << endl ;
}

I s palindrome ? : " << palindrome ( -

25
26

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

27

std : : string
std : : string
std : : string
std : : string
std : : string

28
29
30
31
32

S { " 14341 " } ;


S1 { " 12333321 " } ;
S2 { " 134 " } ;
S3 { " a b c d e r t " } ;
S4 { " abcdcba " } ;

33

print ( S ) ;
print ( S1 ) ;
print ( S2 ) ;
print ( S3 ) ;
print ( S4 ) ;

34
35
36
37
38
39

main.cpp
5. Palindrome (more efficient)
As the string size increase, consider only reversing the half of the string (if string size is even); and reversing
half minus one of the string (if string size is odd).
1
2

# i n c l u d e < iostream >


#include <string >

3
4
5

u s i n g std : : cout ;
u s i n g std : : endl ;

6
7
8
9

/ * * * * Palindrome * * * /
std : : string reverse ( c o n s t std : : string& myString ) {

std : : string sub_string , rev_sub_string ;


i n t size { ( i n t ) ( myString . size ( ) ) } ;
i n t n { size % 2 } ;

10
11
12
13

i f ( n == 0 )
sub_string
else {
size_t pos
size_t len
sub_string

14
15
16
17
18
19

= myString . substr ( n , * myString . end ( ) ) ;


= ( size+1) / 2 ;
= ( size1) / 2 ;
= myString . substr ( pos , len ) ; }

20

f o r ( auto it = sub_string . crbegin ( ) ; it ! = sub_string . crend ( ) ; ++it ) {


rev_sub_string += * it ;
}
r e t u r n rev_sub_string ;

21
22
23
24
25

26
27
28
29
30
31

b o o l palindrome ( c o n s t std : : string& myString ) {


std : : string S1 { reverse ( myString ) } ;
std : : string S2 ;
i n t size { ( i n t ) ( myString . size ( ) ) } ;
i n t n { size % 2 } ;

32

i f ( n == 0 )
S2 = myString . substr ( 0 , n ) ;
else
S2 = myString . substr ( 0 , ( size 1 ) / 2 ) ;

33
34
35
36
37

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

38
39
40

41
42
43

44

v o i d print ( c o n s t std : : string& myString ) {


cout << myString << " vs " << reverse ( myString ) << " . I s palindrome ? : " << palindrome ( myString ) << endl ;
}

45
46

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

47

std : : string
std : : string
std : : string
std : : string
std : : string

48
49
50
51
52

S { " 14341 " } ;


S1 { " 12333321 " } ;
S2 { " 134 " } ;
S3 { " a b c d e r t " } ;
S4 { " abcdcba " } ;

53

print ( S ) ;
print ( S1 ) ;
print ( S2 ) ;
print ( S3 ) ;
print ( S4 ) ;

54
55
56
57
58
59

main.cpp

You might also like