diff options
author | aabacchus <ben@bvnf.space> | 2021-05-19 01:48:25 +0100 |
---|---|---|
committer | aabacchus <ben@bvnf.space> | 2021-05-19 01:48:25 +0100 |
commit | 6740296c1955baf26c77b0037de72a19862dfe8a (patch) | |
tree | 77a6a72e650cbc36ef25a4ef830c8121dba1b948 | |
parent | ea71f2d3d506df44c2a5d20c15c9f8d27428d321 (diff) | |
download | kandr-6740296c1955baf26c77b0037de72a19862dfe8a.tar.gz |
vertical histogram
-rw-r--r-- | 1.6.c | 34 |
1 files 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"); } |