knking.com -- Programming Language Books and Training

 C C++ Java

Books by K. N. King

Home
Books by K. N. King
Short courses
Recommended books
Recommended links
FAQ

C Programming: A Modern Approach (Second Edition)

Chapter 9

Answers to Selected Exercises

2. [was #2]

int check(int x, int y, int n)
{
  return (x >= 0 && x <= n - 1 && y >= 0 && y <= n - 1);
}

4. [was #4]

int day_of_year(int month, int day, int year)
{
  int num_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  int day_count = 0, i;

  for (i = 1; i < month; i++)
    day_count += num_days[i-1];

  /* adjust for leap years, assuming they are divisible by 4 */
  if (year % 4 == 0 && month > 2)
    day_count++;

  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 && (year % 100 != 0 || year % 400 == 0)

6. [was #6; modified]

int digit(int n, int k)
{
  int i;

  for (i = 1; i < k; i++)
    n /= 10;

  return n % 10;
}

8. [was #8] (a) and (b) are valid prototypes. (c) is illegal, since it doesn't specify the type of the parameter. (d) incorrectly specifies that f returns an int value in C89; in C99, omitting the return type is illegal.

10. [was #10]

(a)

int largest(int a[], int n)
{
  int i, max = a[0];

  for (i = 1; i < n; i++)
    if (a[i] > max)
      max = a[i];

  return max;
}

(b)

int average(int a[], int n)
{
  int i, avg = 0;

  for (i = 0; i < n; i++)
    avg += a[i];

  return avg / n;
}

(c)

int num_positive(int a[], int n)
{
  int i, count = 0;

  for (i = 0; i < n; i++)
    if (a[i] > 0)
      count++;

  return count;
}

15. [was #12; modified]

double median(double x, double y, double z)
{
  double result;

  if (x <= y)
    if (y <= z) result = y;
    else if (x <= z) result = z;
    else result = x;
  else {
    if (z <= y) result = y;
    else if (x <= z) result = x;
    else result = z;
  }

  return result;
}

17. [was #14]

int fact(int n)
{
  int i, result = 1;

  for (i = 2; i <= n; i++)
    result *= i;

  return result;
}

19. [was #16] The following program tests the pb function:

#include <stdio.h>

void pb(int n);

int main(void)
{
  int n;

  printf("Enter a number: ");
  scanf("%d", &n);

  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: 53
Output of pb: 110101

A trace of pb's 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, causing
            pb(1) to print 1 and return, causing
          pb(3) to print 1 and return, causing
        pb(6) to print 0 and return, causing
      pb(13) to print 1 and return, causing
    pb(26) to print 0 and return, causing
  pb(53) to print 1 and return.


Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved.

Google
 
Web knking.com