-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.c
52 lines (48 loc) · 899 Bytes
/
histogram.c
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#define MAX_RANGE 4096
int main()
{
int wordlengths[MAX_RANGE];
int I, c, longestword;
int letters = 0;
I = c = longestword = 0;
for (I = 0; I <= MAX_RANGE; I++)
{
wordlengths[I] = 0;
}
while ((c = getchar()) != EOF)
{
if ( c !=" "|| c != '\t' || c != '\n' )
letters += 1;
else
{
if ( letters > 0)
++(wordlengths[letters]);
if ( letters > longestword)
longestword = letters;
letters = 0;
}
}
int frequency = 0;
int M = 0;
for ( M = 0; M <= MAX_RANGE; M++ );
{
if ( (wordlengths[M]) >= frequency )
frequency = wordlengths[M];
}
printf("frequency: %d\n", frequency);
printf("longestword: %d\n", longestword);
int v, h;
v = h = 0;
for ( v = frequency; v >= 0; v--)
{
for ( h = 0; h <= MAX_RANGE; h++)
{ if ( wordlengths[h] >= v )
{
printf("#");
}
else printf(" ");
}
printf("\n");
}
}