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];
}
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.
The loop reverses the contents of a. After the statement is
executed, a will contain the following data: 10, 9, 8, 7, 6, 5, 4,