C语言程序设计现代方法第二版习题答案CProgrammingAModernApproach

上传人:m**** 文档编号:486310778 上传时间:2023-04-21 格式:DOC 页数:87 大小:306.50KB
返回 下载 相关 举报
C语言程序设计现代方法第二版习题答案CProgrammingAModernApproach_第1页
第1页 / 共87页
C语言程序设计现代方法第二版习题答案CProgrammingAModernApproach_第2页
第2页 / 共87页
C语言程序设计现代方法第二版习题答案CProgrammingAModernApproach_第3页
第3页 / 共87页
C语言程序设计现代方法第二版习题答案CProgrammingAModernApproach_第4页
第4页 / 共87页
C语言程序设计现代方法第二版习题答案CProgrammingAModernApproach_第5页
第5页 / 共87页
点击查看更多>>
资源描述

《C语言程序设计现代方法第二版习题答案CProgrammingAModernApproach》由会员分享,可在线阅读,更多相关《C语言程序设计现代方法第二版习题答案CProgrammingAModernApproach(87页珍藏版)》请在金锄头文库上搜索。

1、.Chapter 2Answers to Selected Exercises2. was #2 (a) The program contains one directive (#include) and four statements (three calls of printf and one return).(b) Parkinsons Law:Work expands so as to fill the timeavailable for its completion.3. was #4#include int main(void) int height = 8, length = 1

2、2, width = 10, volume; volume = height * length * width; printf(Dimensions: %dx%dx%dn, length, width, height); printf(Volume (cubic inches): %dn, volume); printf(Dimensional weight (pounds): %dn, (volume + 165) / 166); return 0;4. was #6 Heres one possible program:#include int main(void) int i, j, k

3、; float x, y, z; printf(Value of i: %dn, i); printf(Value of j: %dn, j); printf(Value of k: %dn, k); printf(Value of x: %gn, x); printf(Value of y: %gn, y);精品. printf(Value of z: %gn, z); return 0;When compiled using GCC and then executed, this program produced the following output:Value of i: 56188

4、48Value of j: 0Value of k: 6844404Value of x: 3.98979e-34Value of y: 9.59105e-39Value of z: 9.59105e-39The values printed depend on many factors, so the chance that youll get exactly these numbers is small.5. was #10 (a) is not legal because 100_bottles begins with a digit.8. was #12 There are 14 to

5、kens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;. Answers to Selected Programming Projects4. was #8; modified#include int main(void) float original_amount, amount_with_tax; printf(Enter an amount: ); scanf(%f, &original_amount); amount_with_tax = original_amount * 1.05f; printf(With tax added: $%.

6、2fn, amount_with_tax); return 0;The amount_with_tax variable is unnecessary. If we remove it, the program is slightly shorter:#include 精品.int main(void) float original_amount; printf(Enter an amount: ); scanf(%f, &original_amount); printf(With tax added: $%.2fn, original_amount * 1.05f); return 0;Ch

7、apter 3Answers to Selected Exercises2. was #2 (a) printf(%-8.1e, x); (b) printf(%10.6e, x); (c) printf(%-8.3f, x); (d) printf(%6.0f, x); 5. was #8 The values of x, i, and y will be 12.3, 45, and .6, respectively.Answers to Selected Programming Projects1. was #4; modified #include int main(void) int

8、month, day, year; printf(Enter a date (mm/dd/yyyy): ); scanf(%d/%d/%d, &month, &day, &year); printf(You entered the date %d%.2d%.2dn, year, month, day); return 0;3. was #6; modified 精品.#include int main(void) int prefix, group, publisher, item, check_digit; printf(Enter ISBN: ); scanf(%d-%d-%d-%d-%d

9、, &prefix, &group, &publisher, &item, &check_digit); printf(GS1 prefix: %dn, prefix); printf(Group identifier: %dn, group); printf(Publisher code: %dn, publisher); printf(Item number: %dn, item); printf(Check digit: %dn, check_digit); /* The five printf calls can be combined as follows: printf(GS1 p

10、refix: %dnGroup identifier: %dnPublisher code: %dnItem number: %dnCheck digit: %dn, prefix, group, publisher, item, check_digit); */ return 0;Chapter 4Answers to Selected Exercises2. was #2 Not in C89. Suppose that i is 9 and j is 7. The value of (-i)/j could be either 1 or 2, depending on the imple

11、mentation. On the other hand, the value of -(i/j) is always 1, regardless of the implementation. In C99, on the other hand, the value of (-i)/j must be equal to the value of -(i/j). 9. was #6 (a) 63 8 (b) 3 2 1 (c) 2 -1 3 (d) 0 0 0 精品.13. was #8 The expression +i is equivalent to (i += 1). The value

12、 of both expressions is i after the increment has been performed. Answers to Selected Programming Projects2. was #4 #include int main(void) int n; printf(Enter a three-digit number: ); scanf(%d, &n); printf(The reversal is: %d%d%dn, n % 10, (n / 10) % 10, n / 100); return 0;Chapter 5Answers to Selec

13、ted Exercises2. was #2 (a) 1 (b) 1 (c) 1 (d) 1 4. was #4 (i j) - (i j) 6. was #12 Yes, the statement is legal. When n is equal to 5, it does nothing, since 5 is not equal to 9. 10. was #16 The output is onetwosince there are no break statements after the cases. Answers to Selected Programming Projec

14、ts精品.2. was #6 #include int main(void) int hours, minutes; printf(Enter a 24-hour time: ); scanf(%d:%d, &hours, &minutes); printf(Equivalent 12-hour time: ); if (hours = 0) printf(12:%.2d AMn, minutes); else if (hours 12) printf(%d:%.2d AMn, hours, minutes); else if (hours = 12) printf(%d:%.2d PMn, hours, minutes); else printf(%d:%.2d PMn, hours - 12, minutes); return 0;4.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号