c语言程序设计现代方法(第二版)习题答案.doc
《c语言程序设计现代方法(第二版)习题答案.doc》由会员分享,可在线阅读,更多相关《c语言程序设计现代方法(第二版)习题答案.doc(72页珍藏版)》请在三一办公上搜索。
1、c语言程序设计现代方法(第二版)习题答案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 <stdio.h> int mai
2、n(void)int height = 8, length = 12, 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
3、 #6 Heres one possible program:#include <stdio.h> int main(void)int i, j, k;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(
4、"Value of z: %gn", z); return 0;When compiled using GCC and then executed, this program produced the following output:Value of i: 5618848Value 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 ch
5、ance 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 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;.Answers to Selected Programming Projects4. was #8; modified#include <stdio.h> int main(void)float
6、original_amount, amount_with_tax; printf("Enter an amount: ");scanf("%f", &original_amount);amount_with_tax = original_amount * 1.05f;printf("With tax added: $%.2fn", amount_with_tax); return 0;The amount_with_tax variable is unnecessary. If we remove it, the progra
7、m is slightly shorter:#include <stdio.h> 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; Chapter 3Answers to Selected Exercises2. was #2(a) print
8、f("%-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 <stdio.h> int main(void)
9、int 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 <stdio.h> int main(void)int prefix, group, publisher, i
10、tem, check_digit; printf("Enter ISBN: ");scanf("%d-%d-%d-%d-%d", &prefix, &group, &publisher, &item, &check_digit); printf("GS1 prefix: %dn", prefix);printf("Group identifier: %dn", group);printf("Publisher code: %dn", publisher);
11、printf("Item number: %dn", item);printf("Check digit: %dn", check_digit); /* The five printf calls can be combined as follows: printf("GS1 prefix: %dnGroup identifier: %dnPublisher code: %dnItem number: %dnCheck digit: %dn",prefix, group, publisher, item, check_digit);*
12、/ 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 implementation. On the other hand, the value of -(i/j) is always 1, regardless of the implementation. In C99, on the other hand, the va
13、lue 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 013. was #8 The expression +i is equivalent to (i += 1). The value of both expressions is i after the increment has been performed. Answers to Selected Programming Projects2. was #4#include <stdio.h>
14、 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 Selected Exercises2. was #2(a) 1(b) 1(c) 1(d) 14. was #4 (i > j) - (i < j)6. was #12 Y
15、es, 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 isonetwosince there are no break statements after the cases.Answers to Selected Programming Projects2. was #6#include <stdio.h> int main(void)int hours, minutes; printf("Ent
16、er 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&quo
17、t;, hours, minutes); elseprintf("%d:%.2d PMn", hours - 12, minutes);return 0;4. was #8; modified#include <stdio.h> int main(void)int speed; printf("Enter a wind speed in knots: "); scanf("%d", &speed); if (speed < 1)printf("Calmn");else if (speed
18、<= 3)printf("Light airn");else if (speed <= 27)printf("Breezen");else if (speed <= 47)printf("Galen");else if (speed <= 63)printf("Stormn");elseprintf("Hurricanen"); return 0;6. was #10#include <stdio.h> int main(void)int check_digi
19、t, d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total; printf("Enter the first (single) digit: ");scanf("%1d", &d);printf("Enter first group of five digits: ");scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5); prin
20、tf("Enter second group of five digits: ");scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5); printf("Enter the last (single) digit: ");scanf("%1d", &check_digit); first_sum = d + i2 + i4 + j1 + j3 + j5;second_sum = i1 + i3 + i5 + j2 + j
21、4;total = 3 * first_sum + second_sum; if (check_digit = 9 - (total - 1) % 10)printf("VALIDn");elseprintf("NOT VALIDn"); return 0;10. was #14#include <stdio.h> int main(void)int grade; printf("Enter numerical grade: ");scanf("%d", &grade); if (grade &
22、lt; 0 | grade > 100) printf("Illegal graden");return 0; switch (grade / 10) case 10:case 9: printf("Letter grade: An");break;case 8: printf("Letter grade: Bn");break;case 7: printf("Letter grade: Cn");break;case 6: printf("Letter grade: Dn");break
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言程序设计 现代 方法 第二 习题 答案

链接地址:https://www.31ppt.com/p-2384671.html