1、C语言程序设计现代方法第二版习题答案 时磊忖呎 Chapter 2An swers to Selected Exercises2.was #2 (a) The program contains one directive (#inelude) and four statements (three calls of printf and one return).(b)Parkinsons Law:Work expands so as to fill the timeavailable for its completi on.3.was #4#i nclude int main(v oid)int
2、 height = 8, le ngth = 12, width = 10, volume;volume = height * len gth * width;printf(Dimensions: %dx%dx%dn, length, width, height);prin tf(Volume (cubic in ches): %dn, volume);printf(Dimensional weight (pounds): %dn, (volume + 165) / 166);return 0;4.was #6 Heres one possible program:#i nclude int
3、main(v oid)int i, j, k;float x, y, z;prin tf(Value of i: %dn, i);prin tf(Value of j: %dn, j);prin tf(Value of k: %dn, k);prin tf(Value of x: %gn, x);prin tf(Value of y: %gn, y); 时磊忖呎 printf(Value of z: %gn, z);return 0;Whe n compiled using GCC and the n executed, this program produced thefollow ing
4、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 chanee that youll get exactly these nu mbers is small.5.was #10 (a) is not legal because 100_bottles beg ins with a digit.8
5、.was #12 There are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3,and ;.An swers to Selected Programmi ng Projects4.was #8; modified#i nclude int main(v oid)float orig in al_am ount, amoun t_with_tax;prin tf(E nter an amoun t:);sca nf(%f, &orig in al_am oun t);amoun t_with_tax = orig in al_am ount
6、 * 1.05f;prin tf(With tax added: $%.2fn, amou nt_with_tax);return 0;The amount_with_tax variable is unnecessary. If we remove it, the programis slightly shorter: #i nclude int main(v oid)float orig in al_am ount;prin tf(E nter an amoun t:);sea nf(%f, &orig in al_am oun t);printf(With tax added: $%.2
7、fn, original_amount * 1.05f);return 0;Chapter 3An swers to Selected Exercises2.was #2(a)printf(%8.1e, x);(b)printf(%10.6e, x);(e) pri ntf(%-8.3f, x);(d) printf(%6.0f, x);5.respectively.was #8 The values of x, i, and y will be 12.3, 45, and .6,An swers to Selected Programmi ng Projects1.was #4; modif
8、ied#i nclude int main(v oid)int mon th, day, year;prin tf(E nter a date (mm/dd/yyyy):);sca nf(%d/%d/%d, &mon th, & day, &year);prin tf(You en tered the date %d%.2d%.2dn, year, mon th, day);return 0;3.was #6; modified#i nclude int main(v oid)int prefix, group, publisher, item, check_digit;printf(Ente
9、r ISBN:);sca nf(%d-%d-%d-%d-%d, & prefix, & group, & publisher, & tem, &check_digit);prin tf(GS1 prefix: %dn, prefix);prin tf(Group ide ntifier: %dn, group);prin tf(Publisher code: %dn, publisher);printf(Item number: %dn, item);prin tf(Check digit: %dn, check_digit);/* The five printf calls can be c
10、ombined as follows:printf(GS1 prefix: %dnGroup identifier: %dnPublishercode: %dnItem number: %dnCheck digit: %dn,prefix, group, publisher, item, check_digit);*/return 0;Chapter 4An swers 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 o
11、r 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 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 expressi on +i is equivale nt to
12、(i += 1). The value of both expressi ons is i after the in creme nt has bee n performed.An swers to Selected Programmi ng Projects2. was #4#i nclude int main(v oid)int n;prin tf(E nter a three-digit nu mber:);sca nf(%d, &n);printf(The reversal is: %d%d%dn, n % 10, (n / 10) % 10, n / 100);return 0;Ch
13、apter 5An swers to Selected Exercises2. was #2(a)1(b)1(c)1(d)14.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 ison etwosince there are no break stateme nts after the cases.An swers to Selected Progr
14、ammi ng Projects 2. was #6#i nclude int main(v oid)int hours, minu tes;prin tf(E nter a 24-hour time:); sca nf(%d:%d, & hours, &min utes);prin tf(Equivale nt 12-hour time:);if (hours = 0)printf(12:%.2d AMn, minutes);else if (hours 12)prin tf(%d:%.2d AMn, hours, mi nutes); else if (hours = 12)prin tf(%d:%.2d PMn, hours, mi nutes); elseprin tf(%d:%.2d PMn, hours - 12, minu tes);return 0;4. was #8; modified#i nclude int main(v oid)int speed;prin tf(E nter a wi nd speed in kno ts:); scanf(%d, &spee
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1