Skip to content

Commit 88497ea

Browse files
committed
revised answer
Improved answer and moved vertical histogram code to a separate file, 1-13a.c.
1 parent 8c43bb7 commit 88497ea

File tree

1 file changed

+33
-47
lines changed

1 file changed

+33
-47
lines changed

chapter01/1-13.c

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,61 @@
22
* Exercise 1-13. Write a program to print a histogram of the lengths of words
33
* in its input. It is easy to draw the histogram with the bars horizontal; a
44
* vertical orientation is more challenging.
5+
*
56
* By Faisal Saadatmand
67
*/
78

9+
/* Horizontal Histogram. See 1-13a.c for a vertical histogram implementation */
10+
811
#include <stdio.h>
912

13+
#define SIZE 5 /* size of lengths array */
14+
#define SCALE 1 /* adjust to accommodate large input */
1015
#define OUT 1 /* outside of a word */
1116
#define IN 0 /* inside of a word */
1217

1318
int main(void)
1419
{
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 */
1722

18-
for (i = 0; i <= 4; ++i)
23+
for (i = 0; i <= SIZE; ++i)
1924
lengths[i] = 0;
2025

2126
state = OUT;
22-
27+
count = 0;
2328
while ((c = getchar()) != EOF) {
2429

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
4533
state = IN;
46-
}
4734

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+
}
5252

5353
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(" *");
5758
printf("\n");
5859
}
5960

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-
}
7561
return 0;
7662
}

0 commit comments

Comments
 (0)