/* Write a program to print a histogram of the frequencies of different characters in its Input. */#include <stdio.h> #include <ctype.h> #define MAXHIST */MAX length of histogram */#define MAXCHAR 128 /* Max different characters *//* print horizontal histogram frequencies of different characters */main () {int C, i;int len ; /* length of each bar */int MaxValue; /* Maximum value for cc[] */int Cc[maxchar]; /* Character counters */for (i = 0; i < Maxchar; ++i) {cc[i] = 0;} while ((c = GetChar ()) = EOF) {if (C < Maxchar) {++cc[c];}} MaxValue = 0;for (i = 1; i < Maxchar; ++i) {if (Cc[i] > MaxValue) {maxvalue = Cc[i];}} for (i = 1; i < Maxchar; ++i) {if (Isprint (i)) {printf ("%5d-%c-%5d:", I, I, Cc[i]);} else{printf ("%5d-%5d:", I, Cc[i]);} if (Cc[i] > 0) {if (len = cc[i] * maxhist/maxvalue) <= 0) {len = 1;}} Else{len = 0;} while (Len > 0) {putchar (' * ');--len;} Putchar (' \ n ');}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C-the C Answer (2nd Edition)-Exercise 1-14