Chapter 01 Exercise 1-13

Printer-friendly version

This is just a basic histogram program. The maximum frequency is 30 just to keep it simple (I mean, jeeze, how many words are longer than 30 characters?). I know this is pretty elementary but after 16 years of not thinking algorithmically, it did stretch my brain a bit.

  1. /*
  2.  * ex01_13.c
  3.  *
  4.  *  Created on: Mar 29, 2010
  5.  *      Author: e0410991
  6.  */
  7.  
  8. /*
  9.  * Exercise 1-13. Write a program to print a histogram of the length of words
  10.  * in its input.  It is easy to draw the histogram with the bars horizontal;
  11.  * a vertical orientation is more challenging.
  12.  */
  13.  
  14. #include <stdio.h>
  15.  
  16. #define IN      1               /* inside a word        */
  17. #define OUT     0               /* outside a word       */
  18. #define MAX     30      /* max word length      */
  19.  
  20. main() {
  21.  
  22.         int c, state, len, i, j, cursor;
  23.         int count[MAX];
  24.         char output[(MAX * 3) + 4];
  25.  
  26.         /* initialize count[] */
  27.         for(i=0; i < MAX; i++) count[i]=0;
  28.  
  29.         state = OUT;
  30.         len = 0;
  31.         /* Process STDIN */
  32.         while ((c = getchar()) != EOF) {
  33.                 if (c == ' ' || c == '\t' || c == '\n') {
  34.                         /* outside a word */
  35.                         state = OUT;
  36.                         if (len > 0) {
  37.                                 /* just left a word */
  38.                                 if (len > MAX) /* max word length */
  39.                                         len = MAX;
  40.                                 count[len - 1] = count[len - 1]++;
  41.                                 len = 0;
  42.                         }
  43.                 } else if (state == OUT) {
  44.                         /* back inside a word now */
  45.                         state = IN;
  46.                 }
  47.                 if (state == IN)
  48.                         len++;
  49.         }
  50.  
  51.         /* build up output lines and print */
  52.         for(i = MAX; i > 0; i--) {
  53.                 sprintf(output, "%2d| ", i);
  54.                 cursor = 4;
  55.                 for(j = 0; j < MAX; j++) {
  56.                         output[cursor] = ' ';
  57.                         output[cursor + 1] = ((count[j] >= i) ? '*' : ' ');
  58.                         output[cursor + 2] = ' ';
  59.                         cursor += 3;
  60.                 }
  61.                 printf("%s\n", output);
  62.         }
  63.         printf("  + ");
  64.         for(i = 0; i < MAX; i++) {
  65.                 printf("%2d ", (i + 1) );
  66.         }
  67.         printf("\n");
  68. }
... the input / output look something like this ...
$ cat test.txt && cat test.txt |./ex01_13
It's an error message about some code that you failed to show us. In
general, you can't expect us to know what the problem is unless you
show us the actual code as well as the error message.
30|
29|
28|
27|
26|
25|
24|
23|
22|
21|
20|
19|
18|
17|
16|
15|
14|
13|
12|
11|
10|           *
 9|     *     *
 8|     *     *
 7|     *  *  *
 6|     *  *  *
 5|     *  *  *
 4|     *  *  *  *  *
 3|     *  *  *  *  *
 2|     *  *  *  *  *  *  *
 1|     *  *  *  *  *  *  *
  +  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30