C語(yǔ)言程序設(shè)計(jì) 現(xiàn)代方法 第二版 習(xí)題答案 C Programming_ A Modern Approach

上傳人:文*** 文檔編號(hào):28073769 上傳時(shí)間:2021-08-23 格式:DOC 頁(yè)數(shù):84 大小:295.01KB
收藏 版權(quán)申訴 舉報(bào) 下載
C語(yǔ)言程序設(shè)計(jì) 現(xiàn)代方法 第二版 習(xí)題答案 C Programming_ A Modern Approach_第1頁(yè)
第1頁(yè) / 共84頁(yè)
C語(yǔ)言程序設(shè)計(jì) 現(xiàn)代方法 第二版 習(xí)題答案 C Programming_ A Modern Approach_第2頁(yè)
第2頁(yè) / 共84頁(yè)
C語(yǔ)言程序設(shè)計(jì) 現(xiàn)代方法 第二版 習(xí)題答案 C Programming_ A Modern Approach_第3頁(yè)
第3頁(yè) / 共84頁(yè)

下載文檔到電腦,查找使用更方便

10 積分

下載資源

還剩頁(yè)未讀,繼續(xù)閱讀

資源描述:

《C語(yǔ)言程序設(shè)計(jì) 現(xiàn)代方法 第二版 習(xí)題答案 C Programming_ A Modern Approach》由會(huì)員分享,可在線閱讀,更多相關(guān)《C語(yǔ)言程序設(shè)計(jì) 現(xiàn)代方法 第二版 習(xí)題答案 C Programming_ A Modern Approach(84頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。

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 = 12

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: 5618848Va

4、lue 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 tokens

5、: 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: $%.2fn,

6、 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;Chapter 3

7、Answers 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 month,

8、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, &prefix,

9、 &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 prefix: %dn

10、Group 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 implementation.

11、 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 of both expr

12、essions 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 Selected Exercises

13、2. 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 Projects2. was #6 #

14、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);

15、 else printf(%d:%.2d PMn, hours - 12, minutes); return 0;4. was #8; modified #include int main(void) int speed; printf(Enter a wind speed in knots: ); scanf(%d, &speed); if (speed 1) printf(Calmn); else if (speed = 3) printf(Light airn); else if (speed = 27) printf(Breezen); else if (speed = 47) pri

16、ntf(Galen); else if (speed = 63) printf(Stormn); else printf(Hurricanen); return 0;6. was #10 #include int main(void) int check_digit, 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 d

17、igits: ); scanf(%1d%1d%1d%1d%1d, &i1, &i2, &i3, &i4, &i5); printf(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 + j4; tot

18、al = 3 * first_sum + second_sum; if (check_digit = 9 - (total - 1) % 10) printf(VALIDn); else printf(NOT VALIDn); return 0;10. was #14 #include int main(void) int grade; printf(Enter numerical grade: ); scanf(%d, &grade); if (grade 100) printf(Illegal graden); return 0; switch (grade / 10) case 10:

19、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; case 5: case 4: case 3: case 2: case 1: case 0: printf(Letter grade: Fn); break; return 0;Chapter 6Answers to Selected Exercises4. was #

20、10 (c) is not equivalent to (a) and (b), because i is incremented before the loop body is executed. 10. was #12 Consider the following while loop: while () continue; The equivalent code using goto would have the following appearance:while () goto loop_end; loop_end: ; /* null statement */12. was #14

21、 for (d = 2; d * d = n; d+) if (n % d = 0) break;The if statement that follows the loop will need to be modified as well:if (d * d = n) printf(%d is divisible by %dn, n, d);else printf(%d is primen, n);14. was #16 The problem is the semicolon at the end of the first line. If we remove it, the statem

22、ent is now correct: if (n % 2 = 0) printf(n is evenn);Answers to Selected Programming Projects2. was #2 #include int main(void) int m, n, remainder; printf(Enter two integers: ); scanf(%d%d, &m, &n); while (n != 0) remainder = m % n; m = n; n = remainder; printf(Greatest common divisor: %dn, m); ret

23、urn 0;4. was #4 #include int main(void) float commission, value; printf(Enter value of trade: ); scanf(%f, &value); while (value != 0.0f) if (value 2500.00f) commission = 30.00f + .017f * value; else if (value 6250.00f) commission = 56.00f + .0066f * value; else if (value 20000.00f) commission = 76.

24、00f + .0034f * value; else if (value 50000.00f) commission = 100.00f + .0022f * value; else if (value 500000.00f) commission = 155.00f + .0011f * value; else commission = 255.00f + .0009f * value; if (commission 39.00f) commission = 39.00f; printf(Commission: $%.2fnn, commission); printf(Enter value

25、 of trade: ); scanf(%f, &value); return 0;6. was #6 #include int main(void) int i, n; printf(Enter limit on maximum square: ); scanf(%d, &n); for (i = 2; i * i = n; i += 2) printf(%dn, i * i); return 0;8. was #8 #include int main(void) int i, n, start_day; printf(Enter number of days in month: ); sc

26、anf(%d, &n); printf(Enter starting day of the week (1=Sun, 7=Sat): ); scanf(%d, &start_day); /* print any leading blank dates */ for (i = 1; i start_day; i+) printf( ); /* now print the calendar */ for (i = 1; i = n; i+) printf(%3d, i); if (start_day + i - 1) % 7 = 0) printf(n); return 0;Chapter 7An

27、swers to Selected Exercises3. was #4 (b) is not legal. 4. was #6 (d) is illegal, since printf requires a string, not a character, as its first argument. 10. was #14 unsigned int, because the (int) cast applies only to j, not j * k. 12. was #16 The value of i is converted to float and added to f, the

28、n the result is converted to double and stored in d. 14. was #18 No. Converting f to int will fail if the value stored in f exceeds the largest value of type int. Answers to Selected Programming Projects1. was #2 short int values are usually stored in 16 bits, causing failure at 182. int and long in

29、t values are usually stored in 32 bits, with failure occurring at 46341. 2. was #8 #include int main(void) int i, n; char ch; printf(This program prints a table of squares.n); printf(Enter number of entries in table: ); scanf(%d, &n); ch = getchar(); /* dispose of new-line character following number

30、 of entries */ /* could simply be getchar(); */ for (i = 1; i = n; i+) printf(%10d%10dn, i, i * i); if (i % 24 = 0) printf(Press Enter to continue.); ch = getchar(); /* or simply getchar(); */ return 0;5. was #10 #include #include int main(void) int sum = 0; char ch; printf(Enter a word: ); while (c

31、h = getchar() != n) switch (toupper(ch) case D: case G: sum += 2; break; case B: case C: case M: case P: sum += 3; break; case F: case H: case V: case W: case Y: sum += 4; break; case K: sum += 5; break; case J: case X: sum += 8; break; case Q: case Z: sum += 10; break; default: sum+; break; printf(

32、Scrabble value: %dn, sum); return 0;6. was #12 #include int main(void) printf(Size of int: %dn, (int) sizeof(int); printf(Size of short: %dn, (int) sizeof(short); printf(Size of long: %dn, (int) sizeof(long); printf(Size of float: %dn, (int) sizeof(float); printf(Size of double: %dn, (int) sizeof(do

33、uble); printf(Size of long double: %dn, (int) sizeof(long double); return 0;Since the type of a sizeof expression may vary from one implementation to another, its necessary in C89 to cast sizeof expressions to a known type before printing them. The sizes of the basic types are small numbers, so its

34、safe to cast them to int. (In general, however, its best to cast sizeof expressions to unsigned long and print them using %lu.) In C99, we can avoid the cast by using the %zu conversion specification.Chapter 8Answers to Selected Exercises1. was #4 The problem with sizeof(a) / sizeof(t) is that it ca

35、nt easily be checked for correctness by someone reading the program. (The reader would have to locate the declaration of a and make sure that its elements have type t.) 2. was #8 To use a digit d (in character form) as a subscript into the array a, we would write ad-0. This assumes that digits have

36、consecutive codes in the underlying character set, which is true of ASCII and other popular character sets. 7. was #10 const int segments107 = 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1

37、, 1, 1, 1, 1, 1, 0, 1, 1;Answers to Selected Programming Projects2. was #2 #include int main(void) int digit_count10 = 0; int digit; long n; printf(Enter a number: ); scanf(%ld, &n); while (n 0) digit = n % 10; digit_countdigit+; n /= 10; printf (Digit: ); for (digit = 0; digit = 9; digit+) printf(%

38、3d, digit); printf(nOccurrences:); for (digit = 0; digit = 9; digit+) printf(%3d, digit_countdigit); printf(n); return 0;5. was #6 #include #define NUM_RATES (int) (sizeof(value) / sizeof(value0)#define INITIAL_BALANCE 100.00int main(void) int i, low_rate, month, num_years, year; double value5; prin

39、tf(Enter interest rate: ); scanf(%d, &low_rate); printf(Enter number of years: ); scanf(%d, &num_years); printf(nYears); for (i = 0; i NUM_RATES; i+) printf(%6d%, low_rate + i); valuei = INITIAL_BALANCE; printf(n); for (year = 1; year = num_years; year+) printf(%3d , year); for (i = 0; i NUM_RATES;

40、i+) for (month = 1; month = 12; month+) valuei += (double) (low_rate + i) / 12) / 100.0 * valuei; printf(%7.2f, valuei); printf(n); return 0;8. was #12 #include #define NUM_QUIZZES 5#define NUM_STUDENTS 5int main(void) int gradesNUM_STUDENTSNUM_QUIZZES; int high, low, quiz, student, total; for (stud

41、ent = 0; student NUM_STUDENTS; student+) printf(Enter grades for student %d: , student + 1); for (quiz = 0; quiz NUM_QUIZZES; quiz+) scanf(%d, &gradesstudentquiz); printf(nStudent Total Averagen); for (student = 0; student NUM_STUDENTS; student+) printf(%4d , student + 1); total = 0; for (quiz = 0;

42、quiz NUM_QUIZZES; quiz+) total += gradesstudentquiz; printf(%3d %3dn, total, total / NUM_QUIZZES); printf(nQuiz Average High Lown); for (quiz = 0; quiz NUM_QUIZZES; quiz+) printf(%3d , quiz + 1); total = 0; high = 0; low = 100; for (student = 0; student high) high = gradesstudentquiz; if (gradesstud

43、entquiz = 0 & x = 0 & y = n - 1);4. was #4 int day_of_year(int month, int day, int year) int num_days = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31; int day_count = 0, i; for (i = 1; i 2) day_count+; return day_count + day;Using the expression year % 4 = 0 to test for leap years is not completely

44、 correct. Centuries are special cases: if a year is a multiple of 100, then it must also be a multiple of 400 in order to be a leap year. The correct test is year % 4 = 0 & (year % 100 != 0 | year % 400 = 0)6. was #6; modified int digit(int n, int k) int i; for (i = 1; i k; i+) n /= 10; return n % 1

45、0;8. was #8 (a) and (b) are valid prototypes. (c) is illegal, since it doesnt specify the type of the parameter. (d) incorrectly specifies that f returns an int value in C89; in C99, omitting the return type is illegal. 10. was #10 (a)int largest(int a, int n) int i, max = a0; for (i = 1; i max) max

46、 = ai; return max;(b)int average(int a, int n) int i, avg = 0; for (i = 0; i n; i+) avg += ai; return avg / n;(c)int num_positive(int a, int n) int i, count = 0; for (i = 0; i 0) count+; return count;15. was #12; modified double median(double x, double y, double z) double result; if (x = y) if (y =

47、z) result = y; else if (x = z) result = z; else result = x; else if (z = y) result = y; else if (x = z) result = x; else result = z; return result;17. was #14 int fact(int n) int i, result = 1; for (i = 2; i = n; i+) result *= i; return result;19. was #16 The following program tests the pb function:

48、 #include void pb(int n);int main(void) int n; printf(Enter a number: ); scanf(%d, &n); printf(Output of pb: ); pb(n); printf(n); return 0;void pb(int n) if (n != 0) pb(n / 2); putchar(0 + n % 2); pb prints the binary representation of the argument n, assuming that n is greater than 0. (We also assu

49、me that digits have consecutive codes in the underlying character set.) For example: Enter a number: 53Output of pb: 110101A trace of pbs execution would look like this: pb(53) finds that 53 is not equal to 0, so it calls pb(26), which finds that 26 is not equal to 0, so it calls pb(13), which finds

50、 that 13 is not equal to 0, so it calls pb(6), which finds that 6 is not equal to 0, so it calls pb(3), which finds that 3 is not equal to 0, so it calls pb(1), which finds that 1 is not equal to 0, so it calls pb(0), which finds that 0 is equal to 0, so it returns, causingpb(1) to print 1 and retur

51、n, causingpb(3) to print 1 and return, causingpb(6) to print 0 and return, causingpb(13) to print 1 and return, causingpb(26) to print 0 and return, causing pb(53) to print 1 and return. Chapter 10Answers to Selected Exercises1. was #2 (a) a, b, and c are visible.(b) a, and d are visible.(c) a, d, a

52、nd e are visible.(d) a and f are visible. Answers to Selected Programming Projects3. was #4 #include /* C99 only */#include #include #define NUM_CARDS 5#define RANK 0#define SUIT 1/* external variables */int handNUM_CARDS2;/* 0 1 _ _ 0 |_|_| 1 |_|_| 2 |_|_| 3 |_|_| 4 |_|_| rank suit*/bool straight, flush, four, three;int pairs; /* can be 0, 1, or 2 */* protot

展開(kāi)閱讀全文
溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!