Professional Documents
Culture Documents
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
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
13
14
15
main.cpp
3. data [5,10]
1
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
13
14
15
16
17
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
7
8
9
10
11
12
13
14
15
16
17
main.cpp
5. data (5,10)
1
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
13
14
15
16
17
18
19
Observation
inRante_5_10 function is not equivalent in exercise 3, 4, 5.
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
13
14
15
16
17
18
19
main.cpp
7. data (2,10) [20,28]
1
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
13
14
15
16
17
18
19
2
3
4
u s i n g std : : cout ;
u s i n g std : : endl ;
5
6
7
9
10
11
12
13
14
15
16
17
18
19
Listing:
9. Maximum number in a container (more than two solutions)
1
2
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
16
17
18
19
20
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
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
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
C. For Statement
1. Accumulated Sum.
1
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
main.cpp
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
main.cpp
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
26
27
28
29
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 ) -
main.cpp
4. Palindrome
The first approach is to reverse the whole string and compare both result
1
2
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
21
22
23
24
25
26
27
std : : string
std : : string
std : : string
std : : string
std : : string
28
29
30
31
32
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
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 ) {
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
20
21
22
23
24
25
26
27
28
29
30
31
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
45
46
47
std : : string
std : : string
std : : string
std : : string
std : : string
48
49
50
51
52
53
print ( S ) ;
print ( S1 ) ;
print ( S2 ) ;
print ( S3 ) ;
print ( S4 ) ;
54
55
56
57
58
59
main.cpp