Chapter Review

1
An entry-condition loop evaluates a test expression before entering the body of the loop. If the condition is initially false, the loop never executes its body. An exit-condition loop evaluates a test expression after processing the body of the loop. Thus, the loop body is executed once, even if the test expression is initially false. The for and while loops are entry-condition loops, and the do while loop is an exit-condition loop.
2
It would print the following:

  1. 01234

Not that cout << endl; is not part of the loop body (because there are no braces).

3
It would print the following:

  1. 0369
  2. 12

4
It would print the following:

  1. 6
  2. 8

5
It would print the following:

  1. k = 8;

6
It's simplest to use the *= operator:

  1. for (int num = 1; num <= 64; num *= 2)
  2. cout << num << " ";

7
You enclose the statements within paired braces to form a single compound statement, or block.
8
Yes, the first statement is valid. The expression 1,024 consists of two expressions — 1 and 024 — joined by a comma operator. The value of the right-hand expression. This is 024, which is octal for 20, so the declaration assigns the value 20 to x. The second statement is also valid. However, operator precedence causes it to be evaluated as follows:
(y = 1), 024;
That is, the left expression sets y to 1, and the value of the entire expression, which isn't used, is 024, or 20.
9
The cin >> ch form skips over spaces, newlines, and tabs when it encounters them. The other two forms read those characters.

Programming Exercises

1

  1. #include <iostream>
  2. int main()
  3. {
  4. using namespace std;
  5. int n1, n2;
  6. int sum = 0;
  7. cout << "Input two integer numbers (example: 2 9): ";
  8. cin >> n1;
  9. cin >> n2;
  10. for (int i = n1; i <= n2; ++i)
  11. {
  12. sum += i;
  13. }
  14. cout << "sum = " << sum << endl;
  15. return 0;
  16. }

2

  1. #include <iostream>
  2. #include <array>
  3. const int ArSize = 101;
  4. int main()
  5. {
  6. using namespace std;
  7. array<long double, ArSize> factorials;
  8. factorials[1] = factorials[0] = 1.0L;
  9. for (int i = 2; i < ArSize; ++i)
  10. {
  11. factorials[i] = i * factorials[i - 1];
  12. }
  13. for (int i = 0; i < ArSize; ++i)
  14. {
  15. cout << i << "! = " << factorials[i] << endl;
  16. }
  17. return 0;
  18. }

3

  1. #include <iostream>
  2. int main()
  3. {
  4. using namespace std;
  5. double x;
  6. double sum = 0.0;
  7. cin >> x;
  8. while (x != 0.0)
  9. {
  10. sum += x;
  11. cin >> x;
  12. }
  13. cout << "sum = " << sum << endl;
  14. return 0;
  15. }

4

  1. #include <iostream>
  2. int main()
  3. {
  4. using namespace std;
  5. double d, c;
  6. d = c = 100.0;
  7. int i;
  8. for (i = 0; d >= c; ++i)
  9. {
  10. d += 0.1 * 100.0;
  11. c *= 1.05;
  12. }
  13. cout << i << " year(s)\n";
  14. cout << "Daphne: " << d << endl;
  15. cout << "Cleo: " << c << endl;
  16. return 0;
  17. }

5

  1. #include <iostream>
  2. const char * const Months[12] =
  3. {
  4. "January",
  5. "February",
  6. "March",
  7. "April",
  8. "May",
  9. "June",
  10. "July",
  11. "August",
  12. "September",
  13. "October",
  14. "November",
  15. "December"
  16. };
  17. int main()
  18. {
  19. using namespace std;
  20. int volumes[12];
  21. int sum = 0;
  22. for (int i = 0; i < 12; ++i)
  23. {
  24. cout << "Enter the sales volume of " << Months[i] << ": ";
  25. cin >> volumes[i];
  26. }
  27. for (int i = 0; i < 12; ++i)
  28. sum += volumes[i];
  29. cout << "Sum = " << sum << endl;
  30. return 0;
  31. }

