Anda di halaman 1dari 7

# Chapter 6 Solutions

1. Declarations
1
2
3
4
5

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

i n t data1 { 1 2 } ;
i n t data2 { 1 5 } ;
}

main.cpp
2. Size of variables
1

2
3
4
5
6

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

i n t data1 { 1 2 } ;
i n t data2 { 1 5 } ;
i n t data3 ;

## size_t s_data1 { s i z e o f ( data1 ) } ;

size_t s_data2 { s i z e o f ( data2 ) } ;
size_t s_data3 { s i z e o f ( data3 ) } ;

8
9
10
11

std : : cout << " I n i t i a l i z e i n t e g e r s i z e : " << s_data1 << std : : endl ;
std : : cout << " I n i t i a l i z e i n t e g e r s i z e : " << s_data2 << std : : endl ;
std : : cout << " U n i t i a l i z e i n t e g e r s i z e : " << s_data3 << std : : endl ;

12
13
14
15

main.cpp
3. Assignment
1

2
3
4
5
6

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

i n t data1 { 1 2 } ;
i n t data2 { 1 5 } ;
c o n s t i n t data3 { data1+data2 } ;

7
8

std : : cout << data1 << " + " << data2 << " = " << data3 << std : : endl ;

9
10

main.cpp

4. Bool function with integers. The output is 1 if the condition is true and 0 if the condition is false.
1

2
3
4
5
6
7
8
9

## b o o l compare ( c o n s t i n t & value1 , c o n s t i n t & value2 ) {

i f ( value1 <= value2 )
std : : cout << value1 << " i s l e s s than " << value2 << std : : endl ;
else
std : : cout << value2 << " s i l e s s than " << value1 << std : : endl ;
r e t u r n ( value1 <= value2 ) ;
}

10
11
12
13
14
15

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

i n t data1 { 1 9 } ;
i n t data2 { 1 5 } ;
compare ( data1 , data2 ) ;
}

main.cpp
5. Bool function with integers. The output is 1 is the condition is true and 0 if the condition is false.
1

2
3
4
5

## b o o l are_equal ( c o n s t i n t & value1 , c o n s t i n t & value2 ) {

r e t u r n ( value1 == value2 ) ;
}

6
7
8
9
10
11

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

i n t data1 { 1 9 } ;
i n t data2 { 1 5 } ;
are_equal ( data1 , data2 ) ;
}

main.cpp
6. Bool function with char. Templates are used to make a function independent of the type (Generic Programming).
1

2
3
4
5

## b o o l are_equal ( c o n s t char& value1 , c o n s t char& value2 ) {

r e t u r n ( value1 == value2 ) ;
}

6
7
8
9
10
11

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

char data1 { ' a ' } ;
char data2 { ' a ' } ;
are_equal ( data1 , data2 ) ;
}

main.cpp

7. My personal bool
1

2
3
4
5

## i n t are_equal ( c o n s t i n t & value1 , c o n s t i n t & value2 ) {

r e t u r n ( ( value1 == value2 ) ? 1 : 0 ) ;
}

6
7
8
9
10
11

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

i n t data1 { 1 2 } ;
i n t data2 { 1 3 } ;
are_equal ( data1 , data2 ) ;
}

main.cpp
8. Constant variables should always be initialized. If you dont initialize a constant expression you will get an
error message. No example provided.
9. The purpose of this exercise is to start differentiate between compile and run errors. Search in google for
more information. Identify that when you compile data1 value will not be 3.1415.
10. Lets see the value of data1.
1

2
3
4
5

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

i n t data1 = 3 . 1 4 1 5 ;
//
i n t data2 { 3 . 1 4 1 5 } ;

7
8

1

2
3
4
5
6
7
8

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

/ * * * Option 1 ( C a s t i n g ) * * * /
char data1 { ' z ' } ;
char data2 { 'w ' } ;
std : : cout << " L e t t e r z v a l u e i s : " << ( i n t ) ( data1 ) << std : : endl ;
std : : cout << " L e t t e r w v a l u e i s : " << ( i n t ) ( data2 ) << std : : endl ;

9
10
11
12
13
14
15

/ * * * Option 2 ( D e f i n i t i o n ) * * * /
i n t data3 { ' z ' } ;
i n t data4 { 'w ' } ;
std : : cout << " L e t t e r z v a l u e i s : " << data3 << std : : endl ;
std : : cout << " L e t t e r w v a l u e i s : " << data4 << std : : endl ;
}

main.cpp

1

2
3
4
5
6
7
8
9
10

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

/ * * * Option 1 ( C a s t i n g ) * * * /
i n t data1 { 0 } ;
i n t data2 { 1 0 } ;
i n t data3 { 5 5 } ;
std : : cout << " I n t 0 v a l u e i s : " << ( char ) ( data1 ) << std : : endl ;
std : : cout << " I n t 10 v a l u e i s : " << ( char ) ( data2 ) << std : : endl ;
std : : cout << " I n t 55 v a l u e i s : " << ( char ) ( data3 ) << std : : endl ;

11
12
13
14
15
16
17
18
19

/ * * * Option 2 ( D e f i n i t i o n ) * * * /
char data4 { 0 } ;
char data5 { 1 0 } ;
char data6 { 5 5 } ;
std : : cout << " I n t 0 v a l u e i s : " << data4 << std : : endl ;
std : : cout << " I n t 10 v a l u e i s : " << data5 << std : : endl ;
std : : cout << " I n t 55 v a l u e i s : " << data6 << std : : endl ;
}

main.cpp

## Arithmetic Type and Declarations

14 Declaration
pi and pi1 are both of type const double. Stop debugging in line 9 and check the previous assertion.
However, they arent equivalent when we consider time as a variable. This is a tricky question in an interview,
so differentiate between run time and compile time constant.
1

2
3

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

4
5
6

c o n s t double pi1 { 3 . 1 4 1 5 } ;
std : : cout << pi1 << std : : endl ;

/ / Run Time c o n s t a n t

constexpr double pi { 3 . 1 4 1 5 } ;
std : : cout << pi << std : : endl ;
}

/ / Compile Time c o n s t a n t

7
8
9
10

main.cpp

15 Declaration

2
3

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

c o n s t double pi1 { 3 . 1 4 1 5 } ;
std : : cout << pi1 << std : : endl ;
double data1 { ( pi1 * 4 ) } ;

5
6
7

/ / Run Time c o n s t a n t

constexpr double pi { 3 . 1 4 1 5 } ;
/ / Compile t i m e c o n s t a n t
std : : cout << pi << std : : endl ;
constexpr double data2 { ( pi * 4 ) } ;
}

9
10
11
12

main.cpp
16 Be careful with types input and output when declaring functions.
1

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

2
3
4
5
6

/ * * * Global f u n c t i o n * * * /
i n t Add ( c o n s t f l o a t & data1 , c o n s t f l o a t & data2 ) {
r e t u r n ( data1 + data2 ) ;
}

7
8
9
10
11

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

f l o a t data1 { 5 } ;
f l o a t data2 { 1 9 } ;
i n t addition1 { Add ( data1 , data2 ) } ;
/ / ( 2 4 ) Ok make sense

12

f l o a t data3 { 3 . 1 4 5 } ;
f l o a t data4 { 9 . 1 8 9 } ;
i n t addition2 { Add ( data3 , data4 ) } ;

13
14
15

/ / ( 1 2 ) No sense a t a l l

16

17

main.cpp
17 Pointer input and output in functions
1

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

2
3
4
5
6

/ * * * Global f u n c t i o n * * * /
double * by2 ( double * data ) {
r e t u r n ( new double ( * data * 2 ) ) ;
}

7
8
9
10
11
12

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

double * data { new double { 1 2 . 5 } } ;
double * result { by2 ( data ) } ;
std : : cout << result << std : : endl ;
std : : cout << * result << std : : endl ; / / Memory v a l u e

13

d e l e t e data ;
d e l e t e result ;

14
15
16

main.cpp
18 Solution not provided. Reader can do it easily.
19 Function f(x)
1
2

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

# i n c l u d e <math . h>

3
4
5
6
7
8

/ * * * Global f u n c t i o n * * * /
double f ( c o n s t double& data ) {
c o n s t double e { 2 . 7 1 8 2 8 } ;
r e t u r n ( pow ( e , data ) ) ;
/ / pow f u n c t i o n i s i n math . h
}

9
10
11
12
13
14
15

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

double data1 { 2 . 5 } ;
double data2 { 5 } ;
std : : cout << " e ^ ( 2 . 5 ) : " << f ( data1 ) << std : : endl ;
std : : cout << " e ^ ( 5 ) : " << f ( data2 ) << std : : endl ;
}

main.cpp
20 Numeric Limits
1

2
3

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

std : : cout << " F l o a t max number : " << ( std : : numeric_limits< f l o a t > : : max ( ) ) << std : : endl ;
std : : cout << " Double max number : " << ( std : : numeric_limits<double > : : max ( ) ) << std : : endl ;
std : : cout << " I n t max number : " << ( std : : numeric_limits< i n t > : : max ( ) ) << std : : endl ;
}

5
6
7
8

main.cpp

C.
a. No error, var1 is global and is defined before function f.
b. Error, var1 is global but is defined after function f.
c. Error. f doesnt return anything is void.
d. Erro. f is a function.
e No error.

D. Initialization
a. int.
b. char.

c. float or double.
d. int.
e. int.
f. string.
g. Undefined type.
h. int.
i. char.
Follow items h and i as example of proper initialization.