From 6740296c1955baf26c77b0037de72a19862dfe8a Mon Sep 17 00:00:00 2001 From: aabacchus Date: Wed, 19 May 2021 01:48:25 +0100 Subject: vertical histogram --- 1.6.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/1.6.c b/1.6.c index ae88521..1d4beef 100644 --- a/1.6.c +++ b/1.6.c @@ -3,8 +3,17 @@ #define INWORD 1 #define MAXWORDLEN 20 +int longest(const int *list) { + int max = 0; + int length = sizeof list / sizeof *list; + for (int i = 0; i < length; i++){ + if (list[i] > max) max = list[i]; + } + return max; +} + int main () { - int c, i, wordlen; + int c, i, j, wordlen; int ndigit[MAXWORDLEN] = {0}; wordlen = 0; char state = OUTWORD; @@ -20,10 +29,23 @@ int main () { ++wordlen; } } - for (i = 0; i < MAXWORDLEN; ++i) { - printf("%2d | ", i); - for (int j = 0; j < ndigit[i]; ++j) - printf("="); - printf("%d\n", ndigit[i]); + int max = longest(&ndigit); + // i counts down from the top to the bottom + // (actually from a couple above the top, for padding) + // we start printing each bar as i gets to be within the bar's value + for (i = max + 2; i > 0; i--) { + // side axis + printf("%2d |", i); + for (int j = 0; j < MAXWORDLEN; ++j) + printf(" %s ", i <= ndigit[j] ? "=" : " "); + printf("\n"); } + // bottom axis + printf(" +"); + for (j = 0; j < MAXWORDLEN; ++j) + printf("-|-"); + printf("\n |"); + for (j = 0; j < MAXWORDLEN; ++j) + printf("%3d", j); + printf("\n"); } -- cgit 1.4.1-2-gfad0