Skip to content

Commit

Permalink
added ex2.1~ex2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Aug 12, 2014
1 parent 0023697 commit ff7b315
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ch02/ex2_4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>

int main()
{
unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl; // 32
std::cout << u - u2 << std::endl; // 4294967264
int i = 10, i2 = 42;
std::cout << i2 - i << std::endl; // 32
std::cout << i - i2 << std::endl; // -32
std::cout << i - u << std::endl; // 0
std::cout << u - i << std::endl; // 0

return 0;
}
51 changes: 51 additions & 0 deletions docs/ch02/ex2.1_2.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
##Exercise 2.1
> What are the differences between int, long, long long,
and short? Between an unsigned and a signed type? Between a float and
a double?

C++ guarantees `short` and `int` is **at least** 16 bits, `long` **at least** 32 bits, `long long` **at least** 64 bits.

The `unsigned` and `signed` type have the **same size**. for example, sizeof(unsigned short) == sizeof(short).

The C and C++ standards do not specify the representation of float, double and long double.
It is possible that all three implemented as IEEE double-precision.
Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) float is indeed a IEEE **single-precision** floating point number (binary32),
and double is a IEEE **double-precision** floating point number (binary64).

Usage:

- Use `int` for integer arithmetic. `short` is usually too small and, in practice,
`long` often has the same size as `int`. If your data values are larger than
the minimum guaranteed size of an `int`, then use `long long`.
(In a word: short < **int** < long < long long)

- Use an unsigned type when you know that the values cannot be negative.
(In a word: no negative, unsigned.)

- Use double for floating-point computations; float usually does not have
enough precision, and the cost of double-precision calculations versus
single-precision is negligible. In fact, on some machines, double-precision
operations are faster than single. The precision offered by long double
usually is unnecessary and often entails considerable run-time cost.
(In a word: float < **double** < long double)

Reference:

- [What are the criteria for choosing between short / int / long data types?](http://www.parashift.com/c++-faq/choosing-int-size.html)
- [Difference between float and double](http://stackoverflow.com/questions/2386772/difference-between-float-and-double)
- Advice: Deciding which Type to Use(This book.)

##Exercise 2.2
>To calculate a mortgage payment, what types would you use
for the rate, principal, and payment? Explain why you selected each type.

use `double`, or also `float`.

The rate most like that: 4.50 % per year.
The principal most like that: $854.36
The payment most like that: $1,142.36

Reference:

- [mortgage-calculator](http://www.bankrate.com/calculators/mortgages/mortgage-calculator.aspx)
- [What's in a Mortgage Payment?](http://www.homeloanlearningcenter.com/mortgagebasics/whatsinamortgagepayment.htm)
91 changes: 91 additions & 0 deletions docs/ch02/ex2.3_2.8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
##Exercise 2.3
> What output will the following code produce?
```cpp
unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl;
std::cout << u - u2 << std::endl;
int i = 10, i2 = 42;
std::cout << i2 - i << std::endl;
std::cout << i - i2 << std::endl;
std::cout << i - u << std::endl;
std::cout << u - i << std::endl;
```

Output(g++ 4.8):

```
32
4294967264
32
-32
0
0
```

##Exercise 2.4
> Write a program to check whether your predictions were
correct. If not, study this section until you understand what the problem is.

[Here](https://github.com/pezy/Cpp-Primer/blob/master/ch02/ex2_4.cpp) is the code, please test it in your computer.

##Exercise 2.5
> Determine the type of each of the following literals. Explain
the differences among the literals in each of the four examples:
- (a) 'a', L'a', "a", L"a"
- (b) 10, 10u, 10L, 10uL, 012, 0xC
- (c) 3.14, 3.14f, 3.14L
- (d) 10, 10u, 10., 10e-2

(a): character literal, wide character literal, string literal, string wide character literal.

(b): decimal, unsigned decimal, long decimal, unsigned long decimal, octal, hexadecimal.

(c): double, float, long double.

##Exercise 2.6
> What, if any, are the differences between the following
definitions:
```cpp
int month = 9, day = 7;
int month = 09, day = 07;
```

The first line's integer is decimal, the second line's integer is octal.

##Exercise 2.7
> What values do these literals represent? What type does each
have?
- (a) "Who goes with F\145rgus?\012"
- (b) 3.14e1L
- (c) 1024f
- (d) 3.14L

(a): Who goes with Fergus?(new line) "string"

(b): 31.4 "long double"

(c): 1024 "float"

(d): 3.14 "long double"

Reference:

- [ASCII Table](http://www.asciitable.com/)

##Exercise 2.8
> Using escape sequences, write a program to print 2M followed
by a newline. Modify the program to print 2, then a tab, then an M, followed
by a newline.

```cpp
#include <iostream>

int main()
{
std::cout << 2 << "\115\012";
std::cout << 2 << "\t\115\012";

return 0;
}

```
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ C++ Primer 5th Answer Note

- Part I: The Basics
- Chapter 2. Variables and Basic Types
- [Exercise 2.1 ~ 2.2](/Cpp-Primer/ch02/ex2.1_2.2)
- [Exercise 2.3 ~ 2.8](/Cpp-Primer/ch02/ex2.3_2.8)
- Chapter 3. Strings, Vectors, and Arrays
- Chapter 4. Expressions
- Chapter 5. Statements
Expand Down

0 comments on commit ff7b315

Please sign in to comment.