kandr

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 6740296c1955baf26c77b0037de72a19862dfe8a
parent ea71f2d3d506df44c2a5d20c15c9f8d27428d321
Author: aabacchus <ben@bvnf.space>
Date:   Wed, 19 May 2021 01:48:25 +0100

vertical histogram

Diffstat:
M1.6.c | 34++++++++++++++++++++++++++++------
1 file changed, 28 insertions(+), 6 deletions(-)

diff --git 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"); }