Skip to content

Commit

Permalink
review ch01-05
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jun 24, 2015
1 parent 5e5e5fc commit aec2b0b
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 140 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
##C++ Primer 5th Answers

[![GitHub issues](https://img.shields.io/github/issues/Mooophy/Cpp-Primer.svg)](https://github.com/pezy/CppPrimer/issues)
[![GitHub issues](https://img.shields.io/github/issues/pezy/CppPrimer.svg)](https://github.com/pezy/CppPrimer/issues)
[![GitHub license](https://img.shields.io/badge/license-CC0-blue.svg)](https://raw.githubusercontent.com/pezy/Cpp-Primer/master/LICENSE)
[![](https://img.shields.io/badge/%E4%B8%AD%E6%96%87-%E8%AE%A8%E8%AE%BA%E5%8C%BA-yellowgreen.svg)](https://github.com/ReadingLab/Discussion-for-Cpp)

### Notes

- **g++** and **clang++** are recommended. Visual Studio 2013 may be used as well, but up to June 2015, Visual Studio has not implemented all c++11 features yet.
- Use `GCC 4.9+`, `Clang 3.4+`, `MSVC 14+`, and [others](http://en.cppreference.com/w/cpp/compiler_support).
- Use `-std=c++11`(recommend: `-pedantic -Wall`) flag for compiling.
- If any bug caught, please [let me know](https://github.com/pezy/Cpp-Primer/issues/new), thanks.
- Have you discovered incorrect information? [Submit](https://github.com/pezy/CppPrimer/issues/new).

### Contents

Expand Down
153 changes: 67 additions & 86 deletions ch01/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
# Chapter 1. Getting Started

##Exercise 1.1

> Review the documentation for your compiler and determine what file naming convention it uses. Compile and run the main program from page 2.
### Windows

![windows](https://db.tt/XGeGsg7O)
![windows](https://cloud.githubusercontent.com/assets/1147451/8334465/a87e3528-1aca-11e5-877d-c610f087fc40.png)


### Linux

![Linux](https://db.tt/2xKWuztU)
![Linux](https://cloud.githubusercontent.com/assets/1147451/8334480/c160e75c-1aca-11e5-92d5-7d0a05fbf493.png)


##Exercise 1.2

> Exercise 1.2: Change the program to return -1. A return value of -1 is often treated as an indicator that the program failed. Recompile and rerun your program to see how your system treats a failure indicator from main.
###Windows

![windows](https://db.tt/DIJd9eZb)
![image](https://cloud.githubusercontent.com/assets/1147451/8335952/72179d5e-1ad3-11e5-84ff-e924816e64a3.png)

###Linux

![linux](https://db.tt/lhzXhpCt)
![image](https://cloud.githubusercontent.com/assets/1147451/8335963/8debbc5e-1ad3-11e5-9761-013139d291d8.png)

**255**? why? please look at [this](http://www.tldp.org/LDP/abs/html/exitcodes.html)
**255**? why? check [this](http://www.tldp.org/LDP/abs/html/exitcodes.html)

##Exercise 1.3
> Write a program to print Hello, World on the standard output.
Expand All @@ -32,8 +36,8 @@

int main()
{
std::cout << "Hello, World" << std::endl;
return 0;
std::cout << "Hello, World" << std::endl;
return 0;
}
```

Expand Down Expand Up @@ -111,7 +115,8 @@ int main()

Compiled result(g++):

![result](https://db.tt/CqQKu8GQ)
![ex1_7](https://cloud.githubusercontent.com/assets/1147451/8334581/4fb4a408-1acb-11e5-98e3-54c0929198ec.png)


##Exercise 1.8

Expand All @@ -122,12 +127,11 @@ std::cout << "*/";
std::cout << /* "*/" */;
std::cout << /* "*/" /* "/*" */;
```
> After you’ve predicted what will happen, test your answers by compiling a
program with each of these statements. Correct any errors you encounter.
> After you’ve predicted what will happen, test your answers by compiling a program with each of these statements. Correct any errors you encounter.
Compiled result(g++):

![result](https://db.tt/mrL9hDCS)
![ex1_8](https://cloud.githubusercontent.com/assets/1147451/8334603/6aa321e0-1acb-11e5-988a-57e87a53b141.png)

Corrected? just added a quote:
```cpp
Expand All @@ -142,13 +146,12 @@ Output:
/**/ */ /*
```

##[Exercise 1.9](ex1_9.cpp)
##[Exercise 1.9](ex1_09.cpp)
##[Exercise 1.10](ex1_10.cpp)
##[Exercise 1.11](ex1_11.cpp)

##Exercise 1.12
> What does the following for loop do? What is the final value
of sum?
> What does the following for loop do? What is the final value of sum?
```cpp
int sum = 0;
for (int i = -100; i <= 100; ++i)
Expand All @@ -158,9 +161,9 @@ for (int i = -100; i <= 100; ++i)
the loop sums the numbers from -100 to 100. the final value of sum is zero.

##Exercise 1.13
> Rewrite the exercises from § 1.4.1 (p. 13) using for loops.
> Rewrite the exercises from 1.4.1 (p. 13) using for loops.
Ex1.9:
**Ex1.9**:
```cpp
#include <iostream>

Expand All @@ -176,166 +179,144 @@ int main()
}
```

Ex1.10:
**Ex1.10**:
```cpp
#include <iostream>

int main()
{
for (int i=10; i>=0; --i)
std::cout << i << std::endl;
std::cout << i << " ";
std::cout << std::endl;

return 0;
}
```

Ex1.11:
**Ex1.11**:
```cpp
#include <iostream>

int main()
{
int val_small = 0, val_big = 0;
int small = 0, big = 0;
std::cout << "please input two integers:";
std::cin >> val_small >> val_big;
std::cin >> small >> big;

if (val_small > val_big) {
int tmp = val_small;
val_small = val_big;
val_big = tmp;
if (small > big) {
int tmp = small;
small = big;
big = tmp;
}

for (int i = val_small; i <= val_big; ++i)
std::cout << i << std::endl;
for (int i = small; i <= big; ++i)
std::cout << i << " ";
std::cout << std::endl;

return 0;
}
```

##Exercise 1.14
> Compare and contrast the loops that used a for with those
using a while. Are there advantages or disadvantages to using either form?
> Compare and contrast the loops that used a `for` with those using a `while`. Are there advantages or disadvantages to using either form?
- Advantage of `for` and disadvantage of `while`:
- Locality, the variable in the scope of the loop.
- Pattern happens so often: using a variable in a condition and incrementing that variable in the body.
- Advantage of `while` and disadvantage of `for`:
- Clear when there is only one static condition.
- Readable when the global variables incremented in the body.

If you need a pattern which is using a variable in a condition and incrementing that variable in the
body. You should use `for` loop. Else the `while` loop is more simple.
-----

Want to know more? look at [this](http://stackoverflow.com/questions/1600282/guideline-while-vs-for)
[A similar question on Stack Overflow](http://stackoverflow.com/questions/2950931/for-vs-while-in-c-programming)

##Exercise 1.15
> Write programs that contain the common errors discussed in
the box on page 16. Familiarize yourself with the messages the compiler
generates.
> Write programs that contain the common errors discussed in the box on page 16. Familiarize yourself with the messages the compiler generates.
**JUST READ IT!**
Self-training.

##Exercise 1.16

> Write your own version of a program that prints the sum of a set of integers read from cin.
Many people confused about this exercise, such as [this](http://www.cplusplus.com/forum/beginner/104169/) and [this](http://stackoverflow.com/questions/17841424/how-to-write-this-while-loop-as-a-for-loop).

In my opinion, the exercise aim to write the program without "**END-OF-FILE**".

**BUT**, the [code](http://www.cplusplus.com/forum/beginner/104169/#msg561450) in first link is not correct.

The following are my own version:
> Write your own version of a program that prints the sum of a set of integers read from `cin`.
```cpp
#include <iostream>

int main()
{
int limit = 0, sum = 0, value = 0;
std::cout << "How many integers would you like to enter?";
std::cin >> limit;

// assume we don't know what is EOF(End-Of-File).
while (std::cin >> value && (--limit != 0))
int sum = 0;
for (int value = 0; std::cin >> value; )
sum += value;

std::cout << sum + value << std::endl;

std::cout << sum << std::endl;
return 0;
}
```

Watch out for "sum + value" in the `cout` line.

##Exercise 1.17

> What happens in the program presented in this section if the input values are all equal? What if there are no duplicated values?
If the input values are all equal, it will print a line which shows the count of the number you input.

If there are no duplicated values, when different values input, a new line will be printed if you click `Enter`.
If all equal, the count will be printed out. If there are no duplicated values, A new line will be printed when `Enter` clicked.

##Exercise 1.18

> Compile and run the program from this section giving it only equal values as input. Run it again giving it values in which no number is repeated.
![run](https://db.tt/F38zExnq)
![ex1_18](https://cloud.githubusercontent.com/assets/1147451/8335404/0861c478-1ad0-11e5-8083-c05a0cd9e758.png)


##Exercise 1.19

> Revise the program you wrote for the exercises in § 1.4.1 (p. 13) that printed a range of numbers so that it handles input in which the first number is smaller than the second.
Yes, we should use `if` to judge which is bigger.

review this [code](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_11.cpp)
check [ex1_11.cpp](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_11.cpp)

##Exercise 1.20

> http://www.informit.com/title/032174113 contains a copy of Sales_item.h in the Chapter 1 code directory. Copy that file to your working directory. Use it to write a program that reads a set of book sales transactions, writing each transaction to the standard output.
[Here](ex1_20.cpp) is the code.
check [code](ex1_20.cpp).

**You need to enable C++11 support in your compiler.
With GCC and Clang, this can be done with the `-std=c++11` option.**
Test it using the `data`/`book.txt`:

**(Never say it again.)**
![ex1_20](https://cloud.githubusercontent.com/assets/1147451/8335638/8f5c2bca-1ad1-11e5-9c51-288382710df2.png)

How to test it? use the `book.txt` in `data` folder. And do it like this:

![run](https://db.tt/fm8iHtkF)

##Exercise 1.21
> Write a program that reads two Sales_item objects that have the same ISBN and produces their sum.
> Write a program that reads two `Sales_item` objects that have the same ISBN and produces their sum.
The program should check whether the objects have the same ISBN.(Have a look at 1.5.2, surprise!)
The program should check whether the objects have the same ISBN.(check 1.5.2)

[Code](ex1_21.cpp)

##Exercise 1.22

> Write a program that reads several transactions for the same ISBN. Write the sum of all the transactions that were read.
Tips: this program will appear in the section 1.6.
Tip: this program will appear in the section 1.6.

[Here](ex1_22.cpp) is the code.
[Code](ex1_22.cpp).

![run](https://db.tt/UlkuvpAS)
![ex1_22](https://cloud.githubusercontent.com/assets/1147451/8335700/d85ee22c-1ad1-11e5-9612-1155145606c1.png)

##Exercise 1.23
> Write a program that reads several transactions and counts
how many transactions occur for each ISBN.

Tip: please review the `1.4.4`.

[Here](ex1_23.cpp) is the code.
[Code](ex1_23.cpp).

##Exercise 1.24
> Test the previous program by giving multiple transactions
representing multiple ISBNs. The records for each ISBN should be grouped
together.
> Test the previous program by giving multiple transactions representing multiple ISBNs. The records for each ISBN should be grouped together.
You can use data/book.txt as the records.
Use `data`/`book.txt` as the records.

![run](https://db.tt/EeDI7lvN)
![ex1_24](https://cloud.githubusercontent.com/assets/1147451/8335734/0fbefbbc-1ad2-11e5-9df3-fa1203dffb42.png)

##Exercise 1.25
> Using the Sales_item.h header from the Web site,
compile and execute the bookstore program presented in this section.

It is the same as Exercise 1.22.
##Exercise 1.25
> Using the `Sales_item.h` header from the Web site, compile and execute the bookstore program presented in this section.
![run](https://db.tt/C6OOPuzA)
![ex1_25](https://cloud.githubusercontent.com/assets/1147451/8335742/1efb475c-1ad2-11e5-9484-69ae44b79385.png)
File renamed without changes.
4 changes: 0 additions & 4 deletions ch01/ex1_1.cpp

This file was deleted.

19 changes: 10 additions & 9 deletions ch01/ex1_11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@

int main()
{
int val_small = 0, val_big = 0;
int small = 0, big = 0;
std::cout << "please input two integers:";
std::cin >> val_small >> val_big;
std::cin >> small >> big;

if (val_small > val_big) {
int tmp = val_small;
val_small = val_big;
val_big = tmp;
if (small > big) {
int tmp = small;
small = big;
big = tmp;
}

while (val_small <= val_big) {
std::cout << val_small << std::endl;
++val_small;
while (small <= big) {
std::cout << small << " ";
++small;
}
std::cout << std::endl;

return 0;
}
16 changes: 0 additions & 16 deletions ch01/ex1_16.cpp

This file was deleted.

Loading

0 comments on commit aec2b0b

Please sign in to comment.