1
0
mirror of https://github.com/krislamo/knrc.git synced 2024-11-10 00:30:35 +00:00

Exercise 1-13. Horizontal histogram

This commit is contained in:
Kris Lamoureux 2021-11-20 00:26:15 -05:00
parent 35dae8afaf
commit 04ac11bea1
Signed by: kris
GPG Key ID: 3EDA9C3441EDA925

46
12-histogram.c Normal file
View File

@ -0,0 +1,46 @@
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXERROR "WARNING: Word length '%d' exceeded 99\n"
int main()
{
int c, i, j, state, count;
int wordl[100];
count = state = OUT;
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);
printf("\n");
for (i = 0; i < 100; ++i) {
if (wordl[i] > 0) {
printf("%2d | ", i);
for (j = 0; j < wordl[i]; ++j)
printf("#");
printf("\n");
}
}
}