Skip to content

Instantly share code, notes, and snippets.

@slayerlab
Created October 21, 2018 10:36
Show Gist options
  • Save slayerlab/854dde428e65bb3e20783c454313c40f to your computer and use it in GitHub Desktop.
Save slayerlab/854dde428e65bb3e20783c454313c40f to your computer and use it in GitHub Desktop.
ANSI C K&R: Horizontal & Vertical Histogram
#include <stdio.h>
/* *
* 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.
* ----
* ANSI C K&R - CHARACTER INPUT AND OUTPUT: PAGE 15
* The quantities IS_LOWER, IS_UPPER, IS_ALPHA, MAXLEN, OUT and IN (macros)
* are symbolic constant, not variables, so they do not appear in declarations.
* Symbolic constant names are conventionally written in upper case so they can
* be readily distinguished from lower case variable name.
* */
#define IS_UPPER(N) ((N) >= 'A' && (N) <= 'Z') /* 'A'==65 && 'Z'==90 */
#define IS_LOWER(N) ((N) >= 'a' && (N) <= 'z') /* 'a'==97 && 'z'==122 */
#define IS_ALPHA(N) (IS_LOWER(N) || IS_UPPER(N)) /* [A-Za-z] x*/
#define MAXLEN 17 /* buffer limit length */
#define OUT 0 /* outside the word */
#define IN 1 /* inside the word */
int main(void)
{
int c = EOF,
/* word buffer & word length */
wbuf[MAXLEN],
wlen = 0;
int state = OUT,
/* X and Y axis */
x = 0, /* horizontal histogram */
y = 0; /* vertical histogram */
/* fill word buffer with zero */
for (int i = 0; i <= MAXLEN; i++)
wbuf[i] = 0;
/* word count while giving data */
while((c = getchar()) != EOF) {
if (IS_ALPHA(c) && wlen < MAXLEN) {
state = IN;
++wlen;
} else if (wlen > 0 && wlen < MAXLEN) {
state = OUT;
++wbuf[wlen];
wlen = 0;
}
}
++wbuf[wlen]; /* return value without EOF */
/* give to 'x' and 'y' the length of longest word */
for (int i = 1; i < MAXLEN; i++) {
if (wbuf[i] && i > x)
x = i;
if (wbuf[i] > y)
y = wbuf[i];
}
/* horizontal histogram */
puts("\nHORIZONTAL HISTOGRAM");
for (int i = 1; i <= x; i++) {
printf("%2d | ", i);
for (int j = 0; j < wbuf[i]; j++)
printf("* ");
putchar('\n');
}
/* *
* vertical histogram
* building the 'y' axis
* */
puts("\nVERTICAL HISTOGRAM");
for (int i = y; i > 0; i--) {
printf("%2d |", i);
for(int j = 1; j <= x; j++) {
if (wbuf[j] < i)
printf(" ");
else
printf(" * ");
}
putchar('\n');
}
printf(" +--");
for (int i = 1; i < x; i++)
printf("------");
putchar('\n');
for (int i = 1; i <= x; i++)
printf("%6d", i);
putchar('\n');
return 0;
}
@slayerlab
Copy link
Author

$ cat histogram.c | ./histogram 

HORIZONTAL HISTOGRAM
 1 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 2 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 3 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 4 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 5 | * * * * * * * * * * * * * * * * * * * * * * * * * * 
 6 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 7 | * * * * * * * * * * * * * 
 8 | * * * * * * * * * * 
 9 | * * * * * * * * * * * 
10 | * * * * 
11 | * * * 
12 | * 
13 | * 
14 | * 

VERTICAL HISTOGRAM
89 | *                                                                                  
88 | *                                                                                  
87 | *                                                                                  
86 | *                                                                                  
85 | *                                                                                  
84 | *                                                                                  
83 | *                                                                                  
82 | *                                                                                  
81 | *                                                                                  
80 | *                                                                                  
79 | *                                                                                  
78 | *                                                                                  
77 | *                                                                                  
76 | *                                                                                  
75 | *                                                                                  
74 | *                                                                                  
73 | *                                                                                  
72 | *                                                                                  
71 | *                                                                                  
70 | *                                                                                  
69 | *                                                                                  
68 | *                                                                                  
67 | *                                                                                  
66 | *                                                                                  
65 | *                                                                                  
64 | *                                                                                  
63 | *                                                                                  
62 | *                                                                                  
61 | *                                                                                  
60 | *                                                                                  
59 | *                                                                                  
58 | *                                                                                  
57 | *                                                                                  
56 | *                                                                                  
55 | *                                                                                  
54 | *                                                                                  
53 | *                                                                                  
52 | *                                                                                  
51 | *                                                                                  
50 | *                 *                                                                
49 | *                 *                                                                
48 | *                 *                                                                
47 | *                 *                                                                
46 | *                 *                                                                
45 | *                 *                                                                
44 | *           *     *                                                                
43 | *           *     *                                                                
42 | *           *     *                                                                
41 | *           *     *                                                                
40 | *           *     *                                                                
39 | *           *     *                                                                
38 | *           *     *                                                                
37 | *           *     *                                                                
36 | *           *     *                                                                
35 | *           *     *                                                                
34 | *     *     *     *           *                                                    
33 | *     *     *     *           *                                                    
32 | *     *     *     *           *                                                    
31 | *     *     *     *           *                                                    
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
$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment