C語言程序設(shè)計 現(xiàn)代方法 第二版 習(xí)題答案 C Programming_ A Modern Approach
《C語言程序設(shè)計 現(xiàn)代方法 第二版 習(xí)題答案 C Programming_ A Modern Approach》由會員分享,可在線閱讀,更多相關(guān)《C語言程序設(shè)計 現(xiàn)代方法 第二版 習(xí)題答案 C Programming_ A Modern Approach(84頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、
Chapter 2
Answers to Selected Exercises
2. [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 time
available for its completion.
3. [was #4]
#include
2、n(void) { int height = 8, length = 12, width = 10, volume; volume = height * length * width; printf("Dimensions: %dx%dx%d\n", length, width, height); printf("Volume (cubic inches): %d\n", volume); printf("Dimensional weight (pounds): %d\n", (volume + 165) / 166); return 0;
3、}
4. [was #6] Heres one possible program:
#include
4、f("Value of z: %g\n", z); return 0; } When compiled using GCC and then executed, this program produced the following output: Value of i: 5618848 Value of j: 0 Value of k: 6844404 Value of x: 3.98979e-34 Value of y: 9.59105e-39 Value of z: 9.59105e-39 The values printed depend on many f
5、actors, 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: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;.
Answers to Selected Programming Projects
4. [was #8; modified]
#include
6、tdio.h> 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: $%.2f\n", amount_with_tax); return 0; } The amount_with_tax variable is unnecess
7、ary. If we remove it, the program is slightly shorter:
#include
8、cted Exercises
2. [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 Projects
1. [was #4; modified]
#include
9、
int main(void)
{
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%.2d\n", year, month, day);
return 0;
}
3. [was #6; modified]
#include
10、oup, publisher, item, check_digit; printf("Enter ISBN: "); scanf("%d-%d-%d-%d-%d", &prefix, &group, &publisher, &item, &check_digit); printf("GS1 prefix: %d\n", prefix); printf("Group identifier: %d\n", group); printf("Publisher code: %d\n", publisher); printf("Item number: %d\
11、n", item); printf("Check digit: %d\n", check_digit); /* The five printf calls can be combined as follows: printf("GS1 prefix: %d\nGroup identifier: %d\nPublisher code: %d\nItem number: %d\nCheck digit: %d\n", prefix, group, publisher, item, check_digit); */ retur
12、n 0; } Chapter 4 Answers to Selected Exercises 2. [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
13、, 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 expressions is i after the increment has been performed. Answers to Selected Programming Projects
14、
2. [was #4]
#include
15、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 onetwo since there are no break statements after the cases. Answers to Selected Programming Projects 2. [was #6
16、]
#include
17、 minutes);
else if (hours == 12)
printf("%d:%.2d PM\n", hours, minutes);
else
printf("%d:%.2d PM\n", hours - 12, minutes);
return 0;
}
4. [was #8; modified]
#include
18、eed); if (speed < 1) printf("Calm\n"); else if (speed <= 3) printf("Light air\n"); else if (speed <= 27) printf("Breeze\n"); else if (speed <= 47) printf("Gale\n"); else if (speed <= 63) printf("Storm\n"); else printf("Hurricane\n"); return 0;
19、}
6. [was #10]
#include
20、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;
21、total = 3 * first_sum + second_sum;
if (check_digit == 9 - ((total - 1) % 10))
printf("VALID\n");
else
printf("NOT VALID\n");
return 0;
}
10. [was #14]
#include
22、 if (grade < 0 || grade > 100) { printf("Illegal grade\n"); return 0; } switch (grade / 10) { case 10: case 9: printf("Letter grade: A\n"); break; case 8: printf("Letter grade: B\n"); break; case 7: printf("Letter grade: C\n");
23、 break; case 6: printf("Letter grade: D\n"); break; case 5: case 4: case 3: case 2: case 1: case 0: printf("Letter grade: F\n"); break; } return 0; } Chapter 6 Answers to Selected Exercises 4. [was #10] (c) is
24、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_e
25、nd: ; /* null statement */ } 12. [was #14] 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 %d\n", n, d); else printf("%d is prime\n", n); 14. [was #16] The
26、problem is the semicolon at the end of the first line. If we remove it, the statement is now correct:
if (n % 2 == 0)
printf("n is even\n");
Answers to Selected Programming Projects
2. [was #2]
#include
27、");
scanf("%d%d", &m, &n);
while (n != 0) {
remainder = m % n;
m = n;
n = remainder;
}
printf("Greatest common divisor: %d\n", m);
return 0;
}
4. [was #4]
#include
28、; 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.00f + .0034f * value; else if (value
29、 < 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: $%.2f\n\n", commi
30、ssion);
printf("Enter value of trade: ");
scanf("%f", &value);
}
return 0;
}
6. [was #6]
#include
31、
return 0;
}
8. [was #8]
#include
32、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 7 Answers to Selected Exercises 3. [was #4] (b) is not legal. 4
33、. [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, then the result is converted to double and stored
34、 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 Projects 1. [was #2] short int values are usually stored in 16 bits, causing failure at 182. int and long int values are usually stored in 32 bi
35、ts, with failure occurring at 46341.
2. [was #8]
#include
36、r following number of entries */ /* could simply be getchar(); */ for (i = 1; i <= n; i++) { printf("%10d%10d\n", i, i * i); if (i % 24 == 0) { printf("Press Enter to continue..."); ch = getchar(); /* or simply getchar(); */ } } return 0; } 5. [was
37、#10]
#include
38、ak; 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("Scrabble value: %d\n", s
39、um);
return 0;
}
6. [was #12]
#include
40、tf("Size of double: %d\n", (int) sizeof(double)); printf("Size of long double: %d\n", (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 t
41、hem. The sizes of the basic types are small numbers, so its 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 8 Answers to Selected Exerc
42、ises 1. [was #4] The problem with sizeof(a) / sizeof(t) is that it cant 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 subsc
43、ript into the array a, we would write a[d-0]. This assumes that digits have consecutive codes in the underlying character set, which is true of ASCII and other popular character sets. 7. [was #10] const int segments[10][7] = {{1, 1, 1, 1, 1, 1}, {0, 1, 1},
44、 {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},
45、 {1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1}};
Answers to Selected Programming Projects
2. [was #2]
#include
46、ld", &n); while (n > 0) { digit = n % 10; digit_count[digit]++; n /= 10; } printf ("Digit: "); for (digit = 0; digit <= 9; digit++) printf("%3d", digit); printf("\nOccurrences:"); for (digit = 0; digit <= 9; digit++) printf("%3d", digit_count[digit
47、]);
printf("\n");
return 0;
}
5. [was #6]
#include
48、("%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); value[i] = INITIAL_BALANCE; } printf("\n"); for (year = 1; year <= num_years; year++) { printf("%
49、3d ", year);
for (i = 0; i < NUM_RATES; i++) {
for (month = 1; month <= 12; month++)
value[i] += ((double) (low_rate + i) / 12) / 100.0 * value[i];
printf("%7.2f", value[i]);
}
printf("\n");
}
return 0;
}
8. [was #12]
#include
50、 NUM_QUIZZES 5 #define NUM_STUDENTS 5 int main(void) { int grades[NUM_STUDENTS][NUM_QUIZZES]; int high, low, quiz, student, total; for (student = 0; student < NUM_STUDENTS; student++) { printf("Enter grades for student %d: ", student + 1); for (quiz = 0; quiz < NUM_QUIZZES
51、; quiz++) scanf("%d", &grades[student][quiz]); } printf("\nStudent Total Average\n"); for (student = 0; student < NUM_STUDENTS; student++) { printf("%4d ", student + 1); total = 0; for (quiz = 0; quiz < NUM_QUIZZES; quiz++) total += grades[student][quiz
52、]; printf("%3d %3d\n", total, total / NUM_QUIZZES); } printf("\nQuiz Average High Low\n"); for (quiz = 0; quiz < NUM_QUIZZES; quiz++) { printf("%3d ", quiz + 1); total = 0; high = 0; low = 100; for (student = 0; student < NUM_STUDENTS; student++) {
53、 total += grades[student][quiz]; if (grades[student][quiz] > high) high = grades[student][quiz]; if (grades[student][quiz] < low) low = grades[student][quiz]; } printf("%3d %3d %3d\n", total / NUM_STUDENTS, high, low); } return 0; } C
54、hapter 9 Answers to Selected Exercises 2. [was #2] int check(int x, int y, int n) { return (x >= 0 && x <= n - 1 && y >= 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
55、 = 0, i; for (i = 1; i < month; i++) day_count += num_days[i-1]; /* adjust for leap years, assuming they are divisible by 4 */ if (year % 4 == 0 && month > 2) day_count++; return day_count + day; } Using the expression year % 4 == 0 to test for leap years is not complet
56、ely 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 <
57、k; i++) n /= 10; return n % 10; } 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
58、(int a[], int n) { int i, max = a[0]; for (i = 1; i < n; i++) if (a[i] > max) max = a[i]; return max; } (b) int average(int a[], int n) { int i, avg = 0; for (i = 0; i < n; i++) avg += a[i]; return avg / n; } (c) int num_positive(int a[], int n) {
59、 int i, count = 0; for (i = 0; i < n; i++) if (a[i] > 0) count++; return count; } 15. [was #12; modified] double median(double x, double y, double z) { double result; if (x <= y) if (y <= z) result = y; else if (x <= z) result = z; else result = x;
60、 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
61、pb function:
#include
62、 pb prints the binary representation of the argument n, assuming that n is greater than 0. (We also assume that digits have consecutive codes in the underlying character set.) For example: Enter a number: 53 Output of pb: 110101 A trace of pbs execution would look like this: pb(53) finds that
63、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 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
64、1 is not equal to 0, so it calls pb(0), which finds that 0 is equal to 0, so it returns, causing pb(1) to print 1 and return, causing pb(3) to print 1 and return, causing pb(6) to print 0 and return, causing pb(13) to print 1 and return, causing pb(26) to print 0 and return, causing pb(53)
65、to print 1 and return.
Chapter 10
Answers to Selected Exercises
1. [was #2] (a) a, b, and c are visible.
(b) a, and d are visible.
(c) a, d, and e are visible.
(d) a and f are visible.
Answers to Selected Programming Projects
3. [was #4]
#include
66、
- 溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2020高考化學(xué)熱門專題:原理綜合透題型析課件
- 現(xiàn)代中國的教育說課稿課件
- 蒸餾和熔點沸點的測定和溫度計的校正
- 臨時起搏器的護(hù)理
- 恒成實業(yè)網(wǎng)絡(luò)推廣方案
- 勿為小惡優(yōu)秀課件-粵教版
- 人教版初中地理七年級上冊人口與人種課件7
- 誡子書課件文檔
- 軟件測試計劃書與測試用例編寫課件
- 人教版五年級數(shù)學(xué)上冊課件3小數(shù)除法第2課時除數(shù)是整數(shù)的小數(shù)除法課件
- 太白酒2002年全國推廣營銷企劃案
- 滬教版小學(xué)語文三年級上冊《小狗杜克》課件1
- 我們的情感世界課件7-人教版
- 擔(dān)保產(chǎn)品案例講解及其風(fēng)險控制設(shè)計(含法律相關(guān)規(guī)范)
- 【部編版】四年級語文上冊《2.走月亮》ppt課件