Exercise 1-13-2 (vertical - Histogram of word lengths)

Chapter_1     Exercise_1-13-1 Exercise_1-14-1







Exercise 1-13     K&R, p. 24


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.




histwv.c         download


#include <stdio.h> // for getchar(), putchar(), printf(), EOF

#define MAXLENGTHS 1000 // max no of different word lengths
#define FALSE 0
#define TRUE 1

// histogram of lengths of words, vertically

int main()
{
int c, i, j;
int wlengths[MAXLENGTHS]; // keep unsorted
int occurrences[MAXLENGTHS]; // match wlengths[]
int maxocc = 0; // max no of occurrences of a length
int wl = 0; // current word length
int nwl = 0; // no of different word lengths
int newlength = FALSE; // new word length

for (i = 0; i < MAXLENGTHS; i++) // initialize
{occurrences[i] = wlengths[i] = 0;}

while ((c = getchar()) != EOF)
{
if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v')
{
if (wl > 0) // new word
{
newlength = TRUE;
for (i = 0; i < nwl; i++)
{
if (wl == wlengths[i])
{
occurrences[i]++;
if (occurrences[i] > maxocc)
{maxocc = occurrences[i];}
newlength = FALSE;
break; // out of for()
}
}
if (newlength == TRUE)
{
newlength = FALSE; // reset newlength
wlengths[nwl] = wl; // word length
occurrences[nwl]++; // occurrences[nwl] = 1;
if (occurrences[nwl] > maxocc) // if (maxocc == 0)
{maxocc = occurrences[nwl];} // {maxocc = 0;}
nwl++; // array index starts at 0, initial value of nwl
}
wl = 0; // reset current word length
}
// else skip whitespace
}
else {wl++;} // not whitespace
}

printf("\t\tLEGEND:\n");
printf("Lengths:\t");
for (i = 0; i < nwl; i++) // print until wlengths[i] == 0
{printf("%d ",wlengths[i]);}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i < nwl; i++)
{printf("%d ",occurrences[i]);}
putchar('\n');

printf("\t\tHISTOGRAM:\n");
printf("Occ.:\t\\\tLengths:\n");
for (i = 0; i < nwl; i++)
{printf("%d ",wlengths[i]);}
putchar('\n');
for (i = 0; i < maxocc; i++)
{
for (j = 0; j < nwl; j++)
{
if(occurrences[j] > i)
{putchar('|');putchar(' ');}
else {putchar(' ');putchar(' ');} // indent
}
putchar('\n');
}
}
/*
gcc histwv.c -o histwv
./histwv < histwv.c // input source file
LEGEND:
Lengths: 8 9 7 10 4 2 3 5 1 6 21 24 11 15 12 17 16 13 19 18 14 25 27 32 23
Occurrences: 6 9 17 9 35 91 30 16 72 19 2 2 7 2 2 3 4 1 2 1 4 1 1 1 1
HISTOGRAM:
Occ.: \ Lengths:
8 9 7 10 4 2 3 5 1 6 21 24 11 15 12 17 16 13 19 18 14 25 27 32 23
| | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | |
.................................................

./histwv < histwv // input binary file
LEGEND:
Lengths: 56 240 7 174 103 24 228 53 19 67 123 23 270 15 168 ...
Occurrences: 1 1 11 1 1 1 1 1 2 1 1 10 1 3 1 ...
HISTOGRAM:
Occ.: \ Lengths:
56 240 7 174 103 24 228 53 19 67 123 23 270 15 168 ...
| | | | | | | | | | | | | | | ...
| | | | ...
.................................
*/









Chapter_1     Exercise_1-13-1 BACK_TO_TOP Exercise_1-14-1



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo