1
0
mirror of https://github.com/krislamo/knrc.git synced 2024-09-19 21:00:35 +00:00
knrc/12-histogram.c

56 lines
978 B
C
Raw Permalink Normal View History

2021-11-20 05:26:15 +00:00
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXERROR "WARNING: Word length '%d' exceeded 99\n"
int main()
{
2021-11-20 07:40:30 +00:00
int c, i, j, state, count, top;
2021-11-20 05:26:15 +00:00
int wordl[100];
2021-11-20 07:40:30 +00:00
count = state = top = OUT;
2021-11-20 05:26:15 +00:00
for(i = 0; i < 100; ++i)
wordl[i] = 0;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t') {
state = OUT;
if (count > 0 && count < 100)
++wordl[count];
else if (count >= 100)
printf(MAXERROR, count);
count = 0;
} else {
++count;
if (state == OUT)
state = IN;
}
}
if (count > 0 && count < 100)
++wordl[count];
else if (count >= 100)
printf(MAXERROR, count);
2021-11-20 07:40:30 +00:00
for (i = 0; i < 100; ++i)
if (wordl[i] > top)
top = wordl[i];
printf("\n");
for (i = 0; i < 100; ++i)
if (wordl[i] > 0)
printf("%2d ", i);
2021-11-20 05:26:15 +00:00
printf("\n");
2021-11-20 07:40:30 +00:00
for (i = 1; i <= top; ++i) {
for (j = 0; j < 100; ++j) {
if (wordl[j] >= i)
printf(" # ");
else if (wordl[j] > 0)
printf(" ");
2021-11-20 05:26:15 +00:00
}
2021-11-20 07:40:30 +00:00
printf("\n");
2021-11-20 05:26:15 +00:00
}
}