|
2 | 2 | * Exercise 1-13. Write a program to print a histogram of the lengths of words
|
3 | 3 | * in its input. It is easy to draw the histogram with the bars horizontal; a
|
4 | 4 | * vertical orientation is more challenging.
|
| 5 | + * |
5 | 6 | * By Faisal Saadatmand
|
6 | 7 | */
|
7 | 8 |
|
| 9 | +/* Horizontal Histogram. See 1-13a.c for a vertical histogram implementation */ |
| 10 | + |
8 | 11 | #include <stdio.h>
|
9 | 12 |
|
| 13 | +#define SIZE 5 /* size of lengths array */ |
| 14 | +#define SCALE 1 /* adjust to accommodate large input */ |
10 | 15 | #define OUT 1 /* outside of a word */
|
11 | 16 | #define IN 0 /* inside of a word */
|
12 | 17 |
|
13 | 18 | int main(void)
|
14 | 19 | {
|
15 |
| - int c, i, j, nChar = 0, state, longestBar; |
16 |
| - int lengths[10]; |
| 20 | + int c, i, j, count, state; |
| 21 | + int lengths[SIZE]; /* words length ranges */ |
17 | 22 |
|
18 |
| - for (i = 0; i <= 4; ++i) |
| 23 | + for (i = 0; i <= SIZE; ++i) |
19 | 24 | lengths[i] = 0;
|
20 | 25 |
|
21 | 26 | state = OUT;
|
22 |
| - |
| 27 | + count = 0; |
23 | 28 | while ((c = getchar()) != EOF) {
|
24 | 29 |
|
25 |
| - if (state == IN) |
26 |
| - ++nChar; |
27 |
| - |
28 |
| - if (c == ' ' || c == '\t' || c =='\n') { |
29 |
| - if (state == IN) { |
30 |
| - if (nChar < 4) |
31 |
| - ++lengths[0]; |
32 |
| - else if (nChar >= 4 && nChar < 8) |
33 |
| - ++lengths[1]; |
34 |
| - else if (nChar >= 8 && nChar < 12) |
35 |
| - ++lengths[2]; |
36 |
| - else if (nChar >= 12 && nChar < 14) |
37 |
| - ++lengths[3]; |
38 |
| - if (nChar >= 14) |
39 |
| - ++lengths[4]; |
40 |
| - |
41 |
| - nChar = 0; |
42 |
| - } |
43 |
| - state = OUT; |
44 |
| - } else if (state == OUT) |
| 30 | + if (c == ' ' || c == '\t' || c == '\n') |
| 31 | + state = OUT; |
| 32 | + else |
45 | 33 | state = IN;
|
46 |
| - } |
47 | 34 |
|
48 |
| - /* print the value of each bar */ |
49 |
| - for (i = 0; i <= 4; ++i) |
50 |
| - printf("%i ", lengths[i]); |
51 |
| - printf("\n"); |
| 35 | + if (state == IN) |
| 36 | + ++count; |
| 37 | + |
| 38 | + if (state == OUT) { |
| 39 | + if (count < 4) |
| 40 | + ++lengths[0]; |
| 41 | + else if (count >= 4 && count < 8) |
| 42 | + ++lengths[1]; |
| 43 | + else if (count >= 8 && count < 12) |
| 44 | + ++lengths[2]; |
| 45 | + else if (count >= 12 && count < 14) |
| 46 | + ++lengths[3]; |
| 47 | + if (count >= 14) |
| 48 | + ++lengths[4]; |
| 49 | + count = 0; |
| 50 | + } |
| 51 | + } |
52 | 52 |
|
53 | 53 | printf("\nHorizontal Histogram\n");
|
54 |
| - for (i = 0; i <= 4; ++i) { |
55 |
| - for (j = 1; j <= lengths[i]; ++j) |
56 |
| - printf("* "); |
| 54 | + for (i = 0; i < SIZE; ++i) { |
| 55 | + printf(" %i\t", lengths[i]); |
| 56 | + for (j = 0; j < lengths[i] / SCALE; ++j) |
| 57 | + printf(" *"); |
57 | 58 | printf("\n");
|
58 | 59 | }
|
59 | 60 |
|
60 |
| - printf("\nVertical Histogram:\n"); |
61 |
| - longestBar = lengths[0]; |
62 |
| - for (i = 0; i <= 4; ++i) /* find the longestBar */ |
63 |
| - if (longestBar < lengths[i] ) |
64 |
| - longestBar = lengths[i]; |
65 |
| - |
66 |
| - while (longestBar != 0) { |
67 |
| - for (i = 0; i <= 4; ++i) |
68 |
| - if (lengths[i] - longestBar < 0) |
69 |
| - printf(" "); |
70 |
| - else |
71 |
| - printf("* "); |
72 |
| - --longestBar; |
73 |
| - printf("\n"); |
74 |
| - } |
75 | 61 | return 0;
|
76 | 62 | }
|
0 commit comments