From 56dcfa9375782c95b7d92aea3215e5541507b00d Mon Sep 17 00:00:00 2001 From: Cian Bagshaw Date: Thu, 4 May 2023 22:35:06 +0100 Subject: [PATCH] 1-13a Made vertical histogram --- 1/13.c | 2 +- 1/13a.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 1/13a.c diff --git a/1/13.c b/1/13.c index 53da9c3..2a6458b 100644 --- a/1/13.c +++ b/1/13.c @@ -28,7 +28,7 @@ int main () { } while (lngst--) { /* for each count */ printf("%2.d|", lngst+1); /* length */ - while (count[lngst]--) putchar('#');/* count (bar) */ + while (count[lngst]--) putchar('-');/* count (bar) */ putchar('\n'); /* newline */ } } diff --git a/1/13a.c b/1/13a.c new file mode 100644 index 0000000..1358227 --- /dev/null +++ b/1/13a.c @@ -0,0 +1,40 @@ +/* + Exercise 1-13. Write a program to print a histogram of the lengths of words in + its input. It is easy to draw the histogram with the bars horizontal; a + vertical orientation is more challenging. + === +*/ + +#include + +#define BUF 50 /* max word length */ + +int main () { + int c, l, i, m, lngst; /* character, length, index, most, longest */ + int count[BUF]; /* length count */ + + /* initialise to zero */ + for (i=0; ilngst) lngst=l; /* update longest */ + count[l-1]++; /* add length to count */ + if (count[l-1]>m) m=count[l-1]; /* update most */ + l=0; /* reset length count */ + } + } else l++; /* count word length */ + } + while (m--) { /* for each line */ + for (i=0; i<=lngst-1; i++) { /* for each count */ + if (count[i]>=m+1) printf(" | "); + else printf(" "); + } putchar('\n'); + } + for (i=1; i<=lngst; i++) /* labels */ + printf("%2.d ", i); + putchar('\n'); + +} -- 2.20.1