




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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) parkinson's law:work expands so as to fill the timeavailable for its completion.3. was #4#include <stdio.h>int main(void) int he
2、ight = 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 #6 here
3、39;s 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);精品. prin
4、tf("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
5、 chance that you'll 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(voi
6、d) 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", amount_with_tax); return 0;the amount_with_tax variable is unnecessary. if we remove it
7、, the program 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
8、. 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 <stdi
9、o.h>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%.2dn", year, month, day); return 0;3. was #6; modified 精品.#include <stdio.h>int main(void) in
10、t prefix, group, publisher, item, 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("publis
11、her 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: %dngroup identifier: %dnpublisher code: %dnitem number: %dncheck digit: %dn", prefix, gro
12、up, 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. on the other hand, the value of -(i/j) is always 1, regardless of the implementation
13、. 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 expressions is i after the increment has been performed. answers to selected programmi
14、ng projects2. was #4 #include <stdio.h>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
15、 (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 projects精品.2. was #6 #include <st
16、dio.h>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",
17、hours, minutes); else if (hours = 12) printf("%d:%.2d pmn", hours, minutes); else printf("%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", &a
18、mp;speed); if (speed < 1) printf("calmn"); else if (speed <= 3) printf("light airn"); else if (speed <= 27) printf("breezen"); else if (speed <= 47) printf("galen"); else if (speed <= 63) printf("stormn");精品. else printf("hurrican
19、en"); return 0;6. was #10 #include <stdio.h>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 digits: ")
20、; 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", &am
21、p;check_digit); first_sum = d + i2 + i4 + j1 + j3 + j5; second_sum = i1 + i3 + i5 + j2 + j4; total = 3 * first_sum + second_sum; if (check_digit = 9 - (total - 1) % 10) printf("validn"); else printf("not validn"); return 0;10. was #14 #include <stdio.h>int main(void) int gr
22、ade; printf("enter numerical grade: ");精品. scanf("%d", &grade); if (grade < 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"); bre
23、ak; 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 #10 (c) is not equivalent to (a) and (b), b
24、ecause 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 for (d = 2; d * d <= n; d+) if (n %
25、 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 statement is
26、now correct: if (n % 2 = 0) printf("n is evenn");answers to selected programming projects2. was #2 #include <stdio.h>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 = rem
27、ainder;精品. printf("greatest common divisor: %dn", m); return 0;4. was #4 #include <stdio.h>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
28、 * value; else if (value < 6250.00f) commission = 56.00f + .0066f * value; else if (value < 20000.00f) commission = 76.00f + .0034f * value; else if (value < 50000.00f) commission = 100.00f + .0022f * value; else if (value < 500000.00f) commission = 155.00f + .0011f * value; else commiss
29、ion = 255.00f + .0009f * value; if (commission < 39.00f) commission = 39.00f; printf("commission: $%.2fnn", commission); printf("enter value of trade: "); scanf("%f", &value); return 0;6. was #6 精品.#include <stdio.h>int main(void) int i, n; printf("ent
30、er 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 <stdio.h>int main(void) int i, n, start_day; printf("enter number of days in month: "); scanf("%d", &n); pr
31、intf("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 (s
32、tart_day + i - 1) % 7 = 0) printf("n"); return 0;chapter 7精品.answers 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
33、* 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 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 valu
34、es are usually stored in 16 bits, causing failure at 182. int and long int values are usually stored in 32 bits, with failure occurring at 46341. 2. was #8 #include <stdio.h>int main(void) int i, n; char ch; printf("this program prints a table of squares.n"); printf("enter numbe
35、r of entries in table: "); scanf("%d", &n); ch = getchar(); /* dispose of new-line character following number 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.&qu
36、ot;); ch = getchar(); /* or simply getchar(); */ 精品. return 0;5. was #10 #include <ctype.h>#include <stdio.h>int main(void) int sum = 0; char ch; printf("enter a word: "); while (ch = getchar() != 'n') switch (toupper(ch) case 'd': case 'g': sum += 2; br
37、eak; 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
38、': case 'z': sum += 10; break; default: sum+; break; printf("scrabble value: %dn", sum); return 0;6. was #12 #include <stdio.h>int main(void)精品. printf("size of int: %dn", (int) sizeof(int); printf("size of short: %dn", (int) sizeof(short); printf(&quo
39、t;size of long: %dn", (int) sizeof(long); printf("size of float: %dn", (int) sizeof(float); printf("size of double: %dn", (int) sizeof(double); printf("size of long double: %dn", (int) sizeof(long double); return 0;since the type of a sizeof expression may vary fro
40、m one implementation to another, it's necessary in c89 to cast sizeof expressions to a known type before printing them. the sizes of the basic types are small numbers, so it's safe to cast them to int. (in general, however, it's best to cast sizeof expressions to unsigned long and print
41、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 can't easily be checked for correctness by someone reading the program. (the reader would have to locate the
42、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 consecutive codes in the underlying character set, which is true of ascii and other popular char
43、acter 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, 1,精品. 1, 1, 1, 1, 0, 1, 1;answers to selected programming projects2. was #2 #include <stdio
44、.h>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("%3d", digit); print
45、f("noccurrences:"); for (digit = 0; digit <= 9; digit+) printf("%3d", digit_countdigit); printf("n"); return 0;5. was #6 #include <stdio.h>#define num_rates (int) (sizeof(value) / sizeof(value0)#define initial_balance 100.00int main(void) int i, low_rate, month
46、, num_years, year; double value5;精品. printf("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 +
47、i); valuei = initial_balance; printf("n"); for (year = 1; year <= num_years; year+) printf("%3d ", year); for (i = 0; i < num_rates; i+) for (month = 1; month <= 12; month+) valuei += (double) (low_rate + i) / 12) / 100.0 * valuei; printf("%7.2f", valuei); prin
48、tf("n"); return 0;8. was #12 #include <stdio.h>#define num_quizzes 5#define num_students 5int main(void) int gradesnum_studentsnum_quizzes; int high, low, quiz, student, total; for (student = 0; student < num_students; student+) printf("enter grades for student %d: ", st
49、udent + 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; quiz < num_quizzes; quiz
50、+) 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 < num_students; student+
51、) total += gradesstudentquiz; if (gradesstudentquiz > high) high = gradesstudentquiz; if (gradesstudentquiz < low) low = gradesstudentquiz; printf("%3d %3d %3dn", total / num_students, high, low); return 0;chapter 9answers to selected exercises2. was #2 int check(int x, int y, int n)
52、 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 = 0, i; for (i = 1; i < month; i+) day_count += num_daysi-1; /* adjust
53、 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 completely correct. centuries are special cases: if a year is a multiple of 100, then it must also be a mu
54、ltiple 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 % 10;8. was #8 (a) and (b) are valid prototypes. (c) is illegal, since it doesn't
55、 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 < n; i+)精品. if (ai > max) max = ai; return max;(b)int average(int a, int n) int
56、 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 < n; i+) if (ai > 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
57、; 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: #include <stdio.h>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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 数字化学习共同体-洞察及研究
- 2025海外员工合同统一范本
- 2025种子销售合同模板
- 2025深圳亚富路合同贷信用贷款无抵押贷款申请条件、手续
- 环保设施项目验收管理流程办法
- 2025年中国菠萝汁啤行业市场发展前景及发展趋势与投资战略研究报告
- 2025-2030年中国塑料编制袋专用涂膜行业深度研究分析报告
- 酒楼厨师岗位职责及菜品创新他
- 特殊教育数学复习计划
- 中班社会交往能力培养计划
- GB/T 29776-2013纺织品防虫蛀性能的测定
- DB32T 4176-2021 公共建筑室内空气质量监测系统技术规程
- 中俄文一般贸易合同范本
- 不合格品退货处理单
- 国家开放大学2022春(202207)《2624医药商品营销实务》期末考试真题及答案-开放专科
- 大连海事大学毕业成绩表
- 尾矿库模施袋筑坝工艺在施工中的应用
- 中国34个省级行政区轮廓图
- 人教版三年级下册数学(全册)同步随堂练习一课一练
- 肺小结节定位和肺段切除规划PPT学习课件
- 精品专题资料(2022-2023年收藏)国家电网公司智能电网知识竞赛题目
评论
0/150
提交评论