From 04ac11bea19dfcb3886e865a2fb0077e8c01a4dc Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Sat, 20 Nov 2021 00:26:15 -0500 Subject: [PATCH] Exercise 1-13. Horizontal histogram --- 12-histogram.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 12-histogram.c diff --git a/12-histogram.c b/12-histogram.c new file mode 100644 index 0000000..6e36296 --- /dev/null +++ b/12-histogram.c @@ -0,0 +1,46 @@ +#include + +#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"); + } + } +}