c语言程序设计:现代方法第二版习题答案_第1页
c语言程序设计:现代方法第二版习题答案_第2页
c语言程序设计:现代方法第二版习题答案_第3页
c语言程序设计:现代方法第二版习题答案_第4页
c语言程序设计:现代方法第二版习题答案_第5页
已阅读5页,还剩79页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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, 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;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: 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 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 Projects4. was #8; modified#include int main(void)float original_amount, amount_with_tax;printf(“Enter an amount: “);scanf(“%f“, 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 program is slightly shorter:#include int main(void)float original_amount;printf(“Enter an amount: “);scanf(“%f“, printf(“With tax added: $%.2fn“, original_amount * 1.05f);return 0;Chapter 3Answers 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, day, year;printf(“Enter a date (mm/dd/yyyy): “);scanf(“%d/%d/%d“, 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“, 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: %dnGroup 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. 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 expressions 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“, 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) 1 4. was #4 (i j) - (i int main(void)int hours, minutes;printf(“Enter a 24-hour time: “);scanf(“%d:%d“, printf(“Equivalent 12-hour time: “);if (hours = 0)printf(“12:%.2d AMn“, minutes);else if (hours int main(void)int speed;printf(“Enter a wind speed in knots: “);scanf(“%d“, if (speed 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“, printf(“Enter first group of five digits: “);scanf(“%1d%1d%1d%1d%1d“, printf(“Enter second group of five digits: “);scanf(“%1d%1d%1d%1d%1d“, printf(“Enter the last (single) digit: “);scanf(“%1d“, 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“);elseprintf(“NOT VALIDn“);return 0;10. was #14 #include int main(void)int grade;printf(“Enter numerical grade: “);scanf(“%d“, if (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;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), 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 for (d = 2; d * d int main(void)int m, n, remainder;printf(“Enter two integers: “);scanf(“%d%d“, while (n != 0) remainder = m % n;m = n;n = remainder;printf(“Greatest common divisor: %dn“, m);return 0;4. was #4 #include int main(void)float commission, value;printf(“Enter value of trade: “);scanf(“%f“, while (value != 0.0f) if (value int main(void)int i, n;printf(“Enter limit on maximum square: “);scanf(“%d“, for (i = 2; i * i int main(void)int i, n, start_day;printf(“Enter number of days in month: “);scanf(“%d“, printf(“Enter starting day of the week (1=Sun, 7=Sat): “);scanf(“%d“, /* print any leading “blank dates“ */for (i = 1; i 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“, ch = getchar();/* dispose of new-line character following number of entries */* could simply be getchar(); */for (i = 1; i #include 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; 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(“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(double);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 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 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 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 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, 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“, while (n 0) digit = n % 10;digit_countdigit+;n /= 10;printf (“Digit: “);for (digit = 0; digit #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;printf(“Enter interest rate: “);scanf(“%d“, printf(“Enter number of years: “);scanf(“%d“, printf(“nYears“);for (i = 0; i #define NUM_QUIZZES 5#define NUM_STUDENTS 5int main(void)int gradesNUM_STUDENTSNUM_QUIZZES;int high, low, quiz, student, total;for (student = 0; student high)high = gradesstudentquiz;if (gradesstudentquiz = 0 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 multiple of 400 in order to be a leap year. The correct test is year % 4 = 0 modified int digit(int n, int k)int i;for (i = 1; i max)max = ai;return max;(b)int average(int a, int n)int i, avg = 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 void pb(int n);int main(void)int n;printf(“Enter a number: “);scanf(“%d“, 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 assume 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 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 return, 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, and 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 */* prototypes */void read_cards(void);void analyze_hand(void);void print_result(void);/* main: Calls read_cards, analyze_hand, and print_result * repeatedly. */int main(void)for (;) read_cards();analyze_hand();print_result();/* read_cards: Reads the card

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论