第一篇:c語言程序設(shè)計現(xiàn)代方法(第二版)習(xí)題答案
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)Parkinson's Law: Work expands so as to fill the time available 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] Here's 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: 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 factors, so the 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 Projects 4.[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”, 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 Answers to Selected 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
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
int main(void){ int 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(“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 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, 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 2.[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 5 Answers to Selected Exercises 2.[was #2](a)1(b)1(c)1(d)1 4.[was #4](i > j)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)printf(“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 digits: ”);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;total = 3 * first_sum + second_sum;
if(check_digit == 91)% 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 < 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”);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 6 Answers to Selected Exercises 4.[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 <= 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 statement is now correct: if(n % 2 == 0)printf(“n is evenn”);Answers to Selected Programming Projects 2.[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);
return 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.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 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: ”);scanf(“%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 + i1 && y >= 0 && y <= npass;card++){ rank = hand[card][RANK];suit = hand[card][SUIT];if(hand[card+1][RANK] < rank){ hand[card][RANK] = hand[card+1][RANK];hand[card][SUIT] = hand[card+1][SUIT];hand[card+1][RANK] = rank;hand[card+1][SUIT] = suit;} }
/* check for flush */ suit = hand[0][SUIT];for(card = 1;card < NUM_CARDS;card++)if(hand[card][SUIT]!= suit)flush = false;
/* check for straight */ for(card = 0;card < NUM_CARDS1 && num_in_rank[0] > 0 && num_in_rank[NUM_RANKS-1] > 0){ straight = true;return;}
/* check for 4-of-a-kind, 3-of-a-kind, and pairs */ for(rank = 0;rank < NUM_RANKS;rank++){ if(num_in_rank[rank] == 4)four = true;if(num_in_rank[rank] == 3)three = true;if(num_in_rank[rank] == 2)pairs++;} }
/********************************************************** * print_result: Prints the classification of the hand, * * based on the values of the external * * variables straight, flush, four, three, * * and pairs.* **********************************************************/ void print_result(void){ if(straight && flush)printf(“Straight flush”);else if(four)printf(“Four of a kind”);else if(three && pairs == 1)printf(“Full house”);else if(flush)printf(“Flush”);else if(straight)printf(“Straight”);else if(three)printf(“Three of a kind”);else if(pairs == 2)printf(“Two pairs”);else if(pairs == 1)printf(“Pair”);else printf(“High card”);printf(“nn”);}
Chapter 11 Answers to Selected Exercises 2.[was #2](e),(f), and(i)are legal.(a)is illegal because p is a pointer to an integer and i is an integer.(b)is illegal because *p is an integer and &i is a pointer to an integer.(c)is illegal because &p is a pointer to a pointer to an integer and q is a pointer to an integer.(d)is illegal for reasons similar to(c).(g)is illegal because p is a pointer to an integer and *q is an integer.(h)is illegal because *p is an integer and q is a pointer to an integer.4.[was #4;modified] void swap(int *p, int *q){ int temp;
temp = *p;*p = *q;*q = temp;} 6.[was #6] void find_two_largest(int a[], int n, int *largest, int *second_largest){ int i;
if(a[0] > a[1]){ *largest = a[0];*second_largest = a[1];} else { *largest = a[1];*second_largest = a[0];}
for(i = 2;i < n;i++)if(a[i] > *largest){ *second_largest = *largest;*largest = a[i];} else if(a[i] > *second_largest)*second_largest = a[i];}
Chapter 12 Answers to Selected Exercises 2.[was #2] The statement is illegal because pointers cannot be added.Here's a legal statement that has the desired effect: middle = low +(highlow)/ 2 is an integer, not a pointer, so it can legally be added to low.4.[was #6] int *top_ptr;
void make_empty(void){ top_ptr = &contents[0];}
bool is_empty(void){ return top_ptr == &contents[0];}
bool is_full(void){ return top_ptr == &contents[STACK_SIZE];} 6.[was #10;modified] int sum_array(const int a[], int n){ int *p, sum;
sum = 0;for(p = a;p < a + n;p++)sum += *p;return sum;} 13.[was #12;modified] #define N 10
double ident[N][N], *p;int num_zeros = N;
for(p = &ident[0][0];p <= &ident[N-1][N-1];p++)if(num_zeros == N){ *p = 1.0;num_zeros = 0;} else { *p = 0.0;num_zeros++;} 15.[was #14] int *p;
for(p = temperatures[i];p < temperatures[i] + 24;p++)printf(“%d ”, *p);Answers to Selected Programming Projects 1.[was #4](a)#include
#define MSG_LEN 80 /* maximum length of message */
int main(void){ char msg[MSG_LEN];int i;
printf(“Enter a message: ”);for(i = 0;i < MSG_LEN;i++){ msg[i] = getchar();if(msg[i] == 'n')break;}
printf(“Reversal is: ”);for(i--;i >= 0;i--)putchar(msg[i]);putchar('n');
return 0;}(b)#include
#define MSG_LEN 80 /* maximum length of message */
int main(void){ char msg[MSG_LEN], *p;
printf(“Enter a message: ”);for(p = &msg[0];p < &msg[MSG_LEN];p++){ *p = getchar();if(*p == 'n')break;}
printf(“Reversal is: ”);for(p--;p >= &msg[0];p--)putchar(*p);putchar('n');
return 0;} 3.[was #8] #include
#define MSG_LEN 80 /* maximum length of message */
int main(void){ char msg[MSG_LEN], *p;
printf(“Enter a message: ”);for(p = msg;p < msg + MSG_LEN;p++){ *p = getchar();if(*p == 'n')break;}
printf(“Reversal is: ”);for(p--;p >= msg;p--)putchar(*p);putchar('n');
return 0;}
Chapter 13 Answers to Selected Exercises 2.[was #2](a)Illegal;p is not a character.(b)Legal;output is a.(c)Legal;output is abc.(d)Illegal;*p is not a pointer.4.[was #4](a)int read_line(char str[], int n){ int ch, i = 0;
while((ch = getchar())!= 'n')if(i == 0 && isspace(ch));/* ignore */ else if(i < n)str[i++] = ch;str[i] = '