You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-**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.
9
+
-Use `GCC 4.9+`, `Clang 3.4+`, `MSVC 14+`, and [others](http://en.cppreference.com/w/cpp/compiler_support).
10
10
- Use `-std=c++11`(recommend: `-pedantic -Wall`) flag for compiling.
11
-
-If any bug caught, please [let me know](https://github.com/pezy/Cpp-Primer/issues/new), thanks.
11
+
-Have you discovered incorrect information? [Submit](https://github.com/pezy/CppPrimer/issues/new).
> 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.
> What does the following for loop do? What is the final value
151
-
of sum?
154
+
> What does the following for loop do? What is the final value of sum?
152
155
```cpp
153
156
int sum = 0;
154
157
for (int i = -100; i <= 100; ++i)
@@ -158,9 +161,9 @@ for (int i = -100; i <= 100; ++i)
158
161
the loop sums the numbers from -100 to 100. the final value of sum is zero.
159
162
160
163
##Exercise 1.13
161
-
> Rewrite the exercises from § 1.4.1 (p. 13) using for loops.
164
+
> Rewrite the exercises from 1.4.1 (p. 13) using for loops.
162
165
163
-
Ex1.9:
166
+
**Ex1.9**:
164
167
```cpp
165
168
#include<iostream>
166
169
@@ -176,166 +179,144 @@ int main()
176
179
}
177
180
```
178
181
179
-
Ex1.10:
182
+
**Ex1.10**:
180
183
```cpp
181
184
#include<iostream>
182
185
183
186
intmain()
184
187
{
185
188
for (int i=10; i>=0; --i)
186
-
std::cout << i << std::endl;
189
+
std::cout << i << " ";
190
+
std::cout << std::endl;
187
191
188
192
return 0;
189
193
}
190
194
```
191
195
192
-
Ex1.11:
196
+
**Ex1.11**:
193
197
```cpp
194
198
#include<iostream>
195
199
196
200
intmain()
197
201
{
198
-
int val_small = 0, val_big = 0;
202
+
int small = 0, big = 0;
199
203
std::cout << "please input two integers:";
200
-
std::cin >> val_small >> val_big;
204
+
std::cin >> small >> big;
201
205
202
-
if (val_small > val_big) {
203
-
int tmp = val_small;
204
-
val_small = val_big;
205
-
val_big = tmp;
206
+
if (small > big) {
207
+
int tmp = small;
208
+
small = big;
209
+
big = tmp;
206
210
}
207
211
208
-
for (int i = val_small; i <= val_big; ++i)
209
-
std::cout << i << std::endl;
212
+
for (int i = small; i <= big; ++i)
213
+
std::cout << i << " ";
214
+
std::cout << std::endl;
210
215
211
216
return 0;
212
217
}
213
218
```
214
219
215
220
##Exercise 1.14
216
-
> Compare and contrast the loops that used a for with those
217
-
using a while. Are there advantages or disadvantages to using either form?
221
+
> Compare and contrast the loops that used a `for` with those using a `while`. Are there advantages or disadvantages to using either form?
222
+
223
+
- Advantage of `for` and disadvantage of `while`:
224
+
- Locality, the variable in the scope of the loop.
225
+
- Pattern happens so often: using a variable in a condition and incrementing that variable in the body.
226
+
- Advantage of `while` and disadvantage of `for`:
227
+
- Clear when there is only one static condition.
228
+
- Readable when the global variables incremented in the body.
218
229
219
-
If you need a pattern which is using a variable in a condition and incrementing that variable in the
220
-
body. You should use `for` loop. Else the `while` loop is more simple.
230
+
-----
221
231
222
-
Want to know more? look at [this](http://stackoverflow.com/questions/1600282/guideline-while-vs-for)
232
+
[A similar question on Stack Overflow](http://stackoverflow.com/questions/2950931/for-vs-while-in-c-programming)
223
233
224
234
##Exercise 1.15
225
-
> Write programs that contain the common errors discussed in
226
-
the box on page 16. Familiarize yourself with the messages the compiler
227
-
generates.
235
+
> Write programs that contain the common errors discussed in the box on page 16. Familiarize yourself with the messages the compiler generates.
228
236
229
-
**JUST READ IT!**
237
+
Self-training.
230
238
231
239
##Exercise 1.16
232
240
233
-
> Write your own version of a program that prints the sum of a set of integers read from cin.
234
-
235
-
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).
236
-
237
-
In my opinion, the exercise aim to write the program without "**END-OF-FILE**".
238
-
239
-
**BUT**, the [code](http://www.cplusplus.com/forum/beginner/104169/#msg561450) in first link is not correct.
240
-
241
-
The following are my own version:
241
+
> Write your own version of a program that prints the sum of a set of integers read from `cin`.
242
242
243
243
```cpp
244
244
#include<iostream>
245
245
246
246
intmain()
247
247
{
248
-
int limit = 0, sum = 0, value = 0;
249
-
std::cout << "How many integers would you like to enter?";
250
-
std::cin >> limit;
251
-
252
-
// assume we don't know what is EOF(End-Of-File).
253
-
while (std::cin >> value && (--limit != 0))
248
+
int sum = 0;
249
+
for (int value = 0; std::cin >> value; )
254
250
sum += value;
255
-
256
-
std::cout << sum + value << std::endl;
257
-
251
+
std::cout << sum << std::endl;
258
252
return 0;
259
253
}
260
254
```
261
255
262
-
Watch out for "sum + value" in the `cout` line.
263
-
264
256
##Exercise 1.17
265
257
266
258
> What happens in the program presented in this section if the input values are all equal? What if there are no duplicated values?
267
259
268
-
If the input values are all equal, it will print a line which shows the count of the number you input.
269
-
270
-
If there are no duplicated values, when different values input, a new line will be printed if you click `Enter`.
260
+
If all equal, the count will be printed out. If there are no duplicated values, A new line will be printed when `Enter` clicked.
271
261
272
262
##Exercise 1.18
273
263
274
264
> 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.
> 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.
281
272
282
-
Yes, we should use `if` to judge which is bigger.
283
-
284
-
review this [code](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_11.cpp)
> 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.
289
278
290
-
[Here](ex1_20.cpp) is the code.
279
+
check [code](ex1_20.cpp).
291
280
292
-
**You need to enable C++11 support in your compiler.
293
-
With GCC and Clang, this can be done with the `-std=c++11` option.**
0 commit comments