Chapter 11
Answers to Selected Exercises
2.[was #2] (e), (f), and (i) are legal. (a) is illegal becausepis a pointer to an integer andiis an integer. (b) is illegal because*pis an integer and&iis a pointer to an integer. (c) is illegal because&pis a pointer to a pointer to an integer andqis a pointer to an integer. (d) is illegal for reasons similar to (c). (g) is illegal becausepis a pointer to an integer and*qis an integer. (h) is illegal because*pis an integer andqis 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];
}
展开阅读全文