Navigation(slides)

1.6 Arrays

Let is write a program to count the number of occurrences of each digit, of white space characters (blank, tab, newline), and of all other characters. This is artificial, but it permits us to illustrate several aspects of C in one program.

There are twelve categories of input, so it is convenient to use an array to hold the number of occurrences of each digit, rather than ten individual variables. Here is one version of the program:

   1    #include <stdio.h>
   2 
   3    /* count digits, white space, others */
   4    main()
   5    {
   6        int c, i, nwhite, nother;
   7        int ndigit[10];
   8 
   9        nwhite = nother = 0;
  10        for (i = 0; i < 10; ++i)
  11            ndigit[i] = 0;
  12 
  13        while ((c = getchar()) != EOF)
  14            if (c >= '0' && c <= '9')
  15                ++ndigit[c-'0'];
  16            else if (c == ' ' || c == '\n' || c == '\t')
  17                ++nwhite;
  18            else
  19                ++nother;
  20 
  21        printf("digits =");
  22        for (i = 0; i < 10; ++i)
  23            printf(" %d", ndigit[i]);
  24        printf(", white space = %d, other = %d\n",
  25            nwhite, nother);
  26    }

The output of this program on itself is

   digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345

The declaration

   1    int ndigit[10];

declares ndigit to be an array of 10 integers. Array subscripts always start at zero in C, so the elements are ndigit[0], ndigit[1], ..., ndigit[9]. This is reflected in the for loops that initialize and print the array.

A subscript can be any integer expression, which includes integer variables like i, and integer constants.

This particular program relies on the properties of the character representation of the digits. For example, the test

   if (c >= '0' && c <= '9')

determines whether the character in c is a digit. If it is, the numeric value of that digit is

   c - '0'

This works only if '0', '1', ..., '9' have consecutive increasing values. Fortunately, this is true for all character sets.

By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions. This is natural and convenient; for example c-'0' is an integer expression with a value between 0 and 9 corresponding to the character '0' to '9' stored in c, and thus a valid subscript for the array ndigit.

The decision as to whether a character is a digit, white space, or something else is made with the sequence

   1    if (c >= '0' && c <= '9')
   2        ++ndigit[c-'0'];
   3    else if (c == ' ' || c == '\n' || c == '\t')
   4        ++nwhite;
   5    else
   6        ++nother;

The pattern

   1    if (condition1)
   2        statement1
   3    else if (condition2)
   4        statement2
   5        ...
   6        ...
   7    else
   8        statementn

occurs frequently in programs as a way to express a multi-way decision. The conditions are evaluated in order from the top until some condition is satisfied; at that point the corresponding statement part is executed, and the entire construction is finished. (Any statement can be several statements enclosed in braces.) If none of the conditions is satisfied, the statement after the final else is executed if it is present. If the final else and statement are omitted, as in the word count program, no action takes place. There can be any number of

else if(condition)
  statement

groups between the initial if and the final else.

As a matter of style, it is advisable to format this construction as we have shown; if each if were indented past the previous else, a long sequence of decisions would march off the right side of the page.

The switch statement, to be discussed in Chapter 4, provides another way to write a multi-way branch that is particulary suitable when the condition is whether some integer or character expression matches one of a set of constants. For contrast, we will present a switch version of this program in Section 3.4.

Exercise 1-13. Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.

Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.

Navigation(siblings)

ch3n2k.com | Copyright (c) 2004-2020 czk.