6

  1. #include <iostream>
  2. const char * const Months[12] =
  3. {
  4. "January",
  5. "February",
  6. "March",
  7. "April",
  8. "May",
  9. "June",
  10. "July",
  11. "August",
  12. "September",
  13. "October",
  14. "November",
  15. "December"
  16. };
  17. int main()
  18. {
  19. using namespace std;
  20. int volumes[3][12];
  21. int sum = 0;
  22. int total = 0;
  23. for (int i = 0; i < 3; ++i)
  24. {
  25. cout << "Enter the sales volumes of year: " << i + 1 << endl << endl;
  26. for (int j = 0; j < 12; ++j)
  27. {
  28. cout << "Enter the sales volumes of " << Months[j] << ": ";
  29. cin >> volumes[i][j];
  30. }
  31. }
  32. for (int i = 0; i < 3; ++i)
  33. {
  34. for (int j = 0; j < 12; ++j)
  35. {
  36. sum += volumes[i][j];
  37. }
  38. cout << "Sales volume of year " << i + 1 << " is " << sum << endl;
  39. total += sum;
  40. sum = 0;
  41. }
  42. cout << "Sales volumes of 3 years are: " << total << endl;
  43. return 0;
  44. }

7

  1. #include <iostream>
  2. struct car
  3. {
  4. char make[40];
  5. int year;
  6. };
  7. int main()
  8. {
  9. using namespace std;
  10. int num;
  11. car * cars;
  12. cout << "How many cars do you wish to catalog: ";
  13. cin >> num;
  14. cars = new car[num];
  15. for (int i = 0; i < num; i++)
  16. {
  17. cout << "Car #" << i + 1 << ":\n";
  18. cout << "Please enter the make: ";
  19. //cin >> cars[i].make;
  20. cin.getline(cars[i].make, 40);
  21. cin.get();
  22. cout << "Please enter the year made: ";
  23. cin >> cars[i].year;
  24. }
  25. cout << "Here is your collection:\n";
  26. for (int i = 0; i < num; ++i)
  27. cout << cars[i].year << " " << cars[i].make << endl;
  28. delete [] cars;
  29. return 0;
  30. }

8

  1. #include <iostream>
  2. #include <cstring>
  3. int main()
  4. {
  5. using namespace std;
  6. char word[20];
  7. int count = 0;
  8. cout << "Enter word (to stop, type the word done):\n";
  9. cin >> word;
  10. while (strcmp(word, "done"))
  11. {
  12. ++count;
  13. cin >> word;
  14. }
  15. cout << "You entered a total of " << count << " words.\n";
  16. return 0;
  17. }

9

  1. #include <iostream>
  2. #include <string>
  3. int main()
  4. {
  5. using namespace std;
  6. string word;
  7. int count = 0;
  8. cout << "Enter word (to stop, type the word done):\n";
  9. cin >> word;
  10. while (word != "done")
  11. {
  12. ++count;
  13. cin >> word;
  14. }
  15. cout << "You entered a total of " << count << " words.\n";
  16. return 0;
  17. }

10

  1. #include <iostream>
  2. int main()
  3. {
  4. using namespace std;
  5. cout << "Enter number of rows: ";
  6. int n;
  7. cin >> n;
  8. for (int i = 0; i < n; ++i) // row
  9. {
  10. for (int j = 0; j < n; ++j) // column
  11. if (j < (n - (i + 1)))
  12. cout << ".";
  13. else
  14. cout << "*";
  15. cout << endl;
  16. }
  17. return 0;
  18. }

c++-pimer-plus-6th-chapter05的更多相关文章

  1. The 6th tip of DB Query Analyzer

      The 6th tip of DB Query Analyzer MA Gen feng (Guangdong Unitoll Services incorporated, Guangzhou ...

  2. [转载]ECMA-262 6th Edition / Draft August 24, 2014 Draft ECMAScript Language Specification

    http://people.mozilla.org/~jorendorff/es6-draft.html#sec-23.4 Draft Report Errors and Issues at: htt ...

  3. chapter05

    /** * Created by EX-CHENZECHAO001 on 2018-03-29. */class Chapter05 { } // 类// 类中的字段自动带有getter方法和sett ...

  4. Chapter05 流程控制(Process Control)

    目录 Chapter05 流程控制 5.1 顺序控制 5.2 分支控制 if-else 单分支基本语法: 双分支基础语法: 多分支基础语法 5.3 嵌套分支 5.4 switch分支结构 5.5 Fo ...

  5. ​Si2151/41 6th Generation Silicon TV Tuner ICs

    ​ The Si2151/41 are the industry's most advanced silicon TV tuner ICs supporting all worldwide terre ...

  6. Codeforces Round #361 Jul.6th B题 ☺译

    最近迈克忙着考前复习,他希望通过出门浮躁来冷静一下.迈克所在的城市包含N个可以浮躁的地方,分别编号为1..N.通常迈克在家也很浮躁,所以说他家属于可以浮躁的地方并且编号为1.迈克从家出发,去一些可以浮 ...

  7. Codeforces Round #361 Jul.6th A题 ☺译

    A.迈克和手机 当迈克在沙滩上游泳的时候,他意外的把他的手机扔进了水里.不过你甭担心因为他立马买了个便宜些的代替品,这个代替品是老款九键键盘,这个键盘只有十个等大的数字按键,按以下方式排列: 1 2 ...

  8. October 6th 2016 Week 41st Thursday

    The outer world you see is a reflection of your inner self. 你拥有什么样的内心,你就会看到什么样的世界. And we eventually ...

  9. September 6th 2016 Week 37th Tuesday

    I only wish to face the sea, with spring flowers blossoming. 我只愿面朝大海,春暖花开. That scenery is beautiful ...

  10. July 6th, Week 28th Wednesday, 2016

    Diligence is the mother of good fortune. 勤勉是好运之母. The mother of good fortune can be diligence, conti ...

随机推荐

  1. 使用Navicat for MySQL添加外键约束

    转载:http://blog.csdn.net/u013215018/article/details/54981216 现在有两个表一张是Roles表(角色表),一张是RoleUser表(用户角色) ...

  2. 【python35.1--EasyGui界面】

    一.什么是EasyGUI EasyGUI是python中一个非常简单的GUI编程模块,不同于其他的GUI生成器,它不是事件驱动的,相反,所有的GUI交互都是通过简地函数调用就可以实现(意思是:函数调用 ...

  3. python --- 14 递归 二分法查找

    一.递归 1.函数自己调用自己 2.官方说明最大深度1000,但跑不到1000,要看解释器, 实测998 3.使⽤递归来遍历各种树形结构 二.    二分法查找 掐头结尾取中间 ,  必须是有序序列 ...

  4. sftp服务器的安装与远程

    本文所描述环境只在window系统下 一.搭建sftp服务器 1.首先需要下载一个软件freeSSHd,下载地址http://www.freesshd.com/?ctt=download,下载第一个f ...

  5. topcoder srm 305 div1

    problem1 link 直接按照题意模拟即可. import java.util.*; import java.math.*; import static java.lang.Math.*; pu ...

  6. shell编程中的单/双 小括号, 中括号, 大括号

    linux shell中的变量类型?分字符串或者数字或者bool类型吗? 参考: http://www.cnblogs.com/nufangrensheng/p/3477281.html 不分! sh ...

  7. 2870: 最长道路tree

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2870 思路 先把树转化为二叉树 再链分治 %%yyb 代码 #include <ios ...

  8. 洛谷luogu2782

    P2782 友好城市 题目描述 有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市.北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同.每对友好城市都向政府申 ...

  9. C#获取文件MD5值方法

    https://www.cnblogs.com/Ruiky/archive/2012/04/16/2451663.html private static string GetMD5HashFromFi ...

  10. 论文笔记之:DualGAN: Unsupervised Dual Learning for Image-to-Image Translation

    DualGAN: Unsupervised Dual Learning for Image-to-Image Translation 2017-06-12  21:29:06   引言部分: 本文提出 ...