From f232d42ece1e39c8364ce5c795d1e21655c94262 Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Sat, 20 Nov 2021 02:40:30 -0500 Subject: [PATCH] Exercise 1-13. Vertical histogram --- 12-histogram.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/12-histogram.c b/12-histogram.c index 6e36296..5497c17 100644 --- a/12-histogram.c +++ b/12-histogram.c @@ -6,11 +6,10 @@ int main() { - - int c, i, j, state, count; + int c, i, j, state, count, top; int wordl[100]; - count = state = OUT; + count = state = top = OUT; for(i = 0; i < 100; ++i) wordl[i] = 0; @@ -34,13 +33,23 @@ int main() else if (count >= 100) printf(MAXERROR, count); + 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); - for (j = 0; j < wordl[i]; ++j) - printf("#"); - printf("\n"); + for (i = 0; i < 100; ++i) + if (wordl[i] > 0) + printf("%2d ", i); + + printf("\n"); + for (i = 1; i <= top; ++i) { + for (j = 0; j < 100; ++j) { + if (wordl[j] >= i) + printf(" # "); + else if (wordl[j] > 0) + printf(" "); } + printf("\n"); } }