book-exercises-exam2 - Answers to Book Exercises Chapters...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Answers to Book Exercises – Chapters 11-18, and 22 and 26 Here are the answers to exercises from the second edition of C Programming: A Modern Approach . These answers do not necessarily conform to the coding standards for this course. Note: Some of these exercises were originally the even-numbered exercises in the first edition . For the benefit of those who have the first edition, the original exercise number is given in square brackets. For example, the notation [was #4] indicates that the number of the exercise was 4 in the first edition. If an answer is different because of second-edition changes, the word "modified" will appear inside the brackets: [was #4; modified]. Answers to Exercises (Chapter 11) 1. (a) and (g) are aliases. ( Note: (h) is not an alias because it is illegal to apply the indirection operator to a non-pointer.) 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. 3. Several * operators are missing from the body of the function. Here is the corrected version: void avg_sum(double a[], int n, double *avg, double *sum) { int i; *sum = 0.0; for (i = 0; i < n; i++) *sum += a[i]; *avg = *sum / n; } 4. [was #4; modified] void swap(int *p, int *q) { int temp; temp = *p; *p = *q; *q = temp; } 5. void split_time(long total_sec, int *hr, int *min, int *sec) { *hr = total_sec / 3600; *min = total_sec % 3600 / 60; *sec = total_sec % 3600 % 60; } 6. [was #6] void find_two_largest(int a[], int n, int *largest,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
{ 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]; } 7. void split_date(int day_of_year, int year, int *month, int *day) { int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; /* leap year adjustment */ if ((!(year % 4) && (year % 100)) || !(year % 400)) days[2]++; *month = 1; while (day_of_year > days[*month]) day_of_year -= days[(*month)++]; *day = day_of_year; } 8. int *find_largest(int a[], int n) { int i, *largest; largest = &a[0]; for (i = 1; i < n; i++) if (a[i] > *largest) largest = &a[i]; return largest; } Answers to Exercises (Chapter 12) 1. (a) 14(b) 34(c) 4(d) true(e) false 2. [was #2] The statement is illegal because pointers cannot be added. Here's a legal statement that has the desired effect: middle = low + (high - low) / 2; The value of (high - low) / 2 is an integer, not a pointer, so it can legally be added to low. 3.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

book-exercises-exam2 - Answers to Book Exercises Chapters...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